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__ )
797 m_normalFont
.MacCreateFromThemeFont( kThemeViewsFont
) ;
799 m_normalFont
.MacCreateFromUIFont( kCTFontViewsFontType
) ;
802 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
804 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
805 m_normalFont
.GetFamily(),
806 m_normalFont
.GetStyle(),
808 m_normalFont
.GetUnderlined(),
809 m_normalFont
.GetFaceName(),
810 m_normalFont
.GetEncoding());
813 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
818 const wxValidator
& validator
,
819 const wxString
& name
)
823 wxGetOsVersion(&major
, &minor
);
826 style
|= wxTR_ROW_LINES
;
829 if ( !wxControl::Create( parent
, id
, pos
, size
,
830 style
|wxHSCROLL
|wxVSCROLL
,
835 // If the tree display has no buttons, but does have
836 // connecting lines, we can use a narrower layout.
837 // It may not be a good idea to force this...
838 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
844 wxVisualAttributes attr
= GetDefaultAttributes();
845 SetOwnForegroundColour( attr
.colFg
);
846 SetOwnBackgroundColour( attr
.colBg
);
848 SetOwnFont(attr
.font
);
850 // this is a misnomer: it's called "dotted pen" but uses (default) wxSOLID
851 // style because we apparently get performance problems when using dotted
852 // pen for drawing in some ports -- but under MSW it seems to work fine
854 m_dottedPen
= wxPen(*wxLIGHT_GREY
, 0, wxPENSTYLE_DOT
);
856 m_dottedPen
= *wxGREY_PEN
;
859 SetInitialSize(size
);
864 wxGenericTreeCtrl::~wxGenericTreeCtrl()
866 delete m_hilightBrush
;
867 delete m_hilightUnfocusedBrush
;
871 delete m_renameTimer
;
874 if (m_ownsImageListButtons
)
875 delete m_imageListButtons
;
878 // -----------------------------------------------------------------------------
880 // -----------------------------------------------------------------------------
882 unsigned int wxGenericTreeCtrl::GetCount() const
890 unsigned int count
= m_anchor
->GetChildrenCount();
891 if ( !HasFlag(wxTR_HIDE_ROOT
) )
893 // take the root itself into account
900 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
902 m_indent
= (unsigned short) indent
;
907 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
908 bool recursively
) const
910 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
912 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
915 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
917 // Do not try to expand the root node if it hasn't been created yet
918 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
920 // if we will hide the root, make sure children are visible
921 m_anchor
->SetHasPlus();
923 CalculatePositions();
926 // right now, just sets the styles. Eventually, we may
927 // want to update the inherited styles, but right now
928 // none of the parents has updatable styles
929 m_windowStyle
= styles
;
933 // -----------------------------------------------------------------------------
934 // functions to work with tree items
935 // -----------------------------------------------------------------------------
937 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
939 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
941 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
944 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
945 wxTreeItemIcon which
) const
947 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
949 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
952 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
954 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
956 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
959 int wxGenericTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
961 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
963 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
964 return pItem
->GetState();
967 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
969 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
971 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
972 return pItem
->Attr().GetTextColour();
975 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
977 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
979 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
980 return pItem
->Attr().GetBackgroundColour();
983 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
985 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
987 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
988 return pItem
->Attr().GetFont();
991 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
993 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
996 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
997 pItem
->SetText(text
);
998 CalculateSize(pItem
, dc
);
1002 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
1004 wxTreeItemIcon which
)
1006 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1008 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1009 pItem
->SetImage(image
, which
);
1011 wxClientDC
dc(this);
1012 CalculateSize(pItem
, dc
);
1016 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1018 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1021 data
->SetId( item
);
1023 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1026 void wxGenericTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
1028 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1030 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1031 pItem
->SetState(state
);
1035 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1037 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1039 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1040 pItem
->SetHasPlus(has
);
1044 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1046 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1048 // avoid redrawing the tree if no real change
1049 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1050 if ( pItem
->IsBold() != bold
)
1052 pItem
->SetBold(bold
);
1054 // recalculate the item size as bold and non bold fonts have different
1056 wxClientDC
dc(this);
1057 CalculateSize(pItem
, dc
);
1063 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1066 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1072 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1073 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1076 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1077 pItem
->Attr().SetTextColour(fg
);
1078 pItem
->Attr().SetBackgroundColour(bg
);
1082 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1083 const wxColour
& col
)
1085 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1087 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1088 pItem
->Attr().SetTextColour(col
);
1092 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1093 const wxColour
& col
)
1095 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1097 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1098 pItem
->Attr().SetBackgroundColour(col
);
1102 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1104 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1106 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1107 pItem
->Attr().SetFont(font
);
1111 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1113 wxTreeCtrlBase::SetFont(font
);
1115 m_normalFont
= font
;
1116 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1117 m_normalFont
.GetFamily(),
1118 m_normalFont
.GetStyle(),
1120 m_normalFont
.GetUnderlined(),
1121 m_normalFont
.GetFaceName(),
1122 m_normalFont
.GetEncoding());
1128 // -----------------------------------------------------------------------------
1129 // item status inquiries
1130 // -----------------------------------------------------------------------------
1132 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1134 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1136 // An item is only visible if it's not a descendant of a collapsed item
1137 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1138 wxGenericTreeItem
* parent
= pItem
->GetParent();
1141 if (!parent
->IsExpanded())
1143 parent
= parent
->GetParent();
1147 GetViewStart(& startX
, & startY
);
1149 wxSize clientSize
= GetClientSize();
1152 if (!GetBoundingRect(item
, rect
))
1154 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1156 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1158 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1164 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1166 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1168 // consider that the item does have children if it has the "+" button: it
1169 // might not have them (if it had never been expanded yet) but then it
1170 // could have them as well and it's better to err on this side rather than
1171 // disabling some operations which are restricted to the items with
1172 // children for an item which does have them
1173 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1176 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1178 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1180 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1183 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1185 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1187 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1190 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1192 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1194 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1197 // -----------------------------------------------------------------------------
1199 // -----------------------------------------------------------------------------
1201 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1203 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1205 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1208 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1209 wxTreeItemIdValue
& cookie
) const
1211 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1214 return GetNextChild(item
, cookie
);
1217 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1218 wxTreeItemIdValue
& cookie
) const
1220 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1222 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1224 // it's ok to cast cookie to size_t, we never have indices big enough to
1225 // overflow "void *"
1226 size_t *pIndex
= (size_t *)&cookie
;
1227 if ( *pIndex
< children
.GetCount() )
1229 return children
.Item((*pIndex
)++);
1233 // there are no more of them
1234 return wxTreeItemId();
1238 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1240 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1242 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1243 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1246 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1248 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1250 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1251 wxGenericTreeItem
*parent
= i
->GetParent();
1252 if ( parent
== NULL
)
1254 // root item doesn't have any siblings
1255 return wxTreeItemId();
1258 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1259 int index
= siblings
.Index(i
);
1260 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1262 size_t n
= (size_t)(index
+ 1);
1263 return n
== siblings
.GetCount() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1266 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1268 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1270 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1271 wxGenericTreeItem
*parent
= i
->GetParent();
1272 if ( parent
== NULL
)
1274 // root item doesn't have any siblings
1275 return wxTreeItemId();
1278 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1279 int index
= siblings
.Index(i
);
1280 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1282 return index
== 0 ? wxTreeItemId()
1283 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1286 // Only for internal use right now, but should probably be public
1287 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1289 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1291 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1293 // First see if there are any children.
1294 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1295 if (children
.GetCount() > 0)
1297 return children
.Item(0);
1301 // Try a sibling of this or ancestor instead
1302 wxTreeItemId p
= item
;
1303 wxTreeItemId toFind
;
1306 toFind
= GetNextSibling(p
);
1307 p
= GetItemParent(p
);
1308 } while (p
.IsOk() && !toFind
.IsOk());
1313 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1315 wxTreeItemId id
= GetRootItem();
1324 } while (id
.IsOk());
1326 return wxTreeItemId();
1329 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1331 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1332 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1334 wxTreeItemId id
= item
;
1337 while (id
= GetNext(id
), id
.IsOk())
1343 return wxTreeItemId();
1346 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1348 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1349 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1351 // find out the starting point
1352 wxTreeItemId prevItem
= GetPrevSibling(item
);
1353 if ( !prevItem
.IsOk() )
1355 prevItem
= GetItemParent(item
);
1358 // find the first visible item after it
1359 while ( prevItem
.IsOk() && !IsVisible(prevItem
) )
1361 prevItem
= GetNext(prevItem
);
1362 if ( !prevItem
.IsOk() || prevItem
== item
)
1364 // there are no visible items before item
1365 return wxTreeItemId();
1369 // from there we must be able to navigate until this item
1370 while ( prevItem
.IsOk() )
1372 const wxTreeItemId nextItem
= GetNextVisible(prevItem
);
1373 if ( !nextItem
.IsOk() || nextItem
== item
)
1376 prevItem
= nextItem
;
1382 // called by wxTextTreeCtrl when it marks itself for deletion
1383 void wxGenericTreeCtrl::ResetTextControl()
1388 // find the first item starting with the given prefix after the given item
1389 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1390 const wxString
& prefixOrig
) const
1392 // match is case insensitive as this is more convenient to the user: having
1393 // to press Shift-letter to go to the item starting with a capital letter
1394 // would be too bothersome
1395 wxString prefix
= prefixOrig
.Lower();
1397 // determine the starting point: we shouldn't take the current item (this
1398 // allows to switch between two items starting with the same letter just by
1399 // pressing it) but we shouldn't jump to the next one if the user is
1400 // continuing to type as otherwise he might easily skip the item he wanted
1401 wxTreeItemId id
= idParent
;
1402 if ( prefix
.length() == 1 )
1407 // look for the item starting with the given prefix after it
1408 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1413 // if we haven't found anything...
1416 // ... wrap to the beginning
1418 if ( HasFlag(wxTR_HIDE_ROOT
) )
1420 // can't select virtual root
1424 // and try all the items (stop when we get to the one we started from)
1425 while (id
.IsOk() && id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1429 // If we haven't found the item, id.IsOk() will be false, as per
1436 // -----------------------------------------------------------------------------
1438 // -----------------------------------------------------------------------------
1440 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1442 const wxString
& text
,
1445 wxTreeItemData
*data
)
1447 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1450 // should we give a warning here?
1451 return AddRoot(text
, image
, selImage
, data
);
1454 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1456 wxGenericTreeItem
*item
=
1457 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1461 data
->m_pItem
= item
;
1464 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1467 InvalidateBestSize();
1471 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1474 wxTreeItemData
*data
)
1476 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1478 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1480 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1481 image
, selImage
, data
);
1484 data
->m_pItem
= m_anchor
;
1487 if (HasFlag(wxTR_HIDE_ROOT
))
1489 // if root is hidden, make sure we can navigate
1491 m_anchor
->SetHasPlus();
1493 CalculatePositions();
1496 if (!HasFlag(wxTR_MULTIPLE
))
1498 m_current
= m_key_current
= m_anchor
;
1499 m_current
->SetHilight( true );
1502 InvalidateBestSize();
1506 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1507 const wxTreeItemId
& idPrevious
,
1508 const wxString
& text
,
1509 int image
, int selImage
,
1510 wxTreeItemData
*data
)
1512 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1515 // should we give a warning here?
1516 return AddRoot(text
, image
, selImage
, data
);
1520 if (idPrevious
.IsOk())
1522 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1523 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1524 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1527 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1531 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1533 wxTreeEvent
event(wxEVT_COMMAND_TREE_DELETE_ITEM
, this, item
);
1534 GetEventHandler()->ProcessEvent( event
);
1537 // Don't leave edit or selection on a child which is about to disappear
1538 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1540 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1541 m_textCtrl
->EndEdit( true );
1543 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1544 m_key_current
= NULL
;
1546 if (IsDescendantOf(item
, m_select_me
)) {
1549 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1550 m_current
->SetHilight( false );
1556 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1558 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1560 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1561 ChildrenClosing(item
);
1562 item
->DeleteChildren(this);
1563 InvalidateBestSize();
1566 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1568 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1570 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1572 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1574 // can't delete the item being edited, cancel editing it first
1575 m_textCtrl
->EndEdit( true );
1578 wxGenericTreeItem
*parent
= item
->GetParent();
1580 // don't keep stale pointers around!
1581 if ( IsDescendantOf(item
, m_key_current
) )
1583 // Don't silently change the selection:
1584 // do it properly in idle time, so event
1585 // handlers get called.
1587 // m_key_current = parent;
1588 m_key_current
= NULL
;
1591 // m_select_me records whether we need to select
1592 // a different item, in idle time.
1593 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1595 m_select_me
= parent
;
1598 if ( IsDescendantOf(item
, m_current
) )
1600 // Don't silently change the selection:
1601 // do it properly in idle time, so event
1602 // handlers get called.
1604 // m_current = parent;
1606 m_select_me
= parent
;
1609 // remove the item from the tree
1612 parent
->GetChildren().Remove( item
); // remove by value
1614 else // deleting the root
1616 // nothing will be left in the tree
1620 // and delete all of its children and the item itself now
1621 item
->DeleteChildren(this);
1622 SendDeleteEvent(item
);
1624 if (item
== m_select_me
)
1629 InvalidateBestSize();
1632 void wxGenericTreeCtrl::DeleteAllItems()
1640 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1642 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1644 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1645 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1646 _T("can't expand hidden root") );
1648 if ( !item
->HasPlus() )
1651 if ( item
->IsExpanded() )
1654 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_EXPANDING
, this, item
);
1656 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1658 // cancelled by program
1665 CalculatePositions();
1667 RefreshSubtree(item
);
1674 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1675 GetEventHandler()->ProcessEvent( event
);
1678 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1680 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1681 _T("can't collapse hidden root") );
1683 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1685 if ( !item
->IsExpanded() )
1688 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, this, item
);
1689 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1691 // cancelled by program
1695 ChildrenClosing(item
);
1698 #if 0 // TODO why should items be collapsed recursively?
1699 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1700 size_t count
= children
.GetCount();
1701 for ( size_t n
= 0; n
< count
; n
++ )
1703 Collapse(children
[n
]);
1707 CalculatePositions();
1709 RefreshSubtree(item
);
1711 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1712 GetEventHandler()->ProcessEvent( event
);
1715 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1718 DeleteChildren(item
);
1721 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1723 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1725 if (item
->IsExpanded())
1731 void wxGenericTreeCtrl::Unselect()
1735 m_current
->SetHilight( false );
1736 RefreshLine( m_current
);
1743 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1745 if (item
->IsSelected())
1747 item
->SetHilight(false);
1751 if (item
->HasChildren())
1753 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1754 size_t count
= children
.GetCount();
1755 for ( size_t n
= 0; n
< count
; ++n
)
1757 UnselectAllChildren(children
[n
]);
1762 void wxGenericTreeCtrl::UnselectAll()
1764 wxTreeItemId rootItem
= GetRootItem();
1766 // the tree might not have the root item at all
1769 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1773 // Recursive function !
1774 // To stop we must have crt_item<last_item
1776 // Tag all next children, when no more children,
1777 // Move to parent (not to tag)
1778 // Keep going... if we found last_item, we stop.
1779 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1781 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1783 if (parent
== NULL
) // This is root item
1784 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1786 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1787 int index
= children
.Index(crt_item
);
1788 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1790 size_t count
= children
.GetCount();
1791 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1793 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1796 return TagNextChildren(parent
, last_item
, select
);
1799 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1801 crt_item
->SetHilight(select
);
1802 RefreshLine(crt_item
);
1804 if (crt_item
==last_item
)
1807 if (crt_item
->HasChildren())
1809 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1810 size_t count
= children
.GetCount();
1811 for ( size_t n
= 0; n
< count
; ++n
)
1813 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1821 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1825 // item2 is not necessary after item1
1826 // choice first' and 'last' between item1 and item2
1827 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1828 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1830 bool select
= m_current
->IsSelected();
1832 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1835 TagNextChildren(first
,last
,select
);
1838 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1839 bool unselect_others
,
1840 bool extended_select
)
1842 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1846 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1847 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1849 //wxCHECK_RET( ( (!unselect_others) && is_single),
1850 // wxT("this is a single selection tree") );
1852 // to keep going anyhow !!!
1855 if (item
->IsSelected())
1856 return; // nothing to do
1857 unselect_others
= true;
1858 extended_select
= false;
1860 else if ( unselect_others
&& item
->IsSelected() )
1862 // selection change if there is more than one item currently selected
1863 wxArrayTreeItemIds selected_items
;
1864 if ( GetSelections(selected_items
) == 1 )
1868 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1869 event
.m_itemOld
= m_current
;
1870 // TODO : Here we don't send any selection mode yet !
1872 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1875 wxTreeItemId parent
= GetItemParent( itemId
);
1876 while (parent
.IsOk())
1878 if (!IsExpanded(parent
))
1881 parent
= GetItemParent( parent
);
1885 if (unselect_others
)
1887 if (is_single
) Unselect(); // to speed up thing
1892 if (extended_select
)
1896 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1899 // don't change the mark (m_current)
1900 SelectItemRange(m_current
, item
);
1904 bool select
= true; // the default
1906 // Check if we need to toggle hilight (ctrl mode)
1907 if (!unselect_others
)
1908 select
=!item
->IsSelected();
1910 m_current
= m_key_current
= item
;
1911 m_current
->SetHilight(select
);
1912 RefreshLine( m_current
);
1915 // This can cause idle processing to select the root
1916 // if no item is selected, so it must be after the
1918 EnsureVisible( itemId
);
1920 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1921 GetEventHandler()->ProcessEvent( event
);
1924 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1928 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
1932 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1933 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1935 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1936 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1939 item
->SetHilight(false);
1942 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1943 GetEventHandler()->ProcessEvent( event
);
1947 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1948 wxArrayTreeItemIds
&array
) const
1950 if ( item
->IsSelected() )
1951 array
.Add(wxTreeItemId(item
));
1953 if ( item
->HasChildren() )
1955 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1956 size_t count
= children
.GetCount();
1957 for ( size_t n
= 0; n
< count
; ++n
)
1958 FillArray(children
[n
], array
);
1962 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1965 wxTreeItemId idRoot
= GetRootItem();
1966 if ( idRoot
.IsOk() )
1968 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1970 //else: the tree is empty, so no selections
1972 return array
.GetCount();
1975 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1977 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1979 if (!item
.IsOk()) return;
1981 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1983 // first expand all parent branches
1984 wxGenericTreeItem
*parent
= gitem
->GetParent();
1986 if ( HasFlag(wxTR_HIDE_ROOT
) )
1988 while ( parent
&& parent
!= m_anchor
)
1991 parent
= parent
->GetParent();
1999 parent
= parent
->GetParent();
2003 //if (parent) CalculatePositions();
2008 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
2010 if (!item
.IsOk()) return;
2012 // We have to call this here because the label in
2013 // question might just have been added and no screen
2014 // update taken place.
2016 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2019 DoDirtyProcessing();
2021 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2023 // now scroll to the item
2024 int item_y
= gitem
->GetY();
2028 GetViewStart( &start_x
, &start_y
);
2029 start_y
*= PIXELS_PER_UNIT
;
2033 GetClientSize( &client_w
, &client_h
);
2035 if (item_y
< start_y
+3)
2040 m_anchor
->GetSize( x
, y
, this );
2041 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2042 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2043 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2044 // Item should appear at top
2045 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2047 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2052 m_anchor
->GetSize( x
, y
, this );
2053 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2054 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2055 item_y
+= PIXELS_PER_UNIT
+2;
2056 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2057 // Item should appear at bottom
2058 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
);
2062 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2063 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2065 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2066 wxGenericTreeItem
**item2
)
2068 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2070 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2073 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2075 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2077 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2079 wxCHECK_RET( !s_treeBeingSorted
,
2080 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2082 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2083 if ( children
.GetCount() > 1 )
2087 s_treeBeingSorted
= this;
2088 children
.Sort(tree_ctrl_compare_func
);
2089 s_treeBeingSorted
= NULL
;
2091 //else: don't make the tree dirty as nothing changed
2094 void wxGenericTreeCtrl::CalculateLineHeight()
2096 wxClientDC
dc(this);
2097 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2099 if ( m_imageListNormal
)
2101 // Calculate a m_lineHeight value from the normal Image sizes.
2102 // May be toggle off. Then wxGenericTreeCtrl will spread when
2103 // necessary (which might look ugly).
2104 int n
= m_imageListNormal
->GetImageCount();
2105 for (int i
= 0; i
< n
; i
++)
2107 int width
= 0, height
= 0;
2108 m_imageListNormal
->GetSize(i
, width
, height
);
2109 if (height
> m_lineHeight
) m_lineHeight
= height
;
2113 if ( m_imageListState
)
2115 // Calculate a m_lineHeight value from the state Image sizes.
2116 // May be toggle off. Then wxGenericTreeCtrl will spread when
2117 // necessary (which might look ugly).
2118 int n
= m_imageListState
->GetImageCount();
2119 for (int i
= 0; i
< n
; i
++)
2121 int width
= 0, height
= 0;
2122 m_imageListState
->GetSize(i
, width
, height
);
2123 if (height
> m_lineHeight
) m_lineHeight
= height
;
2127 if (m_imageListButtons
)
2129 // Calculate a m_lineHeight value from the Button image sizes.
2130 // May be toggle off. Then wxGenericTreeCtrl will spread when
2131 // necessary (which might look ugly).
2132 int n
= m_imageListButtons
->GetImageCount();
2133 for (int i
= 0; i
< n
; i
++)
2135 int width
= 0, height
= 0;
2136 m_imageListButtons
->GetSize(i
, width
, height
);
2137 if (height
> m_lineHeight
) m_lineHeight
= height
;
2141 if (m_lineHeight
< 30)
2142 m_lineHeight
+= 2; // at least 2 pixels
2144 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2147 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2149 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2150 m_imageListNormal
= imageList
;
2151 m_ownsImageListNormal
= false;
2153 // Don't do any drawing if we're setting the list to NULL,
2154 // since we may be in the process of deleting the tree control.
2156 CalculateLineHeight();
2159 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2161 if (m_ownsImageListState
) delete m_imageListState
;
2162 m_imageListState
= imageList
;
2163 m_ownsImageListState
= false;
2165 // Don't do any drawing if we're setting the list to NULL,
2166 // since we may be in the process of deleting the tree control.
2168 CalculateLineHeight();
2171 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2173 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2174 m_imageListButtons
= imageList
;
2175 m_ownsImageListButtons
= false;
2177 CalculateLineHeight();
2180 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2182 SetButtonsImageList(imageList
);
2183 m_ownsImageListButtons
= true;
2186 // -----------------------------------------------------------------------------
2188 // -----------------------------------------------------------------------------
2190 void wxGenericTreeCtrl::AdjustMyScrollbars()
2195 m_anchor
->GetSize( x
, y
, this );
2196 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2197 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2198 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2199 int y_pos
= GetScrollPos( wxVERTICAL
);
2200 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2204 SetScrollbars( 0, 0, 0, 0 );
2208 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2210 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2211 return item
->GetHeight();
2213 return m_lineHeight
;
2216 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2218 wxTreeItemAttr
*attr
= item
->GetAttributes();
2219 if ( attr
&& attr
->HasFont() )
2220 dc
.SetFont(attr
->GetFont());
2221 else if (item
->IsBold())
2222 dc
.SetFont(m_boldFont
);
2224 wxCoord text_w
= 0, text_h
= 0;
2225 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2227 int image_h
= 0, image_w
= 0;
2228 int image
= item
->GetCurrentImage();
2229 if ( image
!= NO_IMAGE
)
2231 if ( m_imageListNormal
)
2233 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2234 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2242 int state_h
= 0, state_w
= 0;
2243 int state
= item
->GetState();
2244 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2246 if ( m_imageListState
)
2248 m_imageListState
->GetSize( state
, state_w
, state_h
);
2249 if ( image
!= NO_IMAGE
)
2250 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
2252 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2256 state
= wxTREE_ITEMSTATE_NONE
;
2260 int total_h
= GetLineHeight(item
);
2261 bool drawItemBackground
= false;
2263 if ( item
->IsSelected() )
2265 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2266 drawItemBackground
= true;
2271 if ( attr
&& attr
->HasBackgroundColour() )
2273 drawItemBackground
= true;
2274 colBg
= attr
->GetBackgroundColour();
2278 colBg
= GetBackgroundColour();
2280 dc
.SetBrush(wxBrush(colBg
, wxBRUSHSTYLE_SOLID
));
2283 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2285 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2289 GetVirtualSize(&w
, &h
);
2290 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2291 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2292 dc
.DrawRectangle(rect
);
2294 if (!item
->IsSelected())
2296 dc
.DrawRectangle(rect
);
2300 int flags
= wxCONTROL_SELECTED
;
2302 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2303 && IsControlActive( (ControlRef
)GetHandle() )
2306 flags
|= wxCONTROL_FOCUSED
;
2307 if ((item
== m_current
) && (m_hasFocus
))
2308 flags
|= wxCONTROL_CURRENT
;
2309 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2315 if ( item
->IsSelected() &&
2316 (state
!= wxTREE_ITEMSTATE_NONE
|| image
!= NO_IMAGE
) )
2318 // If it's selected, and there's an state image or normal image,
2319 // then we should take care to leave the area under the image
2320 // painted in the background colour.
2321 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2, item
->GetY() + offset
,
2322 item
->GetWidth() - state_w
- image_w
+ 2, total_h
- offset
);
2323 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2324 dc
.DrawRectangle( rect
);
2329 int flags
= wxCONTROL_SELECTED
;
2331 flags
|= wxCONTROL_FOCUSED
;
2332 if ((item
== m_current
) && (m_hasFocus
))
2333 flags
|= wxCONTROL_CURRENT
;
2334 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2337 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2338 // don't allow backgrounds to be customized. Not drawing the background,
2339 // except for custom item backgrounds, works for both kinds of theme.
2340 else if (drawItemBackground
)
2342 wxRect
rect( item
->GetX()-2, item
->GetY()+offset
,
2343 item
->GetWidth()+2, total_h
-offset
);
2344 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2345 dc
.DrawRectangle( rect
);
2347 if ( attr
&& attr
->HasBackgroundColour() )
2349 dc
.DrawRectangle( rect
);
2356 int flags
= wxCONTROL_SELECTED
;
2358 flags
|= wxCONTROL_FOCUSED
;
2359 if ((item
== m_current
) && (m_hasFocus
))
2360 flags
|= wxCONTROL_CURRENT
;
2361 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2367 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2369 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), state_w
, total_h
);
2370 m_imageListState
->Draw( state
, dc
,
2372 item
->GetY() + ((total_h
> state_h
)?((total_h
-state_h
)/2):0),
2373 wxIMAGELIST_DRAW_TRANSPARENT
);
2374 dc
.DestroyClippingRegion();
2377 if ( image
!= NO_IMAGE
)
2379 dc
.SetClippingRegion( item
->GetX() + state_w
, item
->GetY(), image_w
, total_h
);
2380 m_imageListNormal
->Draw( image
, dc
,
2381 item
->GetX() + state_w
,
2382 item
->GetY() + ((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2383 wxIMAGELIST_DRAW_TRANSPARENT
);
2384 dc
.DestroyClippingRegion();
2387 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
2388 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2389 dc
.DrawText( item
->GetText(),
2390 (wxCoord
)(state_w
+ image_w
+ item
->GetX()),
2391 (wxCoord
)(item
->GetY() + extraH
));
2393 // restore normal font
2394 dc
.SetFont( m_normalFont
);
2397 // Now y stands for the top of the item, whereas it used to stand for middle !
2398 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2400 int x
= level
*m_indent
;
2401 if (!HasFlag(wxTR_HIDE_ROOT
))
2405 else if (level
== 0)
2407 // always expand hidden root
2409 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2410 int count
= children
.GetCount();
2416 PaintLevel(children
[n
], dc
, 1, y
);
2417 } while (++n
< count
);
2419 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2421 // draw line down to last child
2422 origY
+= GetLineHeight(children
[0])>>1;
2423 oldY
+= GetLineHeight(children
[n
-1])>>1;
2424 dc
.DrawLine(3, origY
, 3, oldY
);
2430 item
->SetX(x
+m_spacing
);
2433 int h
= GetLineHeight(item
);
2435 int y_mid
= y_top
+ (h
>>1);
2438 int exposed_x
= dc
.LogicalToDeviceX(0);
2439 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2441 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2445 // don't draw rect outline if we already have the
2446 // background color under Mac
2447 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2448 #endif // !__WXMAC__
2452 if ( item
->IsSelected()
2453 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2454 // On wxMac, if the tree doesn't have the focus we draw an empty
2455 // rectangle, so we want to make sure that the text is visible
2456 // against the normal background, not the highlightbackground, so
2457 // don't use the highlight text colour unless we have the focus.
2458 && m_hasFocus
&& IsControlActive( (ControlRef
)GetHandle() )
2465 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2470 wxTreeItemAttr
*attr
= item
->GetAttributes();
2471 if (attr
&& attr
->HasTextColour())
2472 colText
= attr
->GetTextColour();
2474 colText
= GetForegroundColour();
2478 dc
.SetTextForeground(colText
);
2482 PaintItem(item
, dc
);
2484 if (HasFlag(wxTR_ROW_LINES
))
2486 // if the background colour is white, choose a
2487 // contrasting color for the lines
2488 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2489 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2490 dc
.DrawLine(0, y_top
, 10000, y_top
);
2491 dc
.DrawLine(0, y
, 10000, y
);
2494 // restore DC objects
2495 dc
.SetBrush(*wxWHITE_BRUSH
);
2496 dc
.SetPen(m_dottedPen
);
2497 dc
.SetTextForeground(*wxBLACK
);
2499 if ( !HasFlag(wxTR_NO_LINES
) )
2501 // draw the horizontal line here
2503 if (x
> (signed)m_indent
)
2504 x_start
-= m_indent
;
2505 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2507 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2510 // should the item show a button?
2511 if ( item
->HasPlus() && HasButtons() )
2513 if ( m_imageListButtons
)
2515 // draw the image button here
2518 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2519 : wxTreeItemIcon_Normal
;
2520 if ( item
->IsSelected() )
2521 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2523 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2524 int xx
= x
- image_w
/2;
2525 int yy
= y_mid
- image_h
/2;
2527 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2528 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2529 wxIMAGELIST_DRAW_TRANSPARENT
);
2531 else // no custom buttons
2533 static const int wImage
= 9;
2534 static const int hImage
= 9;
2537 if (item
->IsExpanded())
2538 flag
|= wxCONTROL_EXPANDED
;
2539 if (item
== m_underMouse
)
2540 flag
|= wxCONTROL_CURRENT
;
2542 wxRendererNative::Get().DrawTreeItemButton
2546 wxRect(x
- wImage
/2,
2555 if (item
->IsExpanded())
2557 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2558 int count
= children
.GetCount();
2565 PaintLevel(children
[n
], dc
, level
, y
);
2566 } while (++n
< count
);
2568 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2570 // draw line down to last child
2571 oldY
+= GetLineHeight(children
[n
-1])>>1;
2572 if (HasButtons()) y_mid
+= 5;
2574 // Only draw the portion of the line that is visible, in case it is huge
2575 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2576 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2577 yOrigin
= abs(yOrigin
);
2578 GetClientSize(&width
, &height
);
2580 // Move end points to the begining/end of the view?
2581 if (y_mid
< yOrigin
)
2583 if (oldY
> yOrigin
+ height
)
2584 oldY
= yOrigin
+ height
;
2586 // after the adjustments if y_mid is larger than oldY then the line
2587 // isn't visible at all so don't draw anything
2589 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2595 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2599 if ( item
->HasPlus() )
2601 // it's a folder, indicate it by a border
2606 // draw a line under the drop target because the item will be
2608 DrawLine(item
, !m_dropEffectAboveItem
);
2611 SetCursor(wxCURSOR_BULLSEYE
);
2616 SetCursor(wxCURSOR_NO_ENTRY
);
2620 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2622 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2624 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2626 wxClientDC
dc(this);
2628 dc
.SetLogicalFunction(wxINVERT
);
2629 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2631 int w
= i
->GetWidth() + 2;
2632 int h
= GetLineHeight(i
) + 2;
2634 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2637 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2639 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2641 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2643 wxClientDC
dc(this);
2645 dc
.SetLogicalFunction(wxINVERT
);
2651 y
+= GetLineHeight(i
) - 1;
2654 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2657 // -----------------------------------------------------------------------------
2658 // wxWidgets callbacks
2659 // -----------------------------------------------------------------------------
2661 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
2664 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
2665 RefreshLine( m_current
);
2671 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2679 dc
.SetFont( m_normalFont
);
2680 dc
.SetPen( m_dottedPen
);
2682 // this is now done dynamically
2683 //if(GetImageList() == NULL)
2684 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2687 PaintLevel( m_anchor
, dc
, 0, y
);
2690 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2699 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2708 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2710 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2711 te
.m_evtKey
= event
;
2712 if ( GetEventHandler()->ProcessEvent( te
) )
2714 // intercepted by the user code
2718 if ( (m_current
== 0) || (m_key_current
== 0) )
2724 // how should the selection work for this event?
2725 bool is_multiple
, extended_select
, unselect_others
;
2726 EventFlagsToSelType(GetWindowStyleFlag(),
2729 is_multiple
, extended_select
, unselect_others
);
2731 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2733 if (event
.GetKeyCode() == WXK_RIGHT
)
2734 event
.m_keyCode
= WXK_LEFT
;
2735 else if (event
.GetKeyCode() == WXK_LEFT
)
2736 event
.m_keyCode
= WXK_RIGHT
;
2741 // * : Expand all/Collapse all
2742 // ' ' | return : activate
2743 // up : go up (not last children!)
2745 // left : go to parent
2746 // right : open if parent and go next
2747 // home : go to root
2748 // end : go to last item without opening parents
2749 // alnum : start or continue searching for the item with this prefix
2750 int keyCode
= event
.GetKeyCode();
2755 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2763 if ( !IsExpanded(m_current
) )
2766 ExpandAllChildren(m_current
);
2769 //else: fall through to Collapse() it
2773 if (IsExpanded(m_current
))
2775 Collapse(m_current
);
2781 // Use the item's bounding rectangle to determine position for the event
2783 GetBoundingRect(m_current
, ItemRect
, true);
2785 wxTreeEvent
eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU
, this, m_current
);
2786 // Use the left edge, vertical middle
2787 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
2788 ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2789 GetEventHandler()->ProcessEvent( eventMenu
);
2795 if ( !event
.HasModifiers() )
2797 wxTreeEvent
eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, m_current
);
2798 GetEventHandler()->ProcessEvent( eventAct
);
2801 // in any case, also generate the normal key event for this key,
2802 // even if we generated the ACTIVATED event above: this is what
2803 // wxMSW does and it makes sense because you might not want to
2804 // process ACTIVATED event at all and handle Space and Return
2805 // directly (and differently) which would be impossible otherwise
2809 // up goes to the previous sibling or to the last
2810 // of its children if it's expanded
2813 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2816 prev
= GetItemParent( m_key_current
);
2817 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2819 break; // don't go to root if it is hidden
2823 wxTreeItemIdValue cookie
;
2824 wxTreeItemId current
= m_key_current
;
2825 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2826 if (current
== GetFirstChild( prev
, cookie
))
2828 // otherwise we return to where we came from
2829 DoSelectItem( prev
, unselect_others
, extended_select
);
2830 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2837 while ( IsExpanded(prev
) && HasChildren(prev
) )
2839 wxTreeItemId child
= GetLastChild(prev
);
2846 DoSelectItem( prev
, unselect_others
, extended_select
);
2847 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2852 // left arrow goes to the parent
2855 wxTreeItemId prev
= GetItemParent( m_current
);
2856 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2858 // don't go to root if it is hidden
2859 prev
= GetPrevSibling( m_current
);
2863 DoSelectItem( prev
, unselect_others
, extended_select
);
2869 // this works the same as the down arrow except that we
2870 // also expand the item if it wasn't expanded yet
2871 if (m_current
!= GetRootItem().m_pItem
|| !HasFlag(wxTR_HIDE_ROOT
))
2873 //else: don't try to expand hidden root item (which can be the
2874 // current one when the tree is empty)
2880 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2882 wxTreeItemIdValue cookie
;
2883 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2887 DoSelectItem( child
, unselect_others
, extended_select
);
2888 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2892 wxTreeItemId next
= GetNextSibling( m_key_current
);
2895 wxTreeItemId current
= m_key_current
;
2896 while (current
.IsOk() && !next
)
2898 current
= GetItemParent( current
);
2899 if (current
) next
= GetNextSibling( current
);
2904 DoSelectItem( next
, unselect_others
, extended_select
);
2905 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2911 // <End> selects the last visible tree item
2914 wxTreeItemId last
= GetRootItem();
2916 while ( last
.IsOk() && IsExpanded(last
) )
2918 wxTreeItemId lastChild
= GetLastChild(last
);
2920 // it may happen if the item was expanded but then all of
2921 // its children have been deleted - so IsExpanded() returned
2922 // true, but GetLastChild() returned invalid item
2931 DoSelectItem( last
, unselect_others
, extended_select
);
2936 // <Home> selects the root item
2939 wxTreeItemId prev
= GetRootItem();
2943 if ( HasFlag(wxTR_HIDE_ROOT
) )
2945 wxTreeItemIdValue cookie
;
2946 prev
= GetFirstChild(prev
, cookie
);
2951 DoSelectItem( prev
, unselect_others
, extended_select
);
2956 // do not use wxIsalnum() here
2957 if ( !event
.HasModifiers() &&
2958 ((keyCode
>= '0' && keyCode
<= '9') ||
2959 (keyCode
>= 'a' && keyCode
<= 'z') ||
2960 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2962 // find the next item starting with the given prefix
2963 wxChar ch
= (wxChar
)keyCode
;
2965 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2976 // also start the timer to reset the current prefix if the user
2977 // doesn't press any more alnum keys soon -- we wouldn't want
2978 // to use this prefix for a new item search
2981 m_findTimer
= new wxTreeFindTimer(this);
2984 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2994 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2999 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
3000 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
3001 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
3002 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
3003 if (flags
) return wxTreeItemId();
3005 if (m_anchor
== NULL
)
3007 flags
= wxTREE_HITTEST_NOWHERE
;
3008 return wxTreeItemId();
3011 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
3015 flags
= wxTREE_HITTEST_NOWHERE
;
3016 return wxTreeItemId();
3021 // get the bounding rectangle of the item (or of its label only)
3022 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
3024 bool textOnly
) const
3026 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
3028 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
3033 rect
.width
= i
->GetWidth();
3035 if ( m_imageListNormal
)
3037 int image_w
, image_h
;
3038 m_imageListNormal
->GetSize( 0, image_w
, image_h
);
3039 rect
.width
+= image_w
+ MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3042 else // the entire line
3045 rect
.width
= GetClientSize().x
;
3049 rect
.height
= GetLineHeight(i
);
3051 // we have to return the logical coordinates, not physical ones
3052 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
3057 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
3058 wxClassInfo
* WXUNUSED(textCtrlClass
))
3060 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
3062 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
3064 wxTreeEvent
te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
3065 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
3071 // We have to call this here because the label in
3072 // question might just have been added and no screen
3073 // update taken place.
3075 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3078 DoDirtyProcessing();
3081 // TODO: use textCtrlClass here to create the control of correct class
3082 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
3084 m_textCtrl
->SetFocus();
3089 // returns a pointer to the text edit control if the item is being
3090 // edited, NULL otherwise (it's assumed that no more than one item may
3091 // be edited simultaneously)
3092 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3097 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3098 bool discardChanges
)
3100 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
3102 m_textCtrl
->EndEdit(discardChanges
);
3105 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3106 const wxString
& value
)
3108 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3110 le
.m_editCancelled
= false;
3112 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3115 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3117 // let owner know that the edit was cancelled
3118 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3119 le
.m_label
= wxEmptyString
;
3120 le
.m_editCancelled
= true;
3122 GetEventHandler()->ProcessEvent( le
);
3125 void wxGenericTreeCtrl::OnRenameTimer()
3127 EditLabel( m_current
);
3130 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3132 if ( !m_anchor
)return;
3134 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3136 // Is the mouse over a tree item button?
3138 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3139 wxGenericTreeItem
*underMouse
= thisItem
;
3141 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3142 #endif // wxUSE_TOOLTIPS
3145 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3146 (!event
.LeftIsDown()) &&
3148 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3156 if (underMouse
!= m_underMouse
)
3160 // unhighlight old item
3161 wxGenericTreeItem
*tmp
= m_underMouse
;
3162 m_underMouse
= NULL
;
3166 m_underMouse
= underMouse
;
3168 RefreshLine( m_underMouse
);
3172 // Determines what item we are hovering over and need a tooltip for
3173 wxTreeItemId hoverItem
= thisItem
;
3175 // We do not want a tooltip if we are dragging, or if the rename timer is running
3176 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3178 // Ask the tree control what tooltip (if any) should be shown
3179 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3181 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3183 SetToolTip(hevent
.m_label
);
3188 // we process left mouse up event (enables in-place edit), middle/right down
3189 // (pass to the user code), left dbl click (activate item) and
3190 // dragging/moving events for items drag-and-drop
3191 if ( !(event
.LeftDown() ||
3193 event
.MiddleDown() ||
3194 event
.RightDown() ||
3195 event
.LeftDClick() ||
3197 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3206 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3208 if ( event
.Dragging() && !m_isDragging
)
3210 if (m_dragCount
== 0)
3215 if (m_dragCount
!= 3)
3217 // wait until user drags a bit further...
3221 wxEventType command
= event
.RightIsDown()
3222 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3223 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3225 wxTreeEvent
nevent(command
, this, m_current
);
3226 nevent
.SetPoint(CalcScrolledPosition(pt
));
3228 // by default the dragging is not supported, the user code must
3229 // explicitly allow the event for it to take place
3232 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3234 // we're going to drag this item
3235 m_isDragging
= true;
3237 // remember the old cursor because we will change it while
3239 m_oldCursor
= m_cursor
;
3241 // in a single selection control, hide the selection temporarily
3242 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3244 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3246 if ( m_oldSelection
)
3248 m_oldSelection
->SetHilight(false);
3249 RefreshLine(m_oldSelection
);
3256 else if ( event
.Dragging() )
3258 if ( item
!= m_dropTarget
)
3260 // unhighlight the previous drop target
3261 DrawDropEffect(m_dropTarget
);
3263 m_dropTarget
= item
;
3265 // highlight the current drop target if any
3266 DrawDropEffect(m_dropTarget
);
3268 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3275 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3279 // erase the highlighting
3280 DrawDropEffect(m_dropTarget
);
3282 if ( m_oldSelection
)
3284 m_oldSelection
->SetHilight(true);
3285 RefreshLine(m_oldSelection
);
3286 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3289 // generate the drag end event
3290 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, this, item
);
3292 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3294 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3296 m_isDragging
= false;
3297 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3299 SetCursor(m_oldCursor
);
3301 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3309 // If we got to this point, we are not dragging or moving the mouse.
3310 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3311 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3312 // We skip even if we didn't hit an item because we still should
3313 // restore focus to the tree control even if we didn't exactly hit an item.
3314 if ( event
.LeftDown() )
3319 // here we process only the messages which happen on tree items
3323 if (item
== NULL
) return; /* we hit the blank area */
3325 if ( event
.RightDown() )
3327 // If the item is already selected, do not update the selection.
3328 // Multi-selections should not be cleared if a selected item is clicked.
3329 if (!IsSelected(item
))
3331 DoSelectItem(item
, true, false);
3334 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, this, item
);
3335 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3336 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3338 // Consistent with MSW (for now), send the ITEM_MENU *after*
3339 // the RIGHT_CLICK event. TODO: This behavior may change.
3340 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
3341 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3342 GetEventHandler()->ProcessEvent(nevent2
);
3344 else if ( event
.MiddleDown() )
3346 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
, this, item
);
3347 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3348 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3350 else if ( event
.LeftUp() )
3352 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3354 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, this, item
);
3355 GetEventHandler()->ProcessEvent(nevent
);
3358 // this facilitates multiple-item drag-and-drop
3360 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3362 wxArrayTreeItemIds selections
;
3363 size_t count
= GetSelections(selections
);
3369 DoSelectItem(item
, true, false);
3375 if ( (item
== m_current
) &&
3376 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3377 HasFlag(wxTR_EDIT_LABELS
) )
3379 if ( m_renameTimer
)
3381 if ( m_renameTimer
->IsRunning() )
3382 m_renameTimer
->Stop();
3386 m_renameTimer
= new wxTreeRenameTimer( this );
3389 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3392 m_lastOnSame
= false;
3395 else // !RightDown() && !MiddleDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3397 if ( event
.LeftDown() )
3399 m_lastOnSame
= item
== m_current
;
3402 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3404 // only toggle the item for a single click, double click on
3405 // the button doesn't do anything (it toggles the item twice)
3406 if ( event
.LeftDown() )
3411 // don't select the item if the button was clicked
3416 // clear the previously selected items, if the
3417 // user clicked outside of the present selection.
3418 // otherwise, perform the deselection on mouse-up.
3419 // this allows multiple drag and drop to work.
3420 // but if Cmd is down, toggle selection of the clicked item
3421 if (!IsSelected(item
) || event
.CmdDown())
3423 // how should the selection work for this event?
3424 bool is_multiple
, extended_select
, unselect_others
;
3425 EventFlagsToSelType(GetWindowStyleFlag(),
3428 is_multiple
, extended_select
, unselect_others
);
3430 DoSelectItem(item
, unselect_others
, extended_select
);
3434 // For some reason, Windows isn't recognizing a left double-click,
3435 // so we need to simulate it here. Allow 200 milliseconds for now.
3436 if ( event
.LeftDClick() )
3438 // double clicking should not start editing the item label
3439 if ( m_renameTimer
)
3440 m_renameTimer
->Stop();
3442 m_lastOnSame
= false;
3444 // send activate event first
3445 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, item
);
3446 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3447 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3449 // if the user code didn't process the activate event,
3450 // handle it ourselves by toggling the item when it is
3452 if ( item
->HasPlus() )
3462 void wxGenericTreeCtrl::OnInternalIdle()
3464 wxWindow::OnInternalIdle();
3466 // Check if we need to select the root item
3467 // because nothing else has been selected.
3468 // Delaying it means that we can invoke event handlers
3469 // as required, when a first item is selected.
3470 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3473 SelectItem(m_select_me
);
3474 else if (GetRootItem().IsOk())
3475 SelectItem(GetRootItem());
3478 // after all changes have been done to the tree control,
3479 // actually redraw the tree when everything is over
3481 DoDirtyProcessing();
3484 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3489 wxTreeItemAttr
*attr
= item
->GetAttributes();
3490 if ( attr
&& attr
->HasFont() )
3491 dc
.SetFont(attr
->GetFont());
3492 else if ( item
->IsBold() )
3493 dc
.SetFont(m_boldFont
);
3495 dc
.SetFont(m_normalFont
);
3497 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3500 // restore normal font
3501 dc
.SetFont( m_normalFont
);
3505 int image
= item
->GetCurrentImage();
3506 if ( image
!= NO_IMAGE
)
3508 if ( m_imageListNormal
)
3510 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3511 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3515 int state_h
= 0, state_w
= 0;
3516 int state
= item
->GetState();
3517 if ( state
!= wxTREE_ITEMSTATE_NONE
)
3519 if ( m_imageListState
)
3521 m_imageListState
->GetSize( state
, state_w
, state_h
);
3522 if ( image
!= NO_IMAGE
)
3523 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
3525 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3529 state
= wxTREE_ITEMSTATE_NONE
;
3533 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3536 total_h
+= 2; // at least 2 pixels
3538 total_h
+= total_h
/10; // otherwise 10% extra spacing
3540 item
->SetHeight(total_h
);
3541 if (total_h
>m_lineHeight
)
3542 m_lineHeight
=total_h
;
3544 item
->SetWidth(state_w
+ image_w
+ text_w
+ 2);
3547 // -----------------------------------------------------------------------------
3548 // for developper : y is now the top of the level
3549 // not the middle of it !
3550 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3552 int x
= level
*m_indent
;
3553 if (!HasFlag(wxTR_HIDE_ROOT
))
3557 else if (level
== 0)
3559 // a hidden root is not evaluated, but its
3560 // children are always calculated
3564 CalculateSize( item
, dc
);
3567 item
->SetX( x
+m_spacing
);
3569 y
+= GetLineHeight(item
);
3571 if ( !item
->IsExpanded() )
3573 // we don't need to calculate collapsed branches
3578 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3579 size_t n
, count
= children
.GetCount();
3581 for (n
= 0; n
< count
; ++n
)
3582 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3585 void wxGenericTreeCtrl::CalculatePositions()
3587 if ( !m_anchor
) return;
3589 wxClientDC
dc(this);
3592 dc
.SetFont( m_normalFont
);
3594 dc
.SetPen( m_dottedPen
);
3595 //if(GetImageList() == NULL)
3596 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3599 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3602 void wxGenericTreeCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
3605 wxTreeCtrlBase::Refresh(eraseBackground
, rect
);
3608 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3610 if (m_dirty
|| IsFrozen() )
3613 wxSize client
= GetClientSize();
3616 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3617 rect
.width
= client
.x
;
3618 rect
.height
= client
.y
;
3620 Refresh(true, &rect
);
3622 AdjustMyScrollbars();
3625 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3627 if (m_dirty
|| IsFrozen() )
3631 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3632 rect
.width
= GetClientSize().x
;
3633 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3635 Refresh(true, &rect
);
3638 void wxGenericTreeCtrl::RefreshSelected()
3643 // TODO: this is awfully inefficient, we should keep the list of all
3644 // selected items internally, should be much faster
3646 RefreshSelectedUnder(m_anchor
);
3649 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3654 if ( item
->IsSelected() )
3657 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3658 size_t count
= children
.GetCount();
3659 for ( size_t n
= 0; n
< count
; n
++ )
3661 RefreshSelectedUnder(children
[n
]);
3665 void wxGenericTreeCtrl::DoThaw()
3667 wxTreeCtrlBase::DoThaw();
3670 DoDirtyProcessing();
3675 // ----------------------------------------------------------------------------
3676 // changing colours: we need to refresh the tree control
3677 // ----------------------------------------------------------------------------
3679 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3681 if ( !wxWindow::SetBackgroundColour(colour
) )
3689 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3691 if ( !wxWindow::SetForegroundColour(colour
) )
3699 // Process the tooltip event, to speed up event processing.
3700 // Doesn't actually get a tooltip.
3701 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3707 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3708 // be removed, as well as the #else case below.
3709 #define _USE_VISATTR 0
3714 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3716 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3720 // Use the same color scheme as wxListBox
3721 return wxListBox::GetClassDefaultAttributes(variant
);
3723 wxVisualAttributes attr
;
3724 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3725 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3726 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3731 void wxGenericTreeCtrl::DoDirtyProcessing()
3738 CalculatePositions();
3740 AdjustMyScrollbars();
3743 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
3745 // make sure all positions are calculated as normally this only done during
3746 // idle time but we need them for base class DoGetBestSize() to return the
3748 wxConstCast(this, wxGenericTreeCtrl
)->CalculatePositions();
3750 wxSize size
= wxTreeCtrlBase::DoGetBestSize();
3752 // there seems to be an implicit extra border around the items, although
3753 // I'm not really sure where does it come from -- but without this, the
3754 // scrollbars appear in a tree with default/best size
3757 // and the border has to be rounded up to a multiple of PIXELS_PER_UNIT or
3758 // scrollbars still appear
3759 const wxSize
& borderSize
= GetWindowBorderSize();
3761 int dx
= (size
.x
- borderSize
.x
) % PIXELS_PER_UNIT
;
3763 size
.x
+= PIXELS_PER_UNIT
- dx
;
3764 int dy
= (size
.y
- borderSize
.y
) % PIXELS_PER_UNIT
;
3766 size
.y
+= PIXELS_PER_UNIT
- dy
;
3768 // we need to update the cache too as the base class cached its own value
3769 CacheBestSize(size
);
3774 #endif // wxUSE_TREECTRL