1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/treectlg.cpp
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
8 // Copyright: (c) 1998 Robert Roebling and Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treectrl.h"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/listbox.h"
36 #include "wx/textctrl.h"
39 #include "wx/generic/treectlg.h"
40 #include "wx/imaglist.h"
42 #include "wx/renderer.h"
45 #include "wx/osx/private.h"
48 // -----------------------------------------------------------------------------
50 // -----------------------------------------------------------------------------
52 class WXDLLIMPEXP_FWD_CORE wxGenericTreeItem
;
54 WX_DEFINE_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 static const int NO_IMAGE
= -1;
62 static const int PIXELS_PER_UNIT
= 10;
64 // the margin between the item state image and the item normal image
65 static const int MARGIN_BETWEEN_STATE_AND_IMAGE
= 2;
67 // the margin between the item image and the item text
68 static const int MARGIN_BETWEEN_IMAGE_AND_TEXT
= 4;
70 // -----------------------------------------------------------------------------
72 // -----------------------------------------------------------------------------
74 // timer used for enabling in-place edit
75 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
78 // start editing the current item after half a second (if the mouse hasn't
79 // been clicked/moved)
82 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
84 virtual void Notify();
87 wxGenericTreeCtrl
*m_owner
;
89 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
92 // control used for in-place edit
93 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
96 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
98 void EndEdit( bool discardChanges
);
100 const wxGenericTreeItem
* item() const { return m_itemEdited
; }
103 void OnChar( wxKeyEvent
&event
);
104 void OnKeyUp( wxKeyEvent
&event
);
105 void OnKillFocus( wxFocusEvent
&event
);
107 bool AcceptChanges();
108 void Finish( bool setfocus
);
111 wxGenericTreeCtrl
*m_owner
;
112 wxGenericTreeItem
*m_itemEdited
;
113 wxString m_startValue
;
114 bool m_aboutToFinish
;
116 DECLARE_EVENT_TABLE()
117 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
120 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
121 // for a sufficiently long time
122 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
125 // reset the current prefix after half a second of inactivity
126 enum { DELAY
= 500 };
128 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
130 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
133 wxGenericTreeCtrl
*m_owner
;
135 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
139 class WXDLLEXPORT wxGenericTreeItem
143 wxGenericTreeItem() { m_data
= NULL
; }
144 wxGenericTreeItem( wxGenericTreeItem
*parent
,
145 const wxString
& text
,
148 wxTreeItemData
*data
);
150 ~wxGenericTreeItem();
153 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
155 const wxString
& GetText() const { return m_text
; }
156 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
157 { return m_images
[which
]; }
158 wxTreeItemData
*GetData() const { return m_data
; }
159 int GetState() const { return m_state
; }
161 // returns the current image for the item (depending on its
162 // selected/expanded/whatever state)
163 int GetCurrentImage() const;
165 void SetText( const wxString
&text
) { m_text
= text
; }
166 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
167 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
168 void SetState(int state
) { m_state
= state
; }
170 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
172 void SetBold(bool bold
) { m_isBold
= bold
; }
174 int GetX() const { return m_x
; }
175 int GetY() const { return m_y
; }
177 void SetX(int x
) { m_x
= x
; }
178 void SetY(int y
) { m_y
= y
; }
180 int GetHeight() const { return m_height
; }
181 int GetWidth() const { return m_width
; }
183 void SetHeight(int h
) { m_height
= h
; }
184 void SetWidth(int w
) { m_width
= w
; }
186 wxGenericTreeItem
*GetParent() const { return m_parent
; }
190 // deletes all children notifying the treectrl about it
191 void DeleteChildren(wxGenericTreeCtrl
*tree
);
193 // get count of all children (and grand children if 'recursively')
194 size_t GetChildrenCount(bool recursively
= true) const;
196 void Insert(wxGenericTreeItem
*child
, size_t index
)
197 { m_children
.Insert(child
, index
); }
199 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
201 // return the item at given position (or NULL if no item), onButton is
202 // true if the point belongs to the item's button, otherwise it lies
203 // on the item's label
204 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
205 const wxGenericTreeCtrl
*,
209 void Expand() { m_isCollapsed
= false; }
210 void Collapse() { m_isCollapsed
= true; }
212 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
215 bool HasChildren() const { return !m_children
.IsEmpty(); }
216 bool IsSelected() const { return m_hasHilight
!= 0; }
217 bool IsExpanded() const { return !m_isCollapsed
; }
218 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
219 bool IsBold() const { return m_isBold
!= 0; }
222 // get them - may be NULL
223 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
224 // get them ensuring that the pointer is not NULL
225 wxTreeItemAttr
& Attr()
229 m_attr
= new wxTreeItemAttr
;
235 void SetAttributes(wxTreeItemAttr
*attr
)
237 if ( m_ownsAttr
) delete m_attr
;
241 // set them and delete when done
242 void AssignAttributes(wxTreeItemAttr
*attr
)
249 // since there can be very many of these, we save size by chosing
250 // the smallest representation for the elements and by ordering
251 // the members to avoid padding.
252 wxString m_text
; // label to be rendered for item
254 wxTreeItemData
*m_data
; // user-provided data
256 int m_state
; // item state
258 wxArrayGenericTreeItems m_children
; // list of children
259 wxGenericTreeItem
*m_parent
; // parent of this item
261 wxTreeItemAttr
*m_attr
; // attributes???
263 // tree ctrl images for the normal, selected, expanded and
264 // expanded+selected states
265 int m_images
[wxTreeItemIcon_Max
];
267 wxCoord m_x
; // (virtual) offset from top
268 wxCoord m_y
; // (virtual) offset from left
269 int m_width
; // width of this item
270 int m_height
; // height of this item
272 // use bitfields to save size
273 unsigned int m_isCollapsed
:1;
274 unsigned int m_hasHilight
:1; // same as focused
275 unsigned int m_hasPlus
:1; // used for item which doesn't have
276 // children but has a [+] button
277 unsigned int m_isBold
:1; // render the label in bold font
278 unsigned int m_ownsAttr
:1; // delete attribute when done
280 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
283 // =============================================================================
285 // =============================================================================
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 // translate the key or mouse event flags to the type of selection we're
293 static void EventFlagsToSelType(long style
,
297 bool &extended_select
,
298 bool &unselect_others
)
300 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
301 extended_select
= shiftDown
&& is_multiple
;
302 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
305 // check if the given item is under another one
306 static bool IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
310 if ( item
== parent
)
312 // item is a descendant of parent
316 item
= item
->GetParent();
322 // -----------------------------------------------------------------------------
323 // wxTreeRenameTimer (internal)
324 // -----------------------------------------------------------------------------
326 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
331 void wxTreeRenameTimer::Notify()
333 m_owner
->OnRenameTimer();
336 //-----------------------------------------------------------------------------
337 // wxTreeTextCtrl (internal)
338 //-----------------------------------------------------------------------------
340 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
341 EVT_CHAR (wxTreeTextCtrl::OnChar
)
342 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
343 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
346 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
347 wxGenericTreeItem
*item
)
348 : m_itemEdited(item
), m_startValue(item
->GetText())
351 m_aboutToFinish
= false;
353 int w
= m_itemEdited
->GetWidth(),
354 h
= m_itemEdited
->GetHeight();
357 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
362 int image
= item
->GetCurrentImage();
363 if ( image
!= NO_IMAGE
)
365 if ( m_owner
->m_imageListNormal
)
367 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
368 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
372 wxFAIL_MSG(_T("you must create an image list to use images!"));
376 // FIXME: what are all these hardcoded 4, 8 and 11s really?
380 wxSize bs
= DoGetBestSize() ;
381 // edit control height
384 int diff
= h
- ( bs
.y
- 8 ) ;
390 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
391 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
394 void wxTreeTextCtrl::EndEdit(bool discardChanges
)
396 m_aboutToFinish
= true;
398 if ( discardChanges
)
400 m_owner
->OnRenameCancelled(m_itemEdited
);
406 // Notify the owner about the changes
409 // Even if vetoed, close the control (consistent with MSW)
414 bool wxTreeTextCtrl::AcceptChanges()
416 const wxString value
= GetValue();
418 if ( value
== m_startValue
)
420 // nothing changed, always accept
421 // when an item remains unchanged, the owner
422 // needs to be notified that the user decided
423 // not to change the tree item label, and that
424 // the edit has been cancelled
426 m_owner
->OnRenameCancelled(m_itemEdited
);
430 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
432 // vetoed by the user
436 // accepted, do rename the item
437 m_owner
->SetItemText(m_itemEdited
, value
);
442 void wxTreeTextCtrl::Finish( bool setfocus
)
444 m_owner
->ResetTextControl();
446 wxPendingDelete
.Append(this);
452 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
454 switch ( event
.m_keyCode
)
469 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
471 if ( !m_aboutToFinish
)
473 // auto-grow the textctrl:
474 wxSize parentSize
= m_owner
->GetSize();
475 wxPoint myPos
= GetPosition();
476 wxSize mySize
= GetSize();
478 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
479 if (myPos
.x
+ sx
> parentSize
.x
)
480 sx
= parentSize
.x
- myPos
.x
;
483 SetSize(sx
, wxDefaultCoord
);
489 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
491 if ( !m_aboutToFinish
)
493 if ( !AcceptChanges() )
494 m_owner
->OnRenameCancelled( m_itemEdited
);
499 // We should let the native text control handle focus, too.
503 // -----------------------------------------------------------------------------
505 // -----------------------------------------------------------------------------
507 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
508 const wxString
& text
,
509 int image
, int selImage
,
510 wxTreeItemData
*data
)
513 m_images
[wxTreeItemIcon_Normal
] = image
;
514 m_images
[wxTreeItemIcon_Selected
] = selImage
;
515 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
516 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
519 m_state
= wxTREE_ITEMSTATE_NONE
;
522 m_isCollapsed
= true;
523 m_hasHilight
= false;
529 m_attr
= (wxTreeItemAttr
*)NULL
;
532 // We don't know the height here yet.
537 wxGenericTreeItem::~wxGenericTreeItem()
541 if (m_ownsAttr
) delete m_attr
;
543 wxASSERT_MSG( m_children
.IsEmpty(),
544 wxT("please call DeleteChildren() before deleting the item") );
547 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
549 size_t count
= m_children
.GetCount();
550 for ( size_t n
= 0; n
< count
; n
++ )
552 wxGenericTreeItem
*child
= m_children
[n
];
553 tree
->SendDeleteEvent(child
);
555 child
->DeleteChildren(tree
);
556 if ( child
== tree
->m_select_me
)
557 tree
->m_select_me
= NULL
;
564 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
566 size_t count
= m_children
.GetCount();
570 size_t total
= count
;
571 for (size_t n
= 0; n
< count
; ++n
)
573 total
+= m_children
[n
]->GetChildrenCount();
579 void wxGenericTreeItem::GetSize( int &x
, int &y
,
580 const wxGenericTreeCtrl
*theButton
)
582 int bottomY
=m_y
+theButton
->GetLineHeight(this);
583 if ( y
< bottomY
) y
= bottomY
;
584 int width
= m_x
+ m_width
;
585 if ( x
< width
) x
= width
;
589 size_t count
= m_children
.GetCount();
590 for ( size_t n
= 0; n
< count
; ++n
)
592 m_children
[n
]->GetSize( x
, y
, theButton
);
597 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
598 const wxGenericTreeCtrl
*theCtrl
,
602 // for a hidden root node, don't evaluate it, but do evaluate children
603 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
606 int h
= theCtrl
->GetLineHeight(this);
607 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
609 int y_mid
= m_y
+ h
/2;
610 if (point
.y
< y_mid
)
611 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
613 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
615 int xCross
= m_x
- theCtrl
->GetSpacing();
617 // according to the drawing code the triangels are drawn
618 // at -4 , -4 from the position up to +10/+10 max
619 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
620 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
621 HasPlus() && theCtrl
->HasButtons() )
623 // 5 is the size of the plus sign
624 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
625 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
626 HasPlus() && theCtrl
->HasButtons() )
629 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
633 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
638 // assuming every image (normal and selected) has the same size!
639 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
640 theCtrl
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
645 if ( (GetState() != wxTREE_ITEMSTATE_NONE
) && theCtrl
->m_imageListState
)
646 theCtrl
->m_imageListState
->GetSize(GetState(), state_w
, state_h
);
648 if ((state_w
!= -1) && (point
.x
<= m_x
+ state_w
+ 1))
649 flags
|= wxTREE_HITTEST_ONITEMSTATEICON
;
650 else if ((image_w
!= -1) &&
652 (state_w
!= -1 ? state_w
+ MARGIN_BETWEEN_STATE_AND_IMAGE
: 0)
654 flags
|= wxTREE_HITTEST_ONITEMICON
;
656 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
662 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
663 if (point
.x
> m_x
+m_width
)
664 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
669 // if children are expanded, fall through to evaluate them
670 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
674 size_t count
= m_children
.GetCount();
675 for ( size_t n
= 0; n
< count
; n
++ )
677 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
685 return (wxGenericTreeItem
*) NULL
;
688 int wxGenericTreeItem::GetCurrentImage() const
690 int image
= NO_IMAGE
;
695 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
698 if ( image
== NO_IMAGE
)
700 // we usually fall back to the normal item, but try just the
701 // expanded one (and not selected) first in this case
702 image
= GetImage(wxTreeItemIcon_Expanded
);
708 image
= GetImage(wxTreeItemIcon_Selected
);
711 // maybe it doesn't have the specific image we want,
712 // try the default one instead
713 if ( image
== NO_IMAGE
) image
= GetImage();
718 // -----------------------------------------------------------------------------
719 // wxGenericTreeCtrl implementation
720 // -----------------------------------------------------------------------------
722 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxControl
)
724 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
, wxTreeCtrlBase
)
725 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
726 EVT_SIZE (wxGenericTreeCtrl::OnSize
)
727 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
728 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
729 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
730 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
731 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
734 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
736 * wxTreeCtrl has to be a real class or we have problems with
737 * the run-time information.
740 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
743 // -----------------------------------------------------------------------------
744 // construction/destruction
745 // -----------------------------------------------------------------------------
747 void wxGenericTreeCtrl::Init()
752 m_select_me
= (wxGenericTreeItem
*) NULL
;
760 m_hilightBrush
= new wxBrush
762 wxSystemSettings::GetColour
764 wxSYS_COLOUR_HIGHLIGHT
769 m_hilightUnfocusedBrush
= new wxBrush
771 wxSystemSettings::GetColour
773 wxSYS_COLOUR_BTNSHADOW
778 m_imageListButtons
= NULL
;
779 m_ownsImageListButtons
= false;
782 m_isDragging
= false;
783 m_dropTarget
= m_oldSelection
= NULL
;
787 m_renameTimer
= NULL
;
791 m_dropEffectAboveItem
= false;
793 m_lastOnSame
= false;
795 #if defined( __WXMAC__ ) && wxOSX_USE_COCOA_OR_CARBON
796 m_normalFont
.MacCreateFromThemeFont( kThemeViewsFont
) ;
798 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
800 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
801 m_normalFont
.GetFamily(),
802 m_normalFont
.GetStyle(),
804 m_normalFont
.GetUnderlined(),
805 m_normalFont
.GetFaceName(),
806 m_normalFont
.GetEncoding());
809 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
814 const wxValidator
& validator
,
815 const wxString
& name
)
819 wxGetOsVersion(&major
, &minor
);
822 style
|= wxTR_ROW_LINES
;
825 if ( !wxControl::Create( parent
, id
, pos
, size
,
826 style
|wxHSCROLL
|wxVSCROLL
,
831 // If the tree display has no buttons, but does have
832 // connecting lines, we can use a narrower layout.
833 // It may not be a good idea to force this...
834 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
840 wxVisualAttributes attr
= GetDefaultAttributes();
841 SetOwnForegroundColour( attr
.colFg
);
842 SetOwnBackgroundColour( attr
.colBg
);
844 SetOwnFont(attr
.font
);
846 // this is a misnomer: it's called "dotted pen" but uses (default) wxSOLID
847 // style because we apparently get performance problems when using dotted
848 // pen for drawing in some ports -- but under MSW it seems to work fine
850 m_dottedPen
= wxPen(*wxLIGHT_GREY
, 0, wxPENSTYLE_DOT
);
852 m_dottedPen
= *wxGREY_PEN
;
855 SetInitialSize(size
);
860 wxGenericTreeCtrl::~wxGenericTreeCtrl()
862 delete m_hilightBrush
;
863 delete m_hilightUnfocusedBrush
;
867 delete m_renameTimer
;
870 if (m_ownsImageListButtons
)
871 delete m_imageListButtons
;
874 // -----------------------------------------------------------------------------
876 // -----------------------------------------------------------------------------
878 unsigned int wxGenericTreeCtrl::GetCount() const
886 unsigned int count
= m_anchor
->GetChildrenCount();
887 if ( !HasFlag(wxTR_HIDE_ROOT
) )
889 // take the root itself into account
896 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
898 m_indent
= (unsigned short) indent
;
903 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
904 bool recursively
) const
906 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
908 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
911 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
913 // Do not try to expand the root node if it hasn't been created yet
914 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
916 // if we will hide the root, make sure children are visible
917 m_anchor
->SetHasPlus();
919 CalculatePositions();
922 // right now, just sets the styles. Eventually, we may
923 // want to update the inherited styles, but right now
924 // none of the parents has updatable styles
925 m_windowStyle
= styles
;
929 // -----------------------------------------------------------------------------
930 // functions to work with tree items
931 // -----------------------------------------------------------------------------
933 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
935 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
937 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
940 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
941 wxTreeItemIcon which
) const
943 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
945 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
948 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
950 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
952 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
955 int wxGenericTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
957 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
959 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
960 return pItem
->GetState();
963 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
965 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
967 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
968 return pItem
->Attr().GetTextColour();
971 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
973 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
975 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
976 return pItem
->Attr().GetBackgroundColour();
979 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
981 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
983 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
984 return pItem
->Attr().GetFont();
987 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
989 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
992 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
993 pItem
->SetText(text
);
994 CalculateSize(pItem
, dc
);
998 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
1000 wxTreeItemIcon which
)
1002 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1004 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1005 pItem
->SetImage(image
, which
);
1007 wxClientDC
dc(this);
1008 CalculateSize(pItem
, dc
);
1012 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1014 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1017 data
->SetId( item
);
1019 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1022 void wxGenericTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
1024 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1026 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1027 pItem
->SetState(state
);
1031 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1033 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1035 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1036 pItem
->SetHasPlus(has
);
1040 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1042 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1044 // avoid redrawing the tree if no real change
1045 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1046 if ( pItem
->IsBold() != bold
)
1048 pItem
->SetBold(bold
);
1050 // recalculate the item size as bold and non bold fonts have different
1052 wxClientDC
dc(this);
1053 CalculateSize(pItem
, dc
);
1059 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1062 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1068 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1069 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1072 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1073 pItem
->Attr().SetTextColour(fg
);
1074 pItem
->Attr().SetBackgroundColour(bg
);
1078 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1079 const wxColour
& col
)
1081 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1083 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1084 pItem
->Attr().SetTextColour(col
);
1088 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1089 const wxColour
& col
)
1091 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1093 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1094 pItem
->Attr().SetBackgroundColour(col
);
1098 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1100 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1102 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1103 pItem
->Attr().SetFont(font
);
1107 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1109 wxTreeCtrlBase::SetFont(font
);
1111 m_normalFont
= font
;
1112 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1113 m_normalFont
.GetFamily(),
1114 m_normalFont
.GetStyle(),
1116 m_normalFont
.GetUnderlined(),
1117 m_normalFont
.GetFaceName(),
1118 m_normalFont
.GetEncoding());
1124 // -----------------------------------------------------------------------------
1125 // item status inquiries
1126 // -----------------------------------------------------------------------------
1128 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1130 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1132 // An item is only visible if it's not a descendant of a collapsed item
1133 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1134 wxGenericTreeItem
* parent
= pItem
->GetParent();
1137 if (!parent
->IsExpanded())
1139 parent
= parent
->GetParent();
1143 GetViewStart(& startX
, & startY
);
1145 wxSize clientSize
= GetClientSize();
1148 if (!GetBoundingRect(item
, rect
))
1150 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1152 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1154 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1160 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1162 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1164 // consider that the item does have children if it has the "+" button: it
1165 // might not have them (if it had never been expanded yet) but then it
1166 // could have them as well and it's better to err on this side rather than
1167 // disabling some operations which are restricted to the items with
1168 // children for an item which does have them
1169 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1172 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1174 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1176 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1179 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1181 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1183 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1186 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1188 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1190 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1193 // -----------------------------------------------------------------------------
1195 // -----------------------------------------------------------------------------
1197 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1199 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1201 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1204 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1205 wxTreeItemIdValue
& cookie
) const
1207 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1210 return GetNextChild(item
, cookie
);
1213 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1214 wxTreeItemIdValue
& cookie
) const
1216 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1218 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1220 // it's ok to cast cookie to size_t, we never have indices big enough to
1221 // overflow "void *"
1222 size_t *pIndex
= (size_t *)&cookie
;
1223 if ( *pIndex
< children
.GetCount() )
1225 return children
.Item((*pIndex
)++);
1229 // there are no more of them
1230 return wxTreeItemId();
1234 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1236 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1238 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1239 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1242 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1244 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1246 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1247 wxGenericTreeItem
*parent
= i
->GetParent();
1248 if ( parent
== NULL
)
1250 // root item doesn't have any siblings
1251 return wxTreeItemId();
1254 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1255 int index
= siblings
.Index(i
);
1256 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1258 size_t n
= (size_t)(index
+ 1);
1259 return n
== siblings
.GetCount() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1262 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1264 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1266 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1267 wxGenericTreeItem
*parent
= i
->GetParent();
1268 if ( parent
== NULL
)
1270 // root item doesn't have any siblings
1271 return wxTreeItemId();
1274 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1275 int index
= siblings
.Index(i
);
1276 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1278 return index
== 0 ? wxTreeItemId()
1279 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1282 // Only for internal use right now, but should probably be public
1283 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1285 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1287 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1289 // First see if there are any children.
1290 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1291 if (children
.GetCount() > 0)
1293 return children
.Item(0);
1297 // Try a sibling of this or ancestor instead
1298 wxTreeItemId p
= item
;
1299 wxTreeItemId toFind
;
1302 toFind
= GetNextSibling(p
);
1303 p
= GetItemParent(p
);
1304 } while (p
.IsOk() && !toFind
.IsOk());
1309 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1311 wxTreeItemId id
= GetRootItem();
1320 } while (id
.IsOk());
1322 return wxTreeItemId();
1325 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1327 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1328 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1330 wxTreeItemId id
= item
;
1333 while (id
= GetNext(id
), id
.IsOk())
1339 return wxTreeItemId();
1342 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1344 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1345 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1347 // find out the starting point
1348 wxTreeItemId prevItem
= GetPrevSibling(item
);
1349 if ( !prevItem
.IsOk() )
1351 prevItem
= GetItemParent(item
);
1354 // find the first visible item after it
1355 while ( prevItem
.IsOk() && !IsVisible(prevItem
) )
1357 prevItem
= GetNext(prevItem
);
1358 if ( !prevItem
.IsOk() || prevItem
== item
)
1360 // there are no visible items before item
1361 return wxTreeItemId();
1365 // from there we must be able to navigate until this item
1366 while ( prevItem
.IsOk() )
1368 const wxTreeItemId nextItem
= GetNextVisible(prevItem
);
1369 if ( !nextItem
.IsOk() || nextItem
== item
)
1372 prevItem
= nextItem
;
1378 // called by wxTextTreeCtrl when it marks itself for deletion
1379 void wxGenericTreeCtrl::ResetTextControl()
1384 // find the first item starting with the given prefix after the given item
1385 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1386 const wxString
& prefixOrig
) const
1388 // match is case insensitive as this is more convenient to the user: having
1389 // to press Shift-letter to go to the item starting with a capital letter
1390 // would be too bothersome
1391 wxString prefix
= prefixOrig
.Lower();
1393 // determine the starting point: we shouldn't take the current item (this
1394 // allows to switch between two items starting with the same letter just by
1395 // pressing it) but we shouldn't jump to the next one if the user is
1396 // continuing to type as otherwise he might easily skip the item he wanted
1397 wxTreeItemId id
= idParent
;
1398 if ( prefix
.length() == 1 )
1403 // look for the item starting with the given prefix after it
1404 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1409 // if we haven't found anything...
1412 // ... wrap to the beginning
1414 if ( HasFlag(wxTR_HIDE_ROOT
) )
1416 // can't select virtual root
1420 // and try all the items (stop when we get to the one we started from)
1421 while (id
.IsOk() && id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1425 // If we haven't found the item, id.IsOk() will be false, as per
1432 // -----------------------------------------------------------------------------
1434 // -----------------------------------------------------------------------------
1436 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1438 const wxString
& text
,
1441 wxTreeItemData
*data
)
1443 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1446 // should we give a warning here?
1447 return AddRoot(text
, image
, selImage
, data
);
1450 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1452 wxGenericTreeItem
*item
=
1453 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1457 data
->m_pItem
= item
;
1460 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1463 InvalidateBestSize();
1467 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1470 wxTreeItemData
*data
)
1472 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1474 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1476 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1477 image
, selImage
, data
);
1480 data
->m_pItem
= m_anchor
;
1483 if (HasFlag(wxTR_HIDE_ROOT
))
1485 // if root is hidden, make sure we can navigate
1487 m_anchor
->SetHasPlus();
1489 CalculatePositions();
1492 if (!HasFlag(wxTR_MULTIPLE
))
1494 m_current
= m_key_current
= m_anchor
;
1495 m_current
->SetHilight( true );
1498 InvalidateBestSize();
1502 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1503 const wxTreeItemId
& idPrevious
,
1504 const wxString
& text
,
1505 int image
, int selImage
,
1506 wxTreeItemData
*data
)
1508 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1511 // should we give a warning here?
1512 return AddRoot(text
, image
, selImage
, data
);
1516 if (idPrevious
.IsOk())
1518 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1519 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1520 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1523 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1527 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1529 wxTreeEvent
event(wxEVT_COMMAND_TREE_DELETE_ITEM
, this, item
);
1530 GetEventHandler()->ProcessEvent( event
);
1533 // Don't leave edit or selection on a child which is about to disappear
1534 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1536 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1537 m_textCtrl
->EndEdit( true );
1539 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1540 m_key_current
= NULL
;
1542 if (IsDescendantOf(item
, m_select_me
)) {
1545 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1546 m_current
->SetHilight( false );
1552 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1554 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1556 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1557 ChildrenClosing(item
);
1558 item
->DeleteChildren(this);
1559 InvalidateBestSize();
1562 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1564 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1566 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1568 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1570 // can't delete the item being edited, cancel editing it first
1571 m_textCtrl
->EndEdit( true );
1574 wxGenericTreeItem
*parent
= item
->GetParent();
1576 // don't keep stale pointers around!
1577 if ( IsDescendantOf(item
, m_key_current
) )
1579 // Don't silently change the selection:
1580 // do it properly in idle time, so event
1581 // handlers get called.
1583 // m_key_current = parent;
1584 m_key_current
= NULL
;
1587 // m_select_me records whether we need to select
1588 // a different item, in idle time.
1589 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1591 m_select_me
= parent
;
1594 if ( IsDescendantOf(item
, m_current
) )
1596 // Don't silently change the selection:
1597 // do it properly in idle time, so event
1598 // handlers get called.
1600 // m_current = parent;
1602 m_select_me
= parent
;
1605 // remove the item from the tree
1608 parent
->GetChildren().Remove( item
); // remove by value
1610 else // deleting the root
1612 // nothing will be left in the tree
1616 // and delete all of its children and the item itself now
1617 item
->DeleteChildren(this);
1618 SendDeleteEvent(item
);
1620 if (item
== m_select_me
)
1625 InvalidateBestSize();
1628 void wxGenericTreeCtrl::DeleteAllItems()
1636 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1638 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1640 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1641 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1642 _T("can't expand hidden root") );
1644 if ( !item
->HasPlus() )
1647 if ( item
->IsExpanded() )
1650 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_EXPANDING
, this, item
);
1652 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1654 // cancelled by program
1661 CalculatePositions();
1663 RefreshSubtree(item
);
1670 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1671 GetEventHandler()->ProcessEvent( event
);
1674 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1676 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1677 _T("can't collapse hidden root") );
1679 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1681 if ( !item
->IsExpanded() )
1684 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, this, item
);
1685 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1687 // cancelled by program
1691 ChildrenClosing(item
);
1694 #if 0 // TODO why should items be collapsed recursively?
1695 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1696 size_t count
= children
.GetCount();
1697 for ( size_t n
= 0; n
< count
; n
++ )
1699 Collapse(children
[n
]);
1703 CalculatePositions();
1705 RefreshSubtree(item
);
1707 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1708 GetEventHandler()->ProcessEvent( event
);
1711 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1714 DeleteChildren(item
);
1717 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1719 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1721 if (item
->IsExpanded())
1727 void wxGenericTreeCtrl::Unselect()
1731 m_current
->SetHilight( false );
1732 RefreshLine( m_current
);
1739 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1741 if (item
->IsSelected())
1743 item
->SetHilight(false);
1747 if (item
->HasChildren())
1749 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1750 size_t count
= children
.GetCount();
1751 for ( size_t n
= 0; n
< count
; ++n
)
1753 UnselectAllChildren(children
[n
]);
1758 void wxGenericTreeCtrl::UnselectAll()
1760 wxTreeItemId rootItem
= GetRootItem();
1762 // the tree might not have the root item at all
1765 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1769 // Recursive function !
1770 // To stop we must have crt_item<last_item
1772 // Tag all next children, when no more children,
1773 // Move to parent (not to tag)
1774 // Keep going... if we found last_item, we stop.
1775 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1777 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1779 if (parent
== NULL
) // This is root item
1780 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1782 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1783 int index
= children
.Index(crt_item
);
1784 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1786 size_t count
= children
.GetCount();
1787 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1789 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1792 return TagNextChildren(parent
, last_item
, select
);
1795 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1797 crt_item
->SetHilight(select
);
1798 RefreshLine(crt_item
);
1800 if (crt_item
==last_item
)
1803 if (crt_item
->HasChildren())
1805 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1806 size_t count
= children
.GetCount();
1807 for ( size_t n
= 0; n
< count
; ++n
)
1809 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1817 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1821 // item2 is not necessary after item1
1822 // choice first' and 'last' between item1 and item2
1823 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1824 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1826 bool select
= m_current
->IsSelected();
1828 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1831 TagNextChildren(first
,last
,select
);
1834 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1835 bool unselect_others
,
1836 bool extended_select
)
1838 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1842 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1843 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1845 //wxCHECK_RET( ( (!unselect_others) && is_single),
1846 // wxT("this is a single selection tree") );
1848 // to keep going anyhow !!!
1851 if (item
->IsSelected())
1852 return; // nothing to do
1853 unselect_others
= true;
1854 extended_select
= false;
1856 else if ( unselect_others
&& item
->IsSelected() )
1858 // selection change if there is more than one item currently selected
1859 wxArrayTreeItemIds selected_items
;
1860 if ( GetSelections(selected_items
) == 1 )
1864 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1865 event
.m_itemOld
= m_current
;
1866 // TODO : Here we don't send any selection mode yet !
1868 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1871 wxTreeItemId parent
= GetItemParent( itemId
);
1872 while (parent
.IsOk())
1874 if (!IsExpanded(parent
))
1877 parent
= GetItemParent( parent
);
1881 if (unselect_others
)
1883 if (is_single
) Unselect(); // to speed up thing
1888 if (extended_select
)
1892 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1895 // don't change the mark (m_current)
1896 SelectItemRange(m_current
, item
);
1900 bool select
= true; // the default
1902 // Check if we need to toggle hilight (ctrl mode)
1903 if (!unselect_others
)
1904 select
=!item
->IsSelected();
1906 m_current
= m_key_current
= item
;
1907 m_current
->SetHilight(select
);
1908 RefreshLine( m_current
);
1911 // This can cause idle processing to select the root
1912 // if no item is selected, so it must be after the
1914 EnsureVisible( itemId
);
1916 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1917 GetEventHandler()->ProcessEvent( event
);
1920 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1924 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
1928 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1929 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1931 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1932 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1935 item
->SetHilight(false);
1938 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1939 GetEventHandler()->ProcessEvent( event
);
1943 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1944 wxArrayTreeItemIds
&array
) const
1946 if ( item
->IsSelected() )
1947 array
.Add(wxTreeItemId(item
));
1949 if ( item
->HasChildren() )
1951 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1952 size_t count
= children
.GetCount();
1953 for ( size_t n
= 0; n
< count
; ++n
)
1954 FillArray(children
[n
], array
);
1958 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1961 wxTreeItemId idRoot
= GetRootItem();
1962 if ( idRoot
.IsOk() )
1964 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1966 //else: the tree is empty, so no selections
1968 return array
.GetCount();
1971 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1973 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1975 if (!item
.IsOk()) return;
1977 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1979 // first expand all parent branches
1980 wxGenericTreeItem
*parent
= gitem
->GetParent();
1982 if ( HasFlag(wxTR_HIDE_ROOT
) )
1984 while ( parent
&& parent
!= m_anchor
)
1987 parent
= parent
->GetParent();
1995 parent
= parent
->GetParent();
1999 //if (parent) CalculatePositions();
2004 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
2006 if (!item
.IsOk()) return;
2008 // We have to call this here because the label in
2009 // question might just have been added and no screen
2010 // update taken place.
2012 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2015 DoDirtyProcessing();
2017 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2019 // now scroll to the item
2020 int item_y
= gitem
->GetY();
2024 GetViewStart( &start_x
, &start_y
);
2025 start_y
*= PIXELS_PER_UNIT
;
2029 GetClientSize( &client_w
, &client_h
);
2031 if (item_y
< start_y
+3)
2036 m_anchor
->GetSize( x
, y
, this );
2037 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2038 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2039 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2040 // Item should appear at top
2041 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2043 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2048 m_anchor
->GetSize( x
, y
, this );
2049 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2050 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2051 item_y
+= PIXELS_PER_UNIT
+2;
2052 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2053 // Item should appear at bottom
2054 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, (item_y
+GetLineHeight(gitem
)-client_h
)/PIXELS_PER_UNIT
);
2058 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2059 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2061 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2062 wxGenericTreeItem
**item2
)
2064 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2066 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2069 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2071 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2073 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2075 wxCHECK_RET( !s_treeBeingSorted
,
2076 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2078 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2079 if ( children
.GetCount() > 1 )
2083 s_treeBeingSorted
= this;
2084 children
.Sort(tree_ctrl_compare_func
);
2085 s_treeBeingSorted
= NULL
;
2087 //else: don't make the tree dirty as nothing changed
2090 void wxGenericTreeCtrl::CalculateLineHeight()
2092 wxClientDC
dc(this);
2093 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2095 if ( m_imageListNormal
)
2097 // Calculate a m_lineHeight value from the normal Image sizes.
2098 // May be toggle off. Then wxGenericTreeCtrl will spread when
2099 // necessary (which might look ugly).
2100 int n
= m_imageListNormal
->GetImageCount();
2101 for (int i
= 0; i
< n
; i
++)
2103 int width
= 0, height
= 0;
2104 m_imageListNormal
->GetSize(i
, width
, height
);
2105 if (height
> m_lineHeight
) m_lineHeight
= height
;
2109 if ( m_imageListState
)
2111 // Calculate a m_lineHeight value from the state Image sizes.
2112 // May be toggle off. Then wxGenericTreeCtrl will spread when
2113 // necessary (which might look ugly).
2114 int n
= m_imageListState
->GetImageCount();
2115 for (int i
= 0; i
< n
; i
++)
2117 int width
= 0, height
= 0;
2118 m_imageListState
->GetSize(i
, width
, height
);
2119 if (height
> m_lineHeight
) m_lineHeight
= height
;
2123 if (m_imageListButtons
)
2125 // Calculate a m_lineHeight value from the Button image sizes.
2126 // May be toggle off. Then wxGenericTreeCtrl will spread when
2127 // necessary (which might look ugly).
2128 int n
= m_imageListButtons
->GetImageCount();
2129 for (int i
= 0; i
< n
; i
++)
2131 int width
= 0, height
= 0;
2132 m_imageListButtons
->GetSize(i
, width
, height
);
2133 if (height
> m_lineHeight
) m_lineHeight
= height
;
2137 if (m_lineHeight
< 30)
2138 m_lineHeight
+= 2; // at least 2 pixels
2140 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2143 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2145 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2146 m_imageListNormal
= imageList
;
2147 m_ownsImageListNormal
= false;
2149 // Don't do any drawing if we're setting the list to NULL,
2150 // since we may be in the process of deleting the tree control.
2152 CalculateLineHeight();
2155 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2157 if (m_ownsImageListState
) delete m_imageListState
;
2158 m_imageListState
= imageList
;
2159 m_ownsImageListState
= false;
2161 // Don't do any drawing if we're setting the list to NULL,
2162 // since we may be in the process of deleting the tree control.
2164 CalculateLineHeight();
2167 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2169 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2170 m_imageListButtons
= imageList
;
2171 m_ownsImageListButtons
= false;
2173 CalculateLineHeight();
2176 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2178 SetButtonsImageList(imageList
);
2179 m_ownsImageListButtons
= true;
2182 // -----------------------------------------------------------------------------
2184 // -----------------------------------------------------------------------------
2186 void wxGenericTreeCtrl::AdjustMyScrollbars()
2191 m_anchor
->GetSize( x
, y
, this );
2192 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2193 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2194 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2195 int y_pos
= GetScrollPos( wxVERTICAL
);
2196 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2200 SetScrollbars( 0, 0, 0, 0 );
2204 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2206 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2207 return item
->GetHeight();
2209 return m_lineHeight
;
2212 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2214 wxTreeItemAttr
*attr
= item
->GetAttributes();
2215 if ( attr
&& attr
->HasFont() )
2216 dc
.SetFont(attr
->GetFont());
2217 else if (item
->IsBold())
2218 dc
.SetFont(m_boldFont
);
2220 wxCoord text_w
= 0, text_h
= 0;
2221 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2223 int image_h
= 0, image_w
= 0;
2224 int image
= item
->GetCurrentImage();
2225 if ( image
!= NO_IMAGE
)
2227 if ( m_imageListNormal
)
2229 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2230 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2238 int state_h
= 0, state_w
= 0;
2239 int state
= item
->GetState();
2240 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2242 if ( m_imageListState
)
2244 m_imageListState
->GetSize( state
, state_w
, state_h
);
2245 if ( image
!= NO_IMAGE
)
2246 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
2248 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2252 state
= wxTREE_ITEMSTATE_NONE
;
2256 int total_h
= GetLineHeight(item
);
2257 bool drawItemBackground
= false;
2259 if ( item
->IsSelected() )
2261 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2262 drawItemBackground
= true;
2267 if ( attr
&& attr
->HasBackgroundColour() )
2269 drawItemBackground
= true;
2270 colBg
= attr
->GetBackgroundColour();
2274 colBg
= GetBackgroundColour();
2276 dc
.SetBrush(wxBrush(colBg
, wxBRUSHSTYLE_SOLID
));
2279 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2281 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2285 GetVirtualSize(&w
, &h
);
2286 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2287 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2288 dc
.DrawRectangle(rect
);
2290 if (!item
->IsSelected())
2292 dc
.DrawRectangle(rect
);
2296 int flags
= wxCONTROL_SELECTED
;
2298 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
2299 && IsControlActive( (ControlRef
)GetHandle() )
2302 flags
|= wxCONTROL_FOCUSED
;
2303 if ((item
== m_current
) && (m_hasFocus
))
2304 flags
|= wxCONTROL_CURRENT
;
2305 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2311 if ( item
->IsSelected() &&
2312 (state
!= wxTREE_ITEMSTATE_NONE
|| image
!= NO_IMAGE
) )
2314 // If it's selected, and there's an state image or normal image,
2315 // then we should take care to leave the area under the image
2316 // painted in the background colour.
2317 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2, item
->GetY() + offset
,
2318 item
->GetWidth() - state_w
- image_w
+ 2, total_h
- offset
);
2319 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2320 dc
.DrawRectangle( rect
);
2325 int flags
= wxCONTROL_SELECTED
;
2327 flags
|= wxCONTROL_FOCUSED
;
2328 if ((item
== m_current
) && (m_hasFocus
))
2329 flags
|= wxCONTROL_CURRENT
;
2330 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2333 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2334 // don't allow backgrounds to be customized. Not drawing the background,
2335 // except for custom item backgrounds, works for both kinds of theme.
2336 else if (drawItemBackground
)
2338 wxRect
rect( item
->GetX()-2, item
->GetY()+offset
,
2339 item
->GetWidth()+2, total_h
-offset
);
2340 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2341 dc
.DrawRectangle( rect
);
2343 if ( attr
&& attr
->HasBackgroundColour() )
2345 dc
.DrawRectangle( rect
);
2352 int flags
= wxCONTROL_SELECTED
;
2354 flags
|= wxCONTROL_FOCUSED
;
2355 if ((item
== m_current
) && (m_hasFocus
))
2356 flags
|= wxCONTROL_CURRENT
;
2357 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2363 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2365 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), state_w
, total_h
);
2366 m_imageListState
->Draw( state
, dc
,
2368 item
->GetY() + ((total_h
> state_h
)?((total_h
-state_h
)/2):0),
2369 wxIMAGELIST_DRAW_TRANSPARENT
);
2370 dc
.DestroyClippingRegion();
2373 if ( image
!= NO_IMAGE
)
2375 dc
.SetClippingRegion( item
->GetX() + state_w
, item
->GetY(), image_w
, total_h
);
2376 m_imageListNormal
->Draw( image
, dc
,
2377 item
->GetX() + state_w
,
2378 item
->GetY() + ((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2379 wxIMAGELIST_DRAW_TRANSPARENT
);
2380 dc
.DestroyClippingRegion();
2383 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
2384 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2385 dc
.DrawText( item
->GetText(),
2386 (wxCoord
)(state_w
+ image_w
+ item
->GetX()),
2387 (wxCoord
)(item
->GetY() + extraH
));
2389 // restore normal font
2390 dc
.SetFont( m_normalFont
);
2393 // Now y stands for the top of the item, whereas it used to stand for middle !
2394 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2396 int x
= level
*m_indent
;
2397 if (!HasFlag(wxTR_HIDE_ROOT
))
2401 else if (level
== 0)
2403 // always expand hidden root
2405 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2406 int count
= children
.GetCount();
2412 PaintLevel(children
[n
], dc
, 1, y
);
2413 } while (++n
< count
);
2415 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2417 // draw line down to last child
2418 origY
+= GetLineHeight(children
[0])>>1;
2419 oldY
+= GetLineHeight(children
[n
-1])>>1;
2420 dc
.DrawLine(3, origY
, 3, oldY
);
2426 item
->SetX(x
+m_spacing
);
2429 int h
= GetLineHeight(item
);
2431 int y_mid
= y_top
+ (h
>>1);
2434 int exposed_x
= dc
.LogicalToDeviceX(0);
2435 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2437 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2441 // don't draw rect outline if we already have the
2442 // background color under Mac
2443 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2444 #endif // !__WXMAC__
2448 if ( item
->IsSelected()
2449 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
2450 // On wxMac, if the tree doesn't have the focus we draw an empty
2451 // rectangle, so we want to make sure that the text is visible
2452 // against the normal background, not the highlightbackground, so
2453 // don't use the highlight text colour unless we have the focus.
2454 && m_hasFocus
&& IsControlActive( (ControlRef
)GetHandle() )
2461 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2466 wxTreeItemAttr
*attr
= item
->GetAttributes();
2467 if (attr
&& attr
->HasTextColour())
2468 colText
= attr
->GetTextColour();
2470 colText
= GetForegroundColour();
2474 dc
.SetTextForeground(colText
);
2478 PaintItem(item
, dc
);
2480 if (HasFlag(wxTR_ROW_LINES
))
2482 // if the background colour is white, choose a
2483 // contrasting color for the lines
2484 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2485 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2486 dc
.DrawLine(0, y_top
, 10000, y_top
);
2487 dc
.DrawLine(0, y
, 10000, y
);
2490 // restore DC objects
2491 dc
.SetBrush(*wxWHITE_BRUSH
);
2492 dc
.SetPen(m_dottedPen
);
2493 dc
.SetTextForeground(*wxBLACK
);
2495 if ( !HasFlag(wxTR_NO_LINES
) )
2497 // draw the horizontal line here
2499 if (x
> (signed)m_indent
)
2500 x_start
-= m_indent
;
2501 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2503 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2506 // should the item show a button?
2507 if ( item
->HasPlus() && HasButtons() )
2509 if ( m_imageListButtons
)
2511 // draw the image button here
2514 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2515 : wxTreeItemIcon_Normal
;
2516 if ( item
->IsSelected() )
2517 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2519 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2520 int xx
= x
- image_w
/2;
2521 int yy
= y_mid
- image_h
/2;
2523 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2524 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2525 wxIMAGELIST_DRAW_TRANSPARENT
);
2527 else // no custom buttons
2529 static const int wImage
= 9;
2530 static const int hImage
= 9;
2533 if (item
->IsExpanded())
2534 flag
|= wxCONTROL_EXPANDED
;
2535 if (item
== m_underMouse
)
2536 flag
|= wxCONTROL_CURRENT
;
2538 wxRendererNative::Get().DrawTreeItemButton
2542 wxRect(x
- wImage
/2,
2551 if (item
->IsExpanded())
2553 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2554 int count
= children
.GetCount();
2561 PaintLevel(children
[n
], dc
, level
, y
);
2562 } while (++n
< count
);
2564 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2566 // draw line down to last child
2567 oldY
+= GetLineHeight(children
[n
-1])>>1;
2568 if (HasButtons()) y_mid
+= 5;
2570 // Only draw the portion of the line that is visible, in case it is huge
2571 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2572 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2573 yOrigin
= abs(yOrigin
);
2574 GetClientSize(&width
, &height
);
2576 // Move end points to the begining/end of the view?
2577 if (y_mid
< yOrigin
)
2579 if (oldY
> yOrigin
+ height
)
2580 oldY
= yOrigin
+ height
;
2582 // after the adjustments if y_mid is larger than oldY then the line
2583 // isn't visible at all so don't draw anything
2585 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2591 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2595 if ( item
->HasPlus() )
2597 // it's a folder, indicate it by a border
2602 // draw a line under the drop target because the item will be
2604 DrawLine(item
, !m_dropEffectAboveItem
);
2607 SetCursor(wxCURSOR_BULLSEYE
);
2612 SetCursor(wxCURSOR_NO_ENTRY
);
2616 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2618 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2620 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2622 wxClientDC
dc(this);
2624 dc
.SetLogicalFunction(wxINVERT
);
2625 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2627 int w
= i
->GetWidth() + 2;
2628 int h
= GetLineHeight(i
) + 2;
2630 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2633 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2635 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2637 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2639 wxClientDC
dc(this);
2641 dc
.SetLogicalFunction(wxINVERT
);
2647 y
+= GetLineHeight(i
) - 1;
2650 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2653 // -----------------------------------------------------------------------------
2654 // wxWidgets callbacks
2655 // -----------------------------------------------------------------------------
2657 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
2660 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
2661 RefreshLine( m_current
);
2667 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2675 dc
.SetFont( m_normalFont
);
2676 dc
.SetPen( m_dottedPen
);
2678 // this is now done dynamically
2679 //if(GetImageList() == NULL)
2680 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2683 PaintLevel( m_anchor
, dc
, 0, y
);
2686 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2695 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2704 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2706 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2707 te
.m_evtKey
= event
;
2708 if ( GetEventHandler()->ProcessEvent( te
) )
2710 // intercepted by the user code
2714 if ( (m_current
== 0) || (m_key_current
== 0) )
2720 // how should the selection work for this event?
2721 bool is_multiple
, extended_select
, unselect_others
;
2722 EventFlagsToSelType(GetWindowStyleFlag(),
2725 is_multiple
, extended_select
, unselect_others
);
2727 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2729 if (event
.GetKeyCode() == WXK_RIGHT
)
2730 event
.m_keyCode
= WXK_LEFT
;
2731 else if (event
.GetKeyCode() == WXK_LEFT
)
2732 event
.m_keyCode
= WXK_RIGHT
;
2737 // * : Expand all/Collapse all
2738 // ' ' | return : activate
2739 // up : go up (not last children!)
2741 // left : go to parent
2742 // right : open if parent and go next
2743 // home : go to root
2744 // end : go to last item without opening parents
2745 // alnum : start or continue searching for the item with this prefix
2746 int keyCode
= event
.GetKeyCode();
2751 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2759 if ( !IsExpanded(m_current
) )
2762 ExpandAllChildren(m_current
);
2765 //else: fall through to Collapse() it
2769 if (IsExpanded(m_current
))
2771 Collapse(m_current
);
2777 // Use the item's bounding rectangle to determine position for the event
2779 GetBoundingRect(m_current
, ItemRect
, true);
2781 wxTreeEvent
eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU
, this, m_current
);
2782 // Use the left edge, vertical middle
2783 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
2784 ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2785 GetEventHandler()->ProcessEvent( eventMenu
);
2791 if ( !event
.HasModifiers() )
2793 wxTreeEvent
eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, m_current
);
2794 GetEventHandler()->ProcessEvent( eventAct
);
2797 // in any case, also generate the normal key event for this key,
2798 // even if we generated the ACTIVATED event above: this is what
2799 // wxMSW does and it makes sense because you might not want to
2800 // process ACTIVATED event at all and handle Space and Return
2801 // directly (and differently) which would be impossible otherwise
2805 // up goes to the previous sibling or to the last
2806 // of its children if it's expanded
2809 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2812 prev
= GetItemParent( m_key_current
);
2813 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2815 break; // don't go to root if it is hidden
2819 wxTreeItemIdValue cookie
;
2820 wxTreeItemId current
= m_key_current
;
2821 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2822 if (current
== GetFirstChild( prev
, cookie
))
2824 // otherwise we return to where we came from
2825 DoSelectItem( prev
, unselect_others
, extended_select
);
2826 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2833 while ( IsExpanded(prev
) && HasChildren(prev
) )
2835 wxTreeItemId child
= GetLastChild(prev
);
2842 DoSelectItem( prev
, unselect_others
, extended_select
);
2843 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2848 // left arrow goes to the parent
2851 wxTreeItemId prev
= GetItemParent( m_current
);
2852 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2854 // don't go to root if it is hidden
2855 prev
= GetPrevSibling( m_current
);
2859 DoSelectItem( prev
, unselect_others
, extended_select
);
2865 // this works the same as the down arrow except that we
2866 // also expand the item if it wasn't expanded yet
2867 if (m_current
!= GetRootItem().m_pItem
|| !HasFlag(wxTR_HIDE_ROOT
))
2869 //else: don't try to expand hidden root item (which can be the
2870 // current one when the tree is empty)
2876 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2878 wxTreeItemIdValue cookie
;
2879 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2883 DoSelectItem( child
, unselect_others
, extended_select
);
2884 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2888 wxTreeItemId next
= GetNextSibling( m_key_current
);
2891 wxTreeItemId current
= m_key_current
;
2892 while (current
.IsOk() && !next
)
2894 current
= GetItemParent( current
);
2895 if (current
) next
= GetNextSibling( current
);
2900 DoSelectItem( next
, unselect_others
, extended_select
);
2901 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2907 // <End> selects the last visible tree item
2910 wxTreeItemId last
= GetRootItem();
2912 while ( last
.IsOk() && IsExpanded(last
) )
2914 wxTreeItemId lastChild
= GetLastChild(last
);
2916 // it may happen if the item was expanded but then all of
2917 // its children have been deleted - so IsExpanded() returned
2918 // true, but GetLastChild() returned invalid item
2927 DoSelectItem( last
, unselect_others
, extended_select
);
2932 // <Home> selects the root item
2935 wxTreeItemId prev
= GetRootItem();
2939 if ( HasFlag(wxTR_HIDE_ROOT
) )
2941 wxTreeItemIdValue cookie
;
2942 prev
= GetFirstChild(prev
, cookie
);
2947 DoSelectItem( prev
, unselect_others
, extended_select
);
2952 // do not use wxIsalnum() here
2953 if ( !event
.HasModifiers() &&
2954 ((keyCode
>= '0' && keyCode
<= '9') ||
2955 (keyCode
>= 'a' && keyCode
<= 'z') ||
2956 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2958 // find the next item starting with the given prefix
2959 wxChar ch
= (wxChar
)keyCode
;
2961 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2972 // also start the timer to reset the current prefix if the user
2973 // doesn't press any more alnum keys soon -- we wouldn't want
2974 // to use this prefix for a new item search
2977 m_findTimer
= new wxTreeFindTimer(this);
2980 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2990 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2995 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2996 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2997 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2998 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2999 if (flags
) return wxTreeItemId();
3001 if (m_anchor
== NULL
)
3003 flags
= wxTREE_HITTEST_NOWHERE
;
3004 return wxTreeItemId();
3007 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
3011 flags
= wxTREE_HITTEST_NOWHERE
;
3012 return wxTreeItemId();
3017 // get the bounding rectangle of the item (or of its label only)
3018 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
3020 bool textOnly
) const
3022 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
3024 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
3029 rect
.width
= i
->GetWidth();
3031 if ( m_imageListNormal
)
3033 int image_w
, image_h
;
3034 m_imageListNormal
->GetSize( 0, image_w
, image_h
);
3035 rect
.width
+= image_w
+ MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3038 else // the entire line
3041 rect
.width
= GetClientSize().x
;
3045 rect
.height
= GetLineHeight(i
);
3047 // we have to return the logical coordinates, not physical ones
3048 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
3053 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
3054 wxClassInfo
* WXUNUSED(textCtrlClass
))
3056 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
3058 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
3060 wxTreeEvent
te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
3061 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
3067 // We have to call this here because the label in
3068 // question might just have been added and no screen
3069 // update taken place.
3071 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3074 DoDirtyProcessing();
3077 // TODO: use textCtrlClass here to create the control of correct class
3078 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
3080 m_textCtrl
->SetFocus();
3085 // returns a pointer to the text edit control if the item is being
3086 // edited, NULL otherwise (it's assumed that no more than one item may
3087 // be edited simultaneously)
3088 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3093 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3094 bool discardChanges
)
3096 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
3098 m_textCtrl
->EndEdit(discardChanges
);
3101 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3102 const wxString
& value
)
3104 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3106 le
.m_editCancelled
= false;
3108 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3111 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3113 // let owner know that the edit was cancelled
3114 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3115 le
.m_label
= wxEmptyString
;
3116 le
.m_editCancelled
= true;
3118 GetEventHandler()->ProcessEvent( le
);
3121 void wxGenericTreeCtrl::OnRenameTimer()
3123 EditLabel( m_current
);
3126 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3128 if ( !m_anchor
)return;
3130 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3132 // Is the mouse over a tree item button?
3134 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3135 wxGenericTreeItem
*underMouse
= thisItem
;
3137 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3138 #endif // wxUSE_TOOLTIPS
3141 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3142 (!event
.LeftIsDown()) &&
3144 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3152 if (underMouse
!= m_underMouse
)
3156 // unhighlight old item
3157 wxGenericTreeItem
*tmp
= m_underMouse
;
3158 m_underMouse
= NULL
;
3162 m_underMouse
= underMouse
;
3164 RefreshLine( m_underMouse
);
3168 // Determines what item we are hovering over and need a tooltip for
3169 wxTreeItemId hoverItem
= thisItem
;
3171 // We do not want a tooltip if we are dragging, or if the rename timer is running
3172 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3174 // Ask the tree control what tooltip (if any) should be shown
3175 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3177 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3179 SetToolTip(hevent
.m_label
);
3184 // we process left mouse up event (enables in-place edit), middle/right down
3185 // (pass to the user code), left dbl click (activate item) and
3186 // dragging/moving events for items drag-and-drop
3187 if ( !(event
.LeftDown() ||
3189 event
.MiddleDown() ||
3190 event
.RightDown() ||
3191 event
.LeftDClick() ||
3193 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3202 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3204 if ( event
.Dragging() && !m_isDragging
)
3206 if (m_dragCount
== 0)
3211 if (m_dragCount
!= 3)
3213 // wait until user drags a bit further...
3217 wxEventType command
= event
.RightIsDown()
3218 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3219 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3221 wxTreeEvent
nevent(command
, this, m_current
);
3222 nevent
.SetPoint(CalcScrolledPosition(pt
));
3224 // by default the dragging is not supported, the user code must
3225 // explicitly allow the event for it to take place
3228 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3230 // we're going to drag this item
3231 m_isDragging
= true;
3233 // remember the old cursor because we will change it while
3235 m_oldCursor
= m_cursor
;
3237 // in a single selection control, hide the selection temporarily
3238 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3240 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3242 if ( m_oldSelection
)
3244 m_oldSelection
->SetHilight(false);
3245 RefreshLine(m_oldSelection
);
3252 else if ( event
.Dragging() )
3254 if ( item
!= m_dropTarget
)
3256 // unhighlight the previous drop target
3257 DrawDropEffect(m_dropTarget
);
3259 m_dropTarget
= item
;
3261 // highlight the current drop target if any
3262 DrawDropEffect(m_dropTarget
);
3264 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3271 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3275 // erase the highlighting
3276 DrawDropEffect(m_dropTarget
);
3278 if ( m_oldSelection
)
3280 m_oldSelection
->SetHilight(true);
3281 RefreshLine(m_oldSelection
);
3282 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3285 // generate the drag end event
3286 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, this, item
);
3288 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3290 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3292 m_isDragging
= false;
3293 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3295 SetCursor(m_oldCursor
);
3297 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3305 // If we got to this point, we are not dragging or moving the mouse.
3306 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3307 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3308 // We skip even if we didn't hit an item because we still should
3309 // restore focus to the tree control even if we didn't exactly hit an item.
3310 if ( event
.LeftDown() )
3315 // here we process only the messages which happen on tree items
3319 if (item
== NULL
) return; /* we hit the blank area */
3321 if ( event
.RightDown() )
3323 // If the item is already selected, do not update the selection.
3324 // Multi-selections should not be cleared if a selected item is clicked.
3325 if (!IsSelected(item
))
3327 DoSelectItem(item
, true, false);
3330 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, this, item
);
3331 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3332 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3334 // Consistent with MSW (for now), send the ITEM_MENU *after*
3335 // the RIGHT_CLICK event. TODO: This behavior may change.
3336 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
3337 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3338 GetEventHandler()->ProcessEvent(nevent2
);
3340 else if ( event
.MiddleDown() )
3342 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
, this, item
);
3343 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3344 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3346 else if ( event
.LeftUp() )
3348 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3350 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, this, item
);
3351 GetEventHandler()->ProcessEvent(nevent
);
3354 // this facilitates multiple-item drag-and-drop
3356 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3358 wxArrayTreeItemIds selections
;
3359 size_t count
= GetSelections(selections
);
3365 DoSelectItem(item
, true, false);
3371 if ( (item
== m_current
) &&
3372 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3373 HasFlag(wxTR_EDIT_LABELS
) )
3375 if ( m_renameTimer
)
3377 if ( m_renameTimer
->IsRunning() )
3378 m_renameTimer
->Stop();
3382 m_renameTimer
= new wxTreeRenameTimer( this );
3385 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3388 m_lastOnSame
= false;
3391 else // !RightDown() && !MiddleDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3393 if ( event
.LeftDown() )
3395 m_lastOnSame
= item
== m_current
;
3398 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3400 // only toggle the item for a single click, double click on
3401 // the button doesn't do anything (it toggles the item twice)
3402 if ( event
.LeftDown() )
3407 // don't select the item if the button was clicked
3412 // clear the previously selected items, if the
3413 // user clicked outside of the present selection.
3414 // otherwise, perform the deselection on mouse-up.
3415 // this allows multiple drag and drop to work.
3416 // but if Cmd is down, toggle selection of the clicked item
3417 if (!IsSelected(item
) || event
.CmdDown())
3419 // how should the selection work for this event?
3420 bool is_multiple
, extended_select
, unselect_others
;
3421 EventFlagsToSelType(GetWindowStyleFlag(),
3424 is_multiple
, extended_select
, unselect_others
);
3426 DoSelectItem(item
, unselect_others
, extended_select
);
3430 // For some reason, Windows isn't recognizing a left double-click,
3431 // so we need to simulate it here. Allow 200 milliseconds for now.
3432 if ( event
.LeftDClick() )
3434 // double clicking should not start editing the item label
3435 if ( m_renameTimer
)
3436 m_renameTimer
->Stop();
3438 m_lastOnSame
= false;
3440 // send activate event first
3441 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, item
);
3442 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3443 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3445 // if the user code didn't process the activate event,
3446 // handle it ourselves by toggling the item when it is
3448 if ( item
->HasPlus() )
3458 void wxGenericTreeCtrl::OnInternalIdle()
3460 wxWindow::OnInternalIdle();
3462 // Check if we need to select the root item
3463 // because nothing else has been selected.
3464 // Delaying it means that we can invoke event handlers
3465 // as required, when a first item is selected.
3466 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3469 SelectItem(m_select_me
);
3470 else if (GetRootItem().IsOk())
3471 SelectItem(GetRootItem());
3474 // after all changes have been done to the tree control,
3475 // actually redraw the tree when everything is over
3477 DoDirtyProcessing();
3480 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3485 wxTreeItemAttr
*attr
= item
->GetAttributes();
3486 if ( attr
&& attr
->HasFont() )
3487 dc
.SetFont(attr
->GetFont());
3488 else if ( item
->IsBold() )
3489 dc
.SetFont(m_boldFont
);
3491 dc
.SetFont(m_normalFont
);
3493 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3496 // restore normal font
3497 dc
.SetFont( m_normalFont
);
3501 int image
= item
->GetCurrentImage();
3502 if ( image
!= NO_IMAGE
)
3504 if ( m_imageListNormal
)
3506 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3507 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3511 int state_h
= 0, state_w
= 0;
3512 int state
= item
->GetState();
3513 if ( state
!= wxTREE_ITEMSTATE_NONE
)
3515 if ( m_imageListState
)
3517 m_imageListState
->GetSize( state
, state_w
, state_h
);
3518 if ( image
!= NO_IMAGE
)
3519 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
3521 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3525 state
= wxTREE_ITEMSTATE_NONE
;
3529 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3532 total_h
+= 2; // at least 2 pixels
3534 total_h
+= total_h
/10; // otherwise 10% extra spacing
3536 item
->SetHeight(total_h
);
3537 if (total_h
>m_lineHeight
)
3538 m_lineHeight
=total_h
;
3540 item
->SetWidth(state_w
+ image_w
+ text_w
+ 2);
3543 // -----------------------------------------------------------------------------
3544 // for developper : y is now the top of the level
3545 // not the middle of it !
3546 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3548 int x
= level
*m_indent
;
3549 if (!HasFlag(wxTR_HIDE_ROOT
))
3553 else if (level
== 0)
3555 // a hidden root is not evaluated, but its
3556 // children are always calculated
3560 CalculateSize( item
, dc
);
3563 item
->SetX( x
+m_spacing
);
3565 y
+= GetLineHeight(item
);
3567 if ( !item
->IsExpanded() )
3569 // we don't need to calculate collapsed branches
3574 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3575 size_t n
, count
= children
.GetCount();
3577 for (n
= 0; n
< count
; ++n
)
3578 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3581 void wxGenericTreeCtrl::CalculatePositions()
3583 if ( !m_anchor
) return;
3585 wxClientDC
dc(this);
3588 dc
.SetFont( m_normalFont
);
3590 dc
.SetPen( m_dottedPen
);
3591 //if(GetImageList() == NULL)
3592 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3595 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3598 void wxGenericTreeCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
3601 wxTreeCtrlBase::Refresh(eraseBackground
, rect
);
3604 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3606 if (m_dirty
|| IsFrozen() )
3609 wxSize client
= GetClientSize();
3612 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3613 rect
.width
= client
.x
;
3614 rect
.height
= client
.y
;
3616 Refresh(true, &rect
);
3618 AdjustMyScrollbars();
3621 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3623 if (m_dirty
|| IsFrozen() )
3627 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3628 rect
.width
= GetClientSize().x
;
3629 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3631 Refresh(true, &rect
);
3634 void wxGenericTreeCtrl::RefreshSelected()
3639 // TODO: this is awfully inefficient, we should keep the list of all
3640 // selected items internally, should be much faster
3642 RefreshSelectedUnder(m_anchor
);
3645 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3650 if ( item
->IsSelected() )
3653 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3654 size_t count
= children
.GetCount();
3655 for ( size_t n
= 0; n
< count
; n
++ )
3657 RefreshSelectedUnder(children
[n
]);
3661 void wxGenericTreeCtrl::DoThaw()
3663 wxTreeCtrlBase::DoThaw();
3666 DoDirtyProcessing();
3671 // ----------------------------------------------------------------------------
3672 // changing colours: we need to refresh the tree control
3673 // ----------------------------------------------------------------------------
3675 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3677 if ( !wxWindow::SetBackgroundColour(colour
) )
3685 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3687 if ( !wxWindow::SetForegroundColour(colour
) )
3695 // Process the tooltip event, to speed up event processing.
3696 // Doesn't actually get a tooltip.
3697 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3703 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3704 // be removed, as well as the #else case below.
3705 #define _USE_VISATTR 0
3710 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3712 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3716 // Use the same color scheme as wxListBox
3717 return wxListBox::GetClassDefaultAttributes(variant
);
3719 wxVisualAttributes attr
;
3720 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3721 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3722 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3727 void wxGenericTreeCtrl::DoDirtyProcessing()
3734 CalculatePositions();
3736 AdjustMyScrollbars();
3739 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
3741 // make sure all positions are calculated as normally this only done during
3742 // idle time but we need them for base class DoGetBestSize() to return the
3744 wxConstCast(this, wxGenericTreeCtrl
)->CalculatePositions();
3746 wxSize size
= wxTreeCtrlBase::DoGetBestSize();
3748 // there seems to be an implicit extra border around the items, although
3749 // I'm not really sure where does it come from -- but without this, the
3750 // scrollbars appear in a tree with default/best size
3753 // and the border has to be rounded up to a multiple of PIXELS_PER_UNIT or
3754 // scrollbars still appear
3755 const wxSize
& borderSize
= GetWindowBorderSize();
3757 int dx
= (size
.x
- borderSize
.x
) % PIXELS_PER_UNIT
;
3759 size
.x
+= PIXELS_PER_UNIT
- dx
;
3760 int dy
= (size
.y
- borderSize
.y
) % PIXELS_PER_UNIT
;
3762 size
.y
+= PIXELS_PER_UNIT
- dy
;
3764 // we need to update the cache too as the base class cached its own value
3765 CacheBestSize(size
);
3770 #endif // wxUSE_TREECTRL