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/mac/private.h"
48 // -----------------------------------------------------------------------------
50 // -----------------------------------------------------------------------------
52 class WXDLLIMPEXP_FWD_CORE wxGenericTreeItem
;
54 WX_DEFINE_EXPORTED_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 image and the item text
65 static const int MARGIN_BETWEEN_IMAGE_AND_TEXT
= 4;
67 // -----------------------------------------------------------------------------
69 // -----------------------------------------------------------------------------
71 // timer used for enabling in-place edit
72 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
75 // start editing the current item after half a second (if the mouse hasn't
76 // been clicked/moved)
79 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
81 virtual void Notify();
84 wxGenericTreeCtrl
*m_owner
;
86 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
89 // control used for in-place edit
90 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
93 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
95 void EndEdit( bool discardChanges
);
97 const wxGenericTreeItem
* item() const { return m_itemEdited
; }
100 void OnChar( wxKeyEvent
&event
);
101 void OnKeyUp( wxKeyEvent
&event
);
102 void OnKillFocus( wxFocusEvent
&event
);
104 bool AcceptChanges();
105 void Finish( bool setfocus
);
108 wxGenericTreeCtrl
*m_owner
;
109 wxGenericTreeItem
*m_itemEdited
;
110 wxString m_startValue
;
111 bool m_aboutToFinish
;
113 DECLARE_EVENT_TABLE()
114 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
117 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
118 // for a sufficiently long time
119 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
122 // reset the current prefix after half a second of inactivity
123 enum { DELAY
= 500 };
125 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
127 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
130 wxGenericTreeCtrl
*m_owner
;
132 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
136 class WXDLLEXPORT wxGenericTreeItem
140 wxGenericTreeItem() { m_data
= NULL
; }
141 wxGenericTreeItem( wxGenericTreeItem
*parent
,
142 const wxString
& text
,
145 wxTreeItemData
*data
);
147 ~wxGenericTreeItem();
150 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
152 const wxString
& GetText() const { return m_text
; }
153 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
154 { return m_images
[which
]; }
155 wxTreeItemData
*GetData() const { return m_data
; }
157 // returns the current image for the item (depending on its
158 // selected/expanded/whatever state)
159 int GetCurrentImage() const;
161 void SetText( const wxString
&text
);
162 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
163 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
165 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
167 void SetBold(bool bold
) { m_isBold
= bold
; }
169 int GetX() const { return m_x
; }
170 int GetY() const { return m_y
; }
172 void SetX(int x
) { m_x
= x
; }
173 void SetY(int y
) { m_y
= y
; }
175 int GetHeight() const { return m_height
; }
176 int GetWidth() const { return m_width
; }
178 void SetHeight(int h
) { m_height
= h
; }
179 void SetWidth(int w
) { m_width
= w
; }
181 wxGenericTreeItem
*GetParent() const { return m_parent
; }
185 // deletes all children notifying the treectrl about it
186 void DeleteChildren(wxGenericTreeCtrl
*tree
);
188 // get count of all children (and grand children if 'recursively')
189 size_t GetChildrenCount(bool recursively
= true) const;
191 void Insert(wxGenericTreeItem
*child
, size_t index
)
192 { m_children
.Insert(child
, index
); }
194 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
196 // return the item at given position (or NULL if no item), onButton is
197 // true if the point belongs to the item's button, otherwise it lies
198 // on the item's label
199 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
200 const wxGenericTreeCtrl
*,
204 void Expand() { m_isCollapsed
= false; }
205 void Collapse() { m_isCollapsed
= true; }
207 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
210 bool HasChildren() const { return !m_children
.IsEmpty(); }
211 bool IsSelected() const { return m_hasHilight
!= 0; }
212 bool IsExpanded() const { return !m_isCollapsed
; }
213 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
214 bool IsBold() const { return m_isBold
!= 0; }
217 // get them - may be NULL
218 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
219 // get them ensuring that the pointer is not NULL
220 wxTreeItemAttr
& Attr()
224 m_attr
= new wxTreeItemAttr
;
230 void SetAttributes(wxTreeItemAttr
*attr
)
232 if ( m_ownsAttr
) delete m_attr
;
236 // set them and delete when done
237 void AssignAttributes(wxTreeItemAttr
*attr
)
244 // since there can be very many of these, we save size by chosing
245 // the smallest representation for the elements and by ordering
246 // the members to avoid padding.
247 wxString m_text
; // label to be rendered for item
249 wxTreeItemData
*m_data
; // user-provided data
251 wxArrayGenericTreeItems m_children
; // list of children
252 wxGenericTreeItem
*m_parent
; // parent of this item
254 wxTreeItemAttr
*m_attr
; // attributes???
256 // tree ctrl images for the normal, selected, expanded and
257 // expanded+selected states
258 int m_images
[wxTreeItemIcon_Max
];
260 wxCoord m_x
; // (virtual) offset from top
261 wxCoord m_y
; // (virtual) offset from left
262 int m_width
; // width of this item
263 int m_height
; // height of this item
265 // use bitfields to save size
266 unsigned int m_isCollapsed
:1;
267 unsigned int m_hasHilight
:1; // same as focused
268 unsigned int m_hasPlus
:1; // used for item which doesn't have
269 // children but has a [+] button
270 unsigned int m_isBold
:1; // render the label in bold font
271 unsigned int m_ownsAttr
:1; // delete attribute when done
273 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
276 // =============================================================================
278 // =============================================================================
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
284 // translate the key or mouse event flags to the type of selection we're
286 static void EventFlagsToSelType(long style
,
290 bool &extended_select
,
291 bool &unselect_others
)
293 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
294 extended_select
= shiftDown
&& is_multiple
;
295 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
298 // check if the given item is under another one
299 static bool IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
303 if ( item
== parent
)
305 // item is a descendant of parent
309 item
= item
->GetParent();
315 // -----------------------------------------------------------------------------
316 // wxTreeRenameTimer (internal)
317 // -----------------------------------------------------------------------------
319 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
324 void wxTreeRenameTimer::Notify()
326 m_owner
->OnRenameTimer();
329 //-----------------------------------------------------------------------------
330 // wxTreeTextCtrl (internal)
331 //-----------------------------------------------------------------------------
333 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
334 EVT_CHAR (wxTreeTextCtrl::OnChar
)
335 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
336 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
339 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
340 wxGenericTreeItem
*item
)
341 : m_itemEdited(item
), m_startValue(item
->GetText())
344 m_aboutToFinish
= false;
346 int w
= m_itemEdited
->GetWidth(),
347 h
= m_itemEdited
->GetHeight();
350 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
355 int image
= item
->GetCurrentImage();
356 if ( image
!= NO_IMAGE
)
358 if ( m_owner
->m_imageListNormal
)
360 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
361 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
365 wxFAIL_MSG(_T("you must create an image list to use images!"));
369 // FIXME: what are all these hardcoded 4, 8 and 11s really?
373 wxSize bs
= DoGetBestSize() ;
374 // edit control height
377 int diff
= h
- ( bs
.y
- 8 ) ;
383 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
384 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
387 void wxTreeTextCtrl::EndEdit(bool discardChanges
)
389 m_aboutToFinish
= true;
391 if ( discardChanges
)
393 m_owner
->OnRenameCancelled(m_itemEdited
);
399 // Notify the owner about the changes
402 // Even if vetoed, close the control (consistent with MSW)
407 bool wxTreeTextCtrl::AcceptChanges()
409 const wxString value
= GetValue();
411 if ( value
== m_startValue
)
413 // nothing changed, always accept
414 // when an item remains unchanged, the owner
415 // needs to be notified that the user decided
416 // not to change the tree item label, and that
417 // the edit has been cancelled
419 m_owner
->OnRenameCancelled(m_itemEdited
);
423 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
425 // vetoed by the user
429 // accepted, do rename the item
430 m_owner
->SetItemText(m_itemEdited
, value
);
435 void wxTreeTextCtrl::Finish( bool setfocus
)
437 m_owner
->ResetTextControl();
439 wxPendingDelete
.Append(this);
445 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
447 switch ( event
.m_keyCode
)
462 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
464 if ( !m_aboutToFinish
)
466 // auto-grow the textctrl:
467 wxSize parentSize
= m_owner
->GetSize();
468 wxPoint myPos
= GetPosition();
469 wxSize mySize
= GetSize();
471 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
472 if (myPos
.x
+ sx
> parentSize
.x
)
473 sx
= parentSize
.x
- myPos
.x
;
476 SetSize(sx
, wxDefaultCoord
);
482 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
484 if ( !m_aboutToFinish
)
486 if ( !AcceptChanges() )
487 m_owner
->OnRenameCancelled( m_itemEdited
);
492 // We should let the native text control handle focus, too.
496 // -----------------------------------------------------------------------------
498 // -----------------------------------------------------------------------------
500 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
501 const wxString
& text
,
502 int image
, int selImage
,
503 wxTreeItemData
*data
)
506 m_images
[wxTreeItemIcon_Normal
] = image
;
507 m_images
[wxTreeItemIcon_Selected
] = selImage
;
508 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
509 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
514 m_isCollapsed
= true;
515 m_hasHilight
= false;
521 m_attr
= (wxTreeItemAttr
*)NULL
;
524 // We don't know the height here yet.
529 wxGenericTreeItem::~wxGenericTreeItem()
533 if (m_ownsAttr
) delete m_attr
;
535 wxASSERT_MSG( m_children
.IsEmpty(),
536 wxT("please call DeleteChildren() before deleting the item") );
539 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
541 size_t count
= m_children
.GetCount();
542 for ( size_t n
= 0; n
< count
; n
++ )
544 wxGenericTreeItem
*child
= m_children
[n
];
545 tree
->SendDeleteEvent(child
);
547 child
->DeleteChildren(tree
);
548 if ( child
== tree
->m_select_me
)
549 tree
->m_select_me
= NULL
;
556 void wxGenericTreeItem::SetText( const wxString
&text
)
561 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
563 size_t count
= m_children
.GetCount();
567 size_t total
= count
;
568 for (size_t n
= 0; n
< count
; ++n
)
570 total
+= m_children
[n
]->GetChildrenCount();
576 void wxGenericTreeItem::GetSize( int &x
, int &y
,
577 const wxGenericTreeCtrl
*theButton
)
579 int bottomY
=m_y
+theButton
->GetLineHeight(this);
580 if ( y
< bottomY
) y
= bottomY
;
581 int width
= m_x
+ m_width
;
582 if ( x
< width
) x
= width
;
586 size_t count
= m_children
.GetCount();
587 for ( size_t n
= 0; n
< count
; ++n
)
589 m_children
[n
]->GetSize( x
, y
, theButton
);
594 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
595 const wxGenericTreeCtrl
*theCtrl
,
599 // for a hidden root node, don't evaluate it, but do evaluate children
600 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
603 int h
= theCtrl
->GetLineHeight(this);
604 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
606 int y_mid
= m_y
+ h
/2;
607 if (point
.y
< y_mid
)
608 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
610 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
612 int xCross
= m_x
- theCtrl
->GetSpacing();
614 // according to the drawing code the triangels are drawn
615 // at -4 , -4 from the position up to +10/+10 max
616 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
617 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
618 HasPlus() && theCtrl
->HasButtons() )
620 // 5 is the size of the plus sign
621 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
622 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
623 HasPlus() && theCtrl
->HasButtons() )
626 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
630 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
635 // assuming every image (normal and selected) has the same size!
636 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
637 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
640 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
641 flags
|= wxTREE_HITTEST_ONITEMICON
;
643 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
649 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
650 if (point
.x
> m_x
+m_width
)
651 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
656 // if children are expanded, fall through to evaluate them
657 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
661 size_t count
= m_children
.GetCount();
662 for ( size_t n
= 0; n
< count
; n
++ )
664 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
672 return (wxGenericTreeItem
*) NULL
;
675 int wxGenericTreeItem::GetCurrentImage() const
677 int image
= NO_IMAGE
;
682 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
685 if ( image
== NO_IMAGE
)
687 // we usually fall back to the normal item, but try just the
688 // expanded one (and not selected) first in this case
689 image
= GetImage(wxTreeItemIcon_Expanded
);
695 image
= GetImage(wxTreeItemIcon_Selected
);
698 // maybe it doesn't have the specific image we want,
699 // try the default one instead
700 if ( image
== NO_IMAGE
) image
= GetImage();
705 // -----------------------------------------------------------------------------
706 // wxGenericTreeCtrl implementation
707 // -----------------------------------------------------------------------------
709 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxControl
)
711 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
, wxTreeCtrlBase
)
712 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
713 EVT_SIZE (wxGenericTreeCtrl::OnSize
)
714 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
715 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
716 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
717 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
718 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
721 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
723 * wxTreeCtrl has to be a real class or we have problems with
724 * the run-time information.
727 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
730 // -----------------------------------------------------------------------------
731 // construction/destruction
732 // -----------------------------------------------------------------------------
734 void wxGenericTreeCtrl::Init()
739 m_select_me
= (wxGenericTreeItem
*) NULL
;
747 m_hilightBrush
= new wxBrush
749 wxSystemSettings::GetColour
751 wxSYS_COLOUR_HIGHLIGHT
756 m_hilightUnfocusedBrush
= new wxBrush
758 wxSystemSettings::GetColour
760 wxSYS_COLOUR_BTNSHADOW
765 m_imageListButtons
= NULL
;
766 m_ownsImageListButtons
= false;
769 m_isDragging
= false;
770 m_dropTarget
= m_oldSelection
= NULL
;
774 m_renameTimer
= NULL
;
778 m_dropEffectAboveItem
= false;
780 m_lastOnSame
= false;
783 m_normalFont
.MacCreateFromThemeFont( kThemeViewsFont
) ;
785 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
787 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
788 m_normalFont
.GetFamily(),
789 m_normalFont
.GetStyle(),
791 m_normalFont
.GetUnderlined(),
792 m_normalFont
.GetFaceName(),
793 m_normalFont
.GetEncoding());
796 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
801 const wxValidator
& validator
,
802 const wxString
& name
)
806 wxGetOsVersion( &major
, &minor
);
808 style
&= ~wxTR_LINES_AT_ROOT
;
809 style
|= wxTR_NO_LINES
;
811 style
|= wxTR_ROW_LINES
;
813 if (style
== 0 || style
& wxTR_DEFAULT_STYLE
)
814 style
|= wxTR_FULL_ROW_HIGHLIGHT
;
818 style
|= wxTR_NO_LINES
;
821 if ( !wxControl::Create( parent
, id
, pos
, size
,
822 style
|wxHSCROLL
|wxVSCROLL
,
827 // If the tree display has no buttons, but does have
828 // connecting lines, we can use a narrower layout.
829 // It may not be a good idea to force this...
830 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
836 wxVisualAttributes attr
= GetDefaultAttributes();
837 SetOwnForegroundColour( attr
.colFg
);
838 SetOwnBackgroundColour( attr
.colBg
);
840 SetOwnFont(attr
.font
);
842 // this is a misnomer: it's called "dotted pen" but uses (default) wxSOLID
843 // style because we apparently get performance problems when using dotted
844 // pen for drawing in some ports -- but under MSW it seems to work fine
846 m_dottedPen
= wxPen(*wxLIGHT_GREY
, 0, wxDOT
);
848 m_dottedPen
= *wxGREY_PEN
;
851 SetInitialSize(size
);
856 wxGenericTreeCtrl::~wxGenericTreeCtrl()
858 delete m_hilightBrush
;
859 delete m_hilightUnfocusedBrush
;
863 delete m_renameTimer
;
866 if (m_ownsImageListButtons
)
867 delete m_imageListButtons
;
870 // -----------------------------------------------------------------------------
872 // -----------------------------------------------------------------------------
874 unsigned int wxGenericTreeCtrl::GetCount() const
882 unsigned int count
= m_anchor
->GetChildrenCount();
883 if ( !HasFlag(wxTR_HIDE_ROOT
) )
885 // take the root itself into account
892 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
894 m_indent
= (unsigned short) indent
;
899 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
900 bool recursively
) const
902 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
904 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
907 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
909 // Do not try to expand the root node if it hasn't been created yet
910 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
912 // if we will hide the root, make sure children are visible
913 m_anchor
->SetHasPlus();
915 CalculatePositions();
918 // right now, just sets the styles. Eventually, we may
919 // want to update the inherited styles, but right now
920 // none of the parents has updatable styles
921 m_windowStyle
= styles
;
925 // -----------------------------------------------------------------------------
926 // functions to work with tree items
927 // -----------------------------------------------------------------------------
929 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
931 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
933 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
936 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
937 wxTreeItemIcon which
) const
939 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
941 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
944 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
946 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
948 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
951 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
953 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
955 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
956 return pItem
->Attr().GetTextColour();
959 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
961 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
963 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
964 return pItem
->Attr().GetBackgroundColour();
967 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
969 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
971 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
972 return pItem
->Attr().GetFont();
975 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
977 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
980 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
981 pItem
->SetText(text
);
982 CalculateSize(pItem
, dc
);
986 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
988 wxTreeItemIcon which
)
990 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
992 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
993 pItem
->SetImage(image
, which
);
996 CalculateSize(pItem
, dc
);
1000 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1002 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1005 data
->SetId( item
);
1007 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1010 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1012 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1014 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1015 pItem
->SetHasPlus(has
);
1019 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1021 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1023 // avoid redrawing the tree if no real change
1024 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1025 if ( pItem
->IsBold() != bold
)
1027 pItem
->SetBold(bold
);
1029 // recalculate the item size as bold and non bold fonts have different
1031 wxClientDC
dc(this);
1032 CalculateSize(pItem
, dc
);
1038 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1041 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1047 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1048 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1051 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1052 pItem
->Attr().SetTextColour(fg
);
1053 pItem
->Attr().SetBackgroundColour(bg
);
1057 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1058 const wxColour
& col
)
1060 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1062 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1063 pItem
->Attr().SetTextColour(col
);
1067 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1068 const wxColour
& col
)
1070 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1072 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1073 pItem
->Attr().SetBackgroundColour(col
);
1077 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1079 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1081 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1082 pItem
->Attr().SetFont(font
);
1086 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1088 wxTreeCtrlBase::SetFont(font
);
1090 m_normalFont
= font
;
1091 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1092 m_normalFont
.GetFamily(),
1093 m_normalFont
.GetStyle(),
1095 m_normalFont
.GetUnderlined(),
1096 m_normalFont
.GetFaceName(),
1097 m_normalFont
.GetEncoding());
1103 // -----------------------------------------------------------------------------
1104 // item status inquiries
1105 // -----------------------------------------------------------------------------
1107 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1109 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1111 // An item is only visible if it's not a descendant of a collapsed item
1112 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1113 wxGenericTreeItem
* parent
= pItem
->GetParent();
1116 if (!parent
->IsExpanded())
1118 parent
= parent
->GetParent();
1122 GetViewStart(& startX
, & startY
);
1124 wxSize clientSize
= GetClientSize();
1127 if (!GetBoundingRect(item
, rect
))
1129 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1131 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1133 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1139 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1141 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1143 // consider that the item does have children if it has the "+" button: it
1144 // might not have them (if it had never been expanded yet) but then it
1145 // could have them as well and it's better to err on this side rather than
1146 // disabling some operations which are restricted to the items with
1147 // children for an item which does have them
1148 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1151 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1153 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1155 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1158 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1160 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1162 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1165 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1167 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1169 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1172 // -----------------------------------------------------------------------------
1174 // -----------------------------------------------------------------------------
1176 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1178 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1180 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1183 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1184 wxTreeItemIdValue
& cookie
) const
1186 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1189 return GetNextChild(item
, cookie
);
1192 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1193 wxTreeItemIdValue
& cookie
) const
1195 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1197 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1199 // it's ok to cast cookie to size_t, we never have indices big enough to
1200 // overflow "void *"
1201 size_t *pIndex
= (size_t *)&cookie
;
1202 if ( *pIndex
< children
.GetCount() )
1204 return children
.Item((*pIndex
)++);
1208 // there are no more of them
1209 return wxTreeItemId();
1213 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1215 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1217 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1218 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1221 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1223 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1225 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1226 wxGenericTreeItem
*parent
= i
->GetParent();
1227 if ( parent
== NULL
)
1229 // root item doesn't have any siblings
1230 return wxTreeItemId();
1233 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1234 int index
= siblings
.Index(i
);
1235 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1237 size_t n
= (size_t)(index
+ 1);
1238 return n
== siblings
.GetCount() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1241 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1243 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1245 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1246 wxGenericTreeItem
*parent
= i
->GetParent();
1247 if ( parent
== NULL
)
1249 // root item doesn't have any siblings
1250 return wxTreeItemId();
1253 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1254 int index
= siblings
.Index(i
);
1255 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1257 return index
== 0 ? wxTreeItemId()
1258 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1261 // Only for internal use right now, but should probably be public
1262 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1264 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1266 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1268 // First see if there are any children.
1269 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1270 if (children
.GetCount() > 0)
1272 return children
.Item(0);
1276 // Try a sibling of this or ancestor instead
1277 wxTreeItemId p
= item
;
1278 wxTreeItemId toFind
;
1281 toFind
= GetNextSibling(p
);
1282 p
= GetItemParent(p
);
1283 } while (p
.IsOk() && !toFind
.IsOk());
1288 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1290 wxTreeItemId id
= GetRootItem();
1299 } while (id
.IsOk());
1301 return wxTreeItemId();
1304 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1306 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1307 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1309 wxTreeItemId id
= item
;
1312 while (id
= GetNext(id
), id
.IsOk())
1318 return wxTreeItemId();
1321 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1323 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1324 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1326 // find out the starting point
1327 wxTreeItemId prevItem
= GetPrevSibling(item
);
1328 if ( !prevItem
.IsOk() )
1330 prevItem
= GetItemParent(item
);
1333 // find the first visible item after it
1334 while ( prevItem
.IsOk() && !IsVisible(prevItem
) )
1336 prevItem
= GetNext(prevItem
);
1337 if ( !prevItem
.IsOk() || prevItem
== item
)
1339 // there are no visible items before item
1340 return wxTreeItemId();
1344 // from there we must be able to navigate until this item
1345 while ( prevItem
.IsOk() )
1347 const wxTreeItemId nextItem
= GetNextVisible(prevItem
);
1348 if ( !nextItem
.IsOk() || nextItem
== item
)
1351 prevItem
= nextItem
;
1357 // called by wxTextTreeCtrl when it marks itself for deletion
1358 void wxGenericTreeCtrl::ResetTextControl()
1363 // find the first item starting with the given prefix after the given item
1364 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1365 const wxString
& prefixOrig
) const
1367 // match is case insensitive as this is more convenient to the user: having
1368 // to press Shift-letter to go to the item starting with a capital letter
1369 // would be too bothersome
1370 wxString prefix
= prefixOrig
.Lower();
1372 // determine the starting point: we shouldn't take the current item (this
1373 // allows to switch between two items starting with the same letter just by
1374 // pressing it) but we shouldn't jump to the next one if the user is
1375 // continuing to type as otherwise he might easily skip the item he wanted
1376 wxTreeItemId id
= idParent
;
1377 if ( prefix
.length() == 1 )
1382 // look for the item starting with the given prefix after it
1383 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1388 // if we haven't found anything...
1391 // ... wrap to the beginning
1393 if ( HasFlag(wxTR_HIDE_ROOT
) )
1395 // can't select virtual root
1399 // and try all the items (stop when we get to the one we started from)
1400 while (id
.IsOk() && id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1404 // If we haven't found the item, id.IsOk() will be false, as per
1411 // -----------------------------------------------------------------------------
1413 // -----------------------------------------------------------------------------
1415 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1417 const wxString
& text
,
1420 wxTreeItemData
*data
)
1422 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1425 // should we give a warning here?
1426 return AddRoot(text
, image
, selImage
, data
);
1429 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1431 wxGenericTreeItem
*item
=
1432 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1436 data
->m_pItem
= item
;
1439 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1442 InvalidateBestSize();
1446 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1449 wxTreeItemData
*data
)
1451 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1453 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1455 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1456 image
, selImage
, data
);
1459 data
->m_pItem
= m_anchor
;
1462 if (HasFlag(wxTR_HIDE_ROOT
))
1464 // if root is hidden, make sure we can navigate
1466 m_anchor
->SetHasPlus();
1468 CalculatePositions();
1471 if (!HasFlag(wxTR_MULTIPLE
))
1473 m_current
= m_key_current
= m_anchor
;
1474 m_current
->SetHilight( true );
1477 InvalidateBestSize();
1481 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1482 const wxTreeItemId
& idPrevious
,
1483 const wxString
& text
,
1484 int image
, int selImage
,
1485 wxTreeItemData
*data
)
1487 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1490 // should we give a warning here?
1491 return AddRoot(text
, image
, selImage
, data
);
1495 if (idPrevious
.IsOk())
1497 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1498 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1499 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1502 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1506 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1508 wxTreeEvent
event(wxEVT_COMMAND_TREE_DELETE_ITEM
, this, item
);
1509 GetEventHandler()->ProcessEvent( event
);
1512 // Don't leave edit or selection on a child which is about to disappear
1513 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1515 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1516 m_textCtrl
->EndEdit( true );
1518 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1519 m_key_current
= NULL
;
1521 if (IsDescendantOf(item
, m_select_me
)) {
1524 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1525 m_current
->SetHilight( false );
1531 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1533 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1535 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1536 ChildrenClosing(item
);
1537 item
->DeleteChildren(this);
1538 InvalidateBestSize();
1541 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1543 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1545 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1547 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1549 // can't delete the item being edited, cancel editing it first
1550 m_textCtrl
->EndEdit( true );
1553 wxGenericTreeItem
*parent
= item
->GetParent();
1555 // don't keep stale pointers around!
1556 if ( IsDescendantOf(item
, m_key_current
) )
1558 // Don't silently change the selection:
1559 // do it properly in idle time, so event
1560 // handlers get called.
1562 // m_key_current = parent;
1563 m_key_current
= NULL
;
1566 // m_select_me records whether we need to select
1567 // a different item, in idle time.
1568 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1570 m_select_me
= parent
;
1573 if ( IsDescendantOf(item
, m_current
) )
1575 // Don't silently change the selection:
1576 // do it properly in idle time, so event
1577 // handlers get called.
1579 // m_current = parent;
1581 m_select_me
= parent
;
1584 // remove the item from the tree
1587 parent
->GetChildren().Remove( item
); // remove by value
1589 else // deleting the root
1591 // nothing will be left in the tree
1595 // and delete all of its children and the item itself now
1596 item
->DeleteChildren(this);
1597 SendDeleteEvent(item
);
1599 if (item
== m_select_me
)
1604 InvalidateBestSize();
1607 void wxGenericTreeCtrl::DeleteAllItems()
1615 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1617 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1619 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1620 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1621 _T("can't expand hidden root") );
1623 if ( !item
->HasPlus() )
1626 if ( item
->IsExpanded() )
1629 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_EXPANDING
, this, item
);
1631 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1633 // cancelled by program
1640 CalculatePositions();
1642 RefreshSubtree(item
);
1649 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1650 GetEventHandler()->ProcessEvent( event
);
1653 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1655 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1656 _T("can't collapse hidden root") );
1658 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1660 if ( !item
->IsExpanded() )
1663 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, this, item
);
1664 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1666 // cancelled by program
1670 ChildrenClosing(item
);
1673 #if 0 // TODO why should items be collapsed recursively?
1674 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1675 size_t count
= children
.GetCount();
1676 for ( size_t n
= 0; n
< count
; n
++ )
1678 Collapse(children
[n
]);
1682 CalculatePositions();
1684 RefreshSubtree(item
);
1686 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1687 GetEventHandler()->ProcessEvent( event
);
1690 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1693 DeleteChildren(item
);
1696 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1698 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1700 if (item
->IsExpanded())
1706 void wxGenericTreeCtrl::Unselect()
1710 m_current
->SetHilight( false );
1711 RefreshLine( m_current
);
1718 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1720 if (item
->IsSelected())
1722 item
->SetHilight(false);
1726 if (item
->HasChildren())
1728 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1729 size_t count
= children
.GetCount();
1730 for ( size_t n
= 0; n
< count
; ++n
)
1732 UnselectAllChildren(children
[n
]);
1737 void wxGenericTreeCtrl::UnselectAll()
1739 wxTreeItemId rootItem
= GetRootItem();
1741 // the tree might not have the root item at all
1744 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1748 // Recursive function !
1749 // To stop we must have crt_item<last_item
1751 // Tag all next children, when no more children,
1752 // Move to parent (not to tag)
1753 // Keep going... if we found last_item, we stop.
1754 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1756 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1758 if (parent
== NULL
) // This is root item
1759 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1761 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1762 int index
= children
.Index(crt_item
);
1763 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1765 size_t count
= children
.GetCount();
1766 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1768 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1771 return TagNextChildren(parent
, last_item
, select
);
1774 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1776 crt_item
->SetHilight(select
);
1777 RefreshLine(crt_item
);
1779 if (crt_item
==last_item
)
1782 if (crt_item
->HasChildren())
1784 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1785 size_t count
= children
.GetCount();
1786 for ( size_t n
= 0; n
< count
; ++n
)
1788 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1796 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1800 // item2 is not necessary after item1
1801 // choice first' and 'last' between item1 and item2
1802 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1803 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1805 bool select
= m_current
->IsSelected();
1807 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1810 TagNextChildren(first
,last
,select
);
1813 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1814 bool unselect_others
,
1815 bool extended_select
)
1817 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1821 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1822 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1824 //wxCHECK_RET( ( (!unselect_others) && is_single),
1825 // wxT("this is a single selection tree") );
1827 // to keep going anyhow !!!
1830 if (item
->IsSelected())
1831 return; // nothing to do
1832 unselect_others
= true;
1833 extended_select
= false;
1835 else if ( unselect_others
&& item
->IsSelected() )
1837 // selection change if there is more than one item currently selected
1838 wxArrayTreeItemIds selected_items
;
1839 if ( GetSelections(selected_items
) == 1 )
1843 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1844 event
.m_itemOld
= m_current
;
1845 // TODO : Here we don't send any selection mode yet !
1847 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1850 wxTreeItemId parent
= GetItemParent( itemId
);
1851 while (parent
.IsOk())
1853 if (!IsExpanded(parent
))
1856 parent
= GetItemParent( parent
);
1860 if (unselect_others
)
1862 if (is_single
) Unselect(); // to speed up thing
1867 if (extended_select
)
1871 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1874 // don't change the mark (m_current)
1875 SelectItemRange(m_current
, item
);
1879 bool select
= true; // the default
1881 // Check if we need to toggle hilight (ctrl mode)
1882 if (!unselect_others
)
1883 select
=!item
->IsSelected();
1885 m_current
= m_key_current
= item
;
1886 m_current
->SetHilight(select
);
1887 RefreshLine( m_current
);
1890 // This can cause idle processing to select the root
1891 // if no item is selected, so it must be after the
1893 EnsureVisible( itemId
);
1895 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1896 GetEventHandler()->ProcessEvent( event
);
1899 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1903 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
1907 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1908 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1910 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1911 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1914 item
->SetHilight(false);
1917 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1918 GetEventHandler()->ProcessEvent( event
);
1922 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1923 wxArrayTreeItemIds
&array
) const
1925 if ( item
->IsSelected() )
1926 array
.Add(wxTreeItemId(item
));
1928 if ( item
->HasChildren() )
1930 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1931 size_t count
= children
.GetCount();
1932 for ( size_t n
= 0; n
< count
; ++n
)
1933 FillArray(children
[n
], array
);
1937 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1940 wxTreeItemId idRoot
= GetRootItem();
1941 if ( idRoot
.IsOk() )
1943 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1945 //else: the tree is empty, so no selections
1947 return array
.GetCount();
1950 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1952 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1954 if (!item
.IsOk()) return;
1956 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1958 // first expand all parent branches
1959 wxGenericTreeItem
*parent
= gitem
->GetParent();
1961 if ( HasFlag(wxTR_HIDE_ROOT
) )
1963 while ( parent
&& parent
!= m_anchor
)
1966 parent
= parent
->GetParent();
1974 parent
= parent
->GetParent();
1978 //if (parent) CalculatePositions();
1983 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1985 if (!item
.IsOk()) return;
1987 // We have to call this here because the label in
1988 // question might just have been added and no screen
1989 // update taken place.
1991 #if defined( __WXMSW__ ) || defined(__WXMAC__)
1994 DoDirtyProcessing();
1996 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1998 // now scroll to the item
1999 int item_y
= gitem
->GetY();
2003 GetViewStart( &start_x
, &start_y
);
2004 start_y
*= PIXELS_PER_UNIT
;
2008 GetClientSize( &client_w
, &client_h
);
2010 if (item_y
< start_y
+3)
2015 m_anchor
->GetSize( x
, y
, this );
2016 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2017 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2018 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2019 // Item should appear at top
2020 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2022 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2027 m_anchor
->GetSize( x
, y
, this );
2028 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2029 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2030 item_y
+= PIXELS_PER_UNIT
+2;
2031 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2032 // Item should appear at bottom
2033 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
);
2037 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2038 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2040 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2041 wxGenericTreeItem
**item2
)
2043 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2045 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2048 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2050 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2052 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2054 wxCHECK_RET( !s_treeBeingSorted
,
2055 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2057 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2058 if ( children
.GetCount() > 1 )
2062 s_treeBeingSorted
= this;
2063 children
.Sort(tree_ctrl_compare_func
);
2064 s_treeBeingSorted
= NULL
;
2066 //else: don't make the tree dirty as nothing changed
2069 void wxGenericTreeCtrl::CalculateLineHeight()
2071 wxClientDC
dc(this);
2072 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2074 if ( m_imageListNormal
)
2076 // Calculate a m_lineHeight value from the normal Image sizes.
2077 // May be toggle off. Then wxGenericTreeCtrl will spread when
2078 // necessary (which might look ugly).
2079 int n
= m_imageListNormal
->GetImageCount();
2080 for (int i
= 0; i
< n
; i
++)
2082 int width
= 0, height
= 0;
2083 m_imageListNormal
->GetSize(i
, width
, height
);
2084 if (height
> m_lineHeight
) m_lineHeight
= height
;
2088 if (m_imageListButtons
)
2090 // Calculate a m_lineHeight value from the Button image sizes.
2091 // May be toggle off. Then wxGenericTreeCtrl will spread when
2092 // necessary (which might look ugly).
2093 int n
= m_imageListButtons
->GetImageCount();
2094 for (int i
= 0; i
< n
; i
++)
2096 int width
= 0, height
= 0;
2097 m_imageListButtons
->GetSize(i
, width
, height
);
2098 if (height
> m_lineHeight
) m_lineHeight
= height
;
2102 if (m_lineHeight
< 30)
2103 m_lineHeight
+= 2; // at least 2 pixels
2105 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2108 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2110 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2111 m_imageListNormal
= imageList
;
2112 m_ownsImageListNormal
= false;
2114 // Don't do any drawing if we're setting the list to NULL,
2115 // since we may be in the process of deleting the tree control.
2117 CalculateLineHeight();
2120 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2122 if (m_ownsImageListState
) delete m_imageListState
;
2123 m_imageListState
= imageList
;
2124 m_ownsImageListState
= false;
2127 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2129 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2130 m_imageListButtons
= imageList
;
2131 m_ownsImageListButtons
= false;
2133 CalculateLineHeight();
2136 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2138 SetButtonsImageList(imageList
);
2139 m_ownsImageListButtons
= true;
2142 // -----------------------------------------------------------------------------
2144 // -----------------------------------------------------------------------------
2146 void wxGenericTreeCtrl::AdjustMyScrollbars()
2151 m_anchor
->GetSize( x
, y
, this );
2152 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2153 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2154 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2155 int y_pos
= GetScrollPos( wxVERTICAL
);
2156 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2160 SetScrollbars( 0, 0, 0, 0 );
2164 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2166 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2167 return item
->GetHeight();
2169 return m_lineHeight
;
2172 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2174 // TODO implement "state" icon on items
2176 wxTreeItemAttr
*attr
= item
->GetAttributes();
2177 if ( attr
&& attr
->HasFont() )
2178 dc
.SetFont(attr
->GetFont());
2179 else if (item
->IsBold())
2180 dc
.SetFont(m_boldFont
);
2182 wxCoord text_w
= 0, text_h
= 0;
2183 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2185 int image_h
= 0, image_w
= 0;
2186 int image
= item
->GetCurrentImage();
2187 if ( image
!= NO_IMAGE
)
2189 if ( m_imageListNormal
)
2191 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2192 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2200 int total_h
= GetLineHeight(item
);
2201 bool drawItemBackground
= false;
2203 if ( item
->IsSelected() )
2205 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2206 drawItemBackground
= true;
2211 if ( attr
&& attr
->HasBackgroundColour() )
2213 drawItemBackground
= true;
2214 colBg
= attr
->GetBackgroundColour();
2218 colBg
= GetBackgroundColour();
2220 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2223 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2225 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2229 GetVirtualSize(&w
, &h
);
2230 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2231 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2232 dc
.DrawRectangle(rect
);
2234 if (!item
->IsSelected())
2236 dc
.DrawRectangle(rect
);
2240 int flags
= wxCONTROL_SELECTED
;
2242 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
2243 && IsControlActive( (ControlRef
)GetHandle() )
2246 flags
|= wxCONTROL_FOCUSED
;
2247 if ((item
== m_current
) && (m_hasFocus
))
2248 flags
|= wxCONTROL_CURRENT
;
2249 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2255 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2257 // If it's selected, and there's an image, then we should
2258 // take care to leave the area under the image painted in the
2259 // background colour.
2260 wxRect
rect( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2261 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2262 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2263 dc
.DrawRectangle( rect
);
2268 int flags
= wxCONTROL_SELECTED
;
2270 flags
|= wxCONTROL_FOCUSED
;
2271 if ((item
== m_current
) && (m_hasFocus
))
2272 flags
|= wxCONTROL_CURRENT
;
2273 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2276 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2277 // don't allow backgrounds to be customized. Not drawing the background,
2278 // except for custom item backgrounds, works for both kinds of theme.
2279 else if (drawItemBackground
)
2281 wxRect
rect( item
->GetX()-2, item
->GetY()+offset
,
2282 item
->GetWidth()+2, total_h
-offset
);
2283 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2284 dc
.DrawRectangle( rect
);
2286 if ( attr
&& attr
->HasBackgroundColour() )
2288 dc
.DrawRectangle( rect
);
2295 int flags
= wxCONTROL_SELECTED
;
2297 flags
|= wxCONTROL_FOCUSED
;
2298 if ((item
== m_current
) && (m_hasFocus
))
2299 flags
|= wxCONTROL_CURRENT
;
2300 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2306 if ( image
!= NO_IMAGE
)
2308 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2309 m_imageListNormal
->Draw( image
, dc
,
2311 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2312 wxIMAGELIST_DRAW_TRANSPARENT
);
2313 dc
.DestroyClippingRegion();
2316 dc
.SetBackgroundMode(wxTRANSPARENT
);
2317 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2318 dc
.DrawText( item
->GetText(),
2319 (wxCoord
)(image_w
+ item
->GetX()),
2320 (wxCoord
)(item
->GetY() + extraH
));
2322 // restore normal font
2323 dc
.SetFont( m_normalFont
);
2326 // Now y stands for the top of the item, whereas it used to stand for middle !
2327 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2329 int x
= level
*m_indent
;
2330 if (!HasFlag(wxTR_HIDE_ROOT
))
2334 else if (level
== 0)
2336 // always expand hidden root
2338 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2339 int count
= children
.GetCount();
2345 PaintLevel(children
[n
], dc
, 1, y
);
2346 } while (++n
< count
);
2348 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2350 // draw line down to last child
2351 origY
+= GetLineHeight(children
[0])>>1;
2352 oldY
+= GetLineHeight(children
[n
-1])>>1;
2353 dc
.DrawLine(3, origY
, 3, oldY
);
2359 item
->SetX(x
+m_spacing
);
2362 int h
= GetLineHeight(item
);
2364 int y_mid
= y_top
+ (h
>>1);
2367 int exposed_x
= dc
.LogicalToDeviceX(0);
2368 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2370 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2374 // don't draw rect outline if we already have the
2375 // background color under Mac
2376 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2377 #endif // !__WXMAC__
2381 if ( item
->IsSelected()
2382 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
2383 // On wxMac, if the tree doesn't have the focus we draw an empty
2384 // rectangle, so we want to make sure that the text is visible
2385 // against the normal background, not the highlightbackground, so
2386 // don't use the highlight text colour unless we have the focus.
2387 && m_hasFocus
&& IsControlActive( (ControlRef
)GetHandle() )
2394 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2399 wxTreeItemAttr
*attr
= item
->GetAttributes();
2400 if (attr
&& attr
->HasTextColour())
2401 colText
= attr
->GetTextColour();
2403 colText
= GetForegroundColour();
2407 dc
.SetTextForeground(colText
);
2411 PaintItem(item
, dc
);
2413 if (HasFlag(wxTR_ROW_LINES
))
2415 // if the background colour is white, choose a
2416 // contrasting color for the lines
2417 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2418 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2419 dc
.DrawLine(0, y_top
, 10000, y_top
);
2420 dc
.DrawLine(0, y
, 10000, y
);
2423 // restore DC objects
2424 dc
.SetBrush(*wxWHITE_BRUSH
);
2425 dc
.SetPen(m_dottedPen
);
2426 dc
.SetTextForeground(*wxBLACK
);
2428 if ( !HasFlag(wxTR_NO_LINES
) )
2430 // draw the horizontal line here
2432 if (x
> (signed)m_indent
)
2433 x_start
-= m_indent
;
2434 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2436 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2439 // should the item show a button?
2440 if ( item
->HasPlus() && HasButtons() )
2442 if ( m_imageListButtons
)
2444 // draw the image button here
2447 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2448 : wxTreeItemIcon_Normal
;
2449 if ( item
->IsSelected() )
2450 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2452 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2453 int xx
= x
- image_w
/2;
2454 int yy
= y_mid
- image_h
/2;
2456 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2457 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2458 wxIMAGELIST_DRAW_TRANSPARENT
);
2460 else // no custom buttons
2462 static const int wImage
= 9;
2463 static const int hImage
= 9;
2466 if (item
->IsExpanded())
2467 flag
|= wxCONTROL_EXPANDED
;
2468 if (item
== m_underMouse
)
2469 flag
|= wxCONTROL_CURRENT
;
2471 wxRendererNative::Get().DrawTreeItemButton
2475 wxRect(x
- wImage
/2,
2484 if (item
->IsExpanded())
2486 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2487 int count
= children
.GetCount();
2494 PaintLevel(children
[n
], dc
, level
, y
);
2495 } while (++n
< count
);
2497 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2499 // draw line down to last child
2500 oldY
+= GetLineHeight(children
[n
-1])>>1;
2501 if (HasButtons()) y_mid
+= 5;
2503 // Only draw the portion of the line that is visible, in case it is huge
2504 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2505 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2506 yOrigin
= abs(yOrigin
);
2507 GetClientSize(&width
, &height
);
2509 // Move end points to the begining/end of the view?
2510 if (y_mid
< yOrigin
)
2512 if (oldY
> yOrigin
+ height
)
2513 oldY
= yOrigin
+ height
;
2515 // after the adjustments if y_mid is larger than oldY then the line
2516 // isn't visible at all so don't draw anything
2518 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2524 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2528 if ( item
->HasPlus() )
2530 // it's a folder, indicate it by a border
2535 // draw a line under the drop target because the item will be
2537 DrawLine(item
, !m_dropEffectAboveItem
);
2540 SetCursor(wxCURSOR_BULLSEYE
);
2545 SetCursor(wxCURSOR_NO_ENTRY
);
2549 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2551 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2553 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2555 wxClientDC
dc(this);
2557 dc
.SetLogicalFunction(wxINVERT
);
2558 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2560 int w
= i
->GetWidth() + 2;
2561 int h
= GetLineHeight(i
) + 2;
2563 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2566 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2568 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2570 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2572 wxClientDC
dc(this);
2574 dc
.SetLogicalFunction(wxINVERT
);
2580 y
+= GetLineHeight(i
) - 1;
2583 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2586 // -----------------------------------------------------------------------------
2587 // wxWidgets callbacks
2588 // -----------------------------------------------------------------------------
2590 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
2593 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
2594 RefreshLine( m_current
);
2600 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2608 dc
.SetFont( m_normalFont
);
2609 dc
.SetPen( m_dottedPen
);
2611 // this is now done dynamically
2612 //if(GetImageList() == NULL)
2613 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2616 PaintLevel( m_anchor
, dc
, 0, y
);
2619 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2628 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2637 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2639 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2640 te
.m_evtKey
= event
;
2641 if ( GetEventHandler()->ProcessEvent( te
) )
2643 // intercepted by the user code
2647 if ( (m_current
== 0) || (m_key_current
== 0) )
2653 // how should the selection work for this event?
2654 bool is_multiple
, extended_select
, unselect_others
;
2655 EventFlagsToSelType(GetWindowStyleFlag(),
2658 is_multiple
, extended_select
, unselect_others
);
2660 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2662 if (event
.GetKeyCode() == WXK_RIGHT
)
2663 event
.m_keyCode
= WXK_LEFT
;
2664 else if (event
.GetKeyCode() == WXK_LEFT
)
2665 event
.m_keyCode
= WXK_RIGHT
;
2670 // * : Expand all/Collapse all
2671 // ' ' | return : activate
2672 // up : go up (not last children!)
2674 // left : go to parent
2675 // right : open if parent and go next
2676 // home : go to root
2677 // end : go to last item without opening parents
2678 // alnum : start or continue searching for the item with this prefix
2679 int keyCode
= event
.GetKeyCode();
2684 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2692 if ( !IsExpanded(m_current
) )
2695 ExpandAllChildren(m_current
);
2698 //else: fall through to Collapse() it
2702 if (IsExpanded(m_current
))
2704 Collapse(m_current
);
2710 // Use the item's bounding rectangle to determine position for the event
2712 GetBoundingRect(m_current
, ItemRect
, true);
2714 wxTreeEvent
eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU
, this, m_current
);
2715 // Use the left edge, vertical middle
2716 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
2717 ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2718 GetEventHandler()->ProcessEvent( eventMenu
);
2724 if ( !event
.HasModifiers() )
2726 wxTreeEvent
eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, m_current
);
2727 GetEventHandler()->ProcessEvent( eventAct
);
2730 // in any case, also generate the normal key event for this key,
2731 // even if we generated the ACTIVATED event above: this is what
2732 // wxMSW does and it makes sense because you might not want to
2733 // process ACTIVATED event at all and handle Space and Return
2734 // directly (and differently) which would be impossible otherwise
2738 // up goes to the previous sibling or to the last
2739 // of its children if it's expanded
2742 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2745 prev
= GetItemParent( m_key_current
);
2746 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2748 break; // don't go to root if it is hidden
2752 wxTreeItemIdValue cookie
;
2753 wxTreeItemId current
= m_key_current
;
2754 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2755 if (current
== GetFirstChild( prev
, cookie
))
2757 // otherwise we return to where we came from
2758 DoSelectItem( prev
, unselect_others
, extended_select
);
2759 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2766 while ( IsExpanded(prev
) && HasChildren(prev
) )
2768 wxTreeItemId child
= GetLastChild(prev
);
2775 DoSelectItem( prev
, unselect_others
, extended_select
);
2776 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2781 // left arrow goes to the parent
2784 wxTreeItemId prev
= GetItemParent( m_current
);
2785 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2787 // don't go to root if it is hidden
2788 prev
= GetPrevSibling( m_current
);
2792 DoSelectItem( prev
, unselect_others
, extended_select
);
2798 // this works the same as the down arrow except that we
2799 // also expand the item if it wasn't expanded yet
2800 if (m_current
!= GetRootItem().m_pItem
|| !HasFlag(wxTR_HIDE_ROOT
))
2802 //else: don't try to expand hidden root item (which can be the
2803 // current one when the tree is empty)
2809 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2811 wxTreeItemIdValue cookie
;
2812 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2816 DoSelectItem( child
, unselect_others
, extended_select
);
2817 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2821 wxTreeItemId next
= GetNextSibling( m_key_current
);
2824 wxTreeItemId current
= m_key_current
;
2825 while (current
.IsOk() && !next
)
2827 current
= GetItemParent( current
);
2828 if (current
) next
= GetNextSibling( current
);
2833 DoSelectItem( next
, unselect_others
, extended_select
);
2834 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2840 // <End> selects the last visible tree item
2843 wxTreeItemId last
= GetRootItem();
2845 while ( last
.IsOk() && IsExpanded(last
) )
2847 wxTreeItemId lastChild
= GetLastChild(last
);
2849 // it may happen if the item was expanded but then all of
2850 // its children have been deleted - so IsExpanded() returned
2851 // true, but GetLastChild() returned invalid item
2860 DoSelectItem( last
, unselect_others
, extended_select
);
2865 // <Home> selects the root item
2868 wxTreeItemId prev
= GetRootItem();
2872 if ( HasFlag(wxTR_HIDE_ROOT
) )
2874 wxTreeItemIdValue cookie
;
2875 prev
= GetFirstChild(prev
, cookie
);
2880 DoSelectItem( prev
, unselect_others
, extended_select
);
2885 // do not use wxIsalnum() here
2886 if ( !event
.HasModifiers() &&
2887 ((keyCode
>= '0' && keyCode
<= '9') ||
2888 (keyCode
>= 'a' && keyCode
<= 'z') ||
2889 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2891 // find the next item starting with the given prefix
2892 wxChar ch
= (wxChar
)keyCode
;
2894 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2905 // also start the timer to reset the current prefix if the user
2906 // doesn't press any more alnum keys soon -- we wouldn't want
2907 // to use this prefix for a new item search
2910 m_findTimer
= new wxTreeFindTimer(this);
2913 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2923 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2928 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2929 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2930 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2931 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2932 if (flags
) return wxTreeItemId();
2934 if (m_anchor
== NULL
)
2936 flags
= wxTREE_HITTEST_NOWHERE
;
2937 return wxTreeItemId();
2940 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2944 flags
= wxTREE_HITTEST_NOWHERE
;
2945 return wxTreeItemId();
2950 // get the bounding rectangle of the item (or of its label only)
2951 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2953 bool textOnly
) const
2955 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2957 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2962 rect
.width
= i
->GetWidth();
2964 if ( m_imageListNormal
)
2966 int image_w
, image_h
;
2967 m_imageListNormal
->GetSize( 0, image_w
, image_h
);
2968 rect
.width
+= image_w
+ MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2971 else // the entire line
2974 rect
.width
= GetClientSize().x
;
2978 rect
.height
= GetLineHeight(i
);
2980 // we have to return the logical coordinates, not physical ones
2981 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
2986 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
2987 wxClassInfo
* WXUNUSED(textCtrlClass
))
2989 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
2991 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2993 wxTreeEvent
te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
2994 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
3000 // We have to call this here because the label in
3001 // question might just have been added and no screen
3002 // update taken place.
3004 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3007 DoDirtyProcessing();
3010 // TODO: use textCtrlClass here to create the control of correct class
3011 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
3013 m_textCtrl
->SetFocus();
3018 // returns a pointer to the text edit control if the item is being
3019 // edited, NULL otherwise (it's assumed that no more than one item may
3020 // be edited simultaneously)
3021 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3026 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3027 bool discardChanges
)
3029 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
3031 m_textCtrl
->EndEdit(discardChanges
);
3034 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3035 const wxString
& value
)
3037 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3039 le
.m_editCancelled
= false;
3041 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3044 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3046 // let owner know that the edit was cancelled
3047 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3048 le
.m_label
= wxEmptyString
;
3049 le
.m_editCancelled
= true;
3051 GetEventHandler()->ProcessEvent( le
);
3054 void wxGenericTreeCtrl::OnRenameTimer()
3056 EditLabel( m_current
);
3059 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3061 if ( !m_anchor
)return;
3063 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3065 // Is the mouse over a tree item button?
3067 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3068 wxGenericTreeItem
*underMouse
= thisItem
;
3070 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3071 #endif // wxUSE_TOOLTIPS
3074 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3075 (!event
.LeftIsDown()) &&
3077 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3085 if (underMouse
!= m_underMouse
)
3089 // unhighlight old item
3090 wxGenericTreeItem
*tmp
= m_underMouse
;
3091 m_underMouse
= NULL
;
3095 m_underMouse
= underMouse
;
3097 RefreshLine( m_underMouse
);
3101 // Determines what item we are hovering over and need a tooltip for
3102 wxTreeItemId hoverItem
= thisItem
;
3104 // We do not want a tooltip if we are dragging, or if the rename timer is running
3105 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3107 // Ask the tree control what tooltip (if any) should be shown
3108 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3110 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3112 SetToolTip(hevent
.m_label
);
3117 // we process left mouse up event (enables in-place edit), middle/right down
3118 // (pass to the user code), left dbl click (activate item) and
3119 // dragging/moving events for items drag-and-drop
3120 if ( !(event
.LeftDown() ||
3122 event
.MiddleDown() ||
3123 event
.RightDown() ||
3124 event
.LeftDClick() ||
3126 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3135 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3137 if ( event
.Dragging() && !m_isDragging
)
3139 if (m_dragCount
== 0)
3144 if (m_dragCount
!= 3)
3146 // wait until user drags a bit further...
3150 wxEventType command
= event
.RightIsDown()
3151 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3152 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3154 wxTreeEvent
nevent(command
, this, m_current
);
3155 nevent
.SetPoint(CalcScrolledPosition(pt
));
3157 // by default the dragging is not supported, the user code must
3158 // explicitly allow the event for it to take place
3161 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3163 // we're going to drag this item
3164 m_isDragging
= true;
3166 // remember the old cursor because we will change it while
3168 m_oldCursor
= m_cursor
;
3170 // in a single selection control, hide the selection temporarily
3171 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3173 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3175 if ( m_oldSelection
)
3177 m_oldSelection
->SetHilight(false);
3178 RefreshLine(m_oldSelection
);
3185 else if ( event
.Dragging() )
3187 if ( item
!= m_dropTarget
)
3189 // unhighlight the previous drop target
3190 DrawDropEffect(m_dropTarget
);
3192 m_dropTarget
= item
;
3194 // highlight the current drop target if any
3195 DrawDropEffect(m_dropTarget
);
3197 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3204 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3208 // erase the highlighting
3209 DrawDropEffect(m_dropTarget
);
3211 if ( m_oldSelection
)
3213 m_oldSelection
->SetHilight(true);
3214 RefreshLine(m_oldSelection
);
3215 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3218 // generate the drag end event
3219 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, this, item
);
3221 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3223 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3225 m_isDragging
= false;
3226 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3228 SetCursor(m_oldCursor
);
3230 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3238 // If we got to this point, we are not dragging or moving the mouse.
3239 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3240 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3241 // We skip even if we didn't hit an item because we still should
3242 // restore focus to the tree control even if we didn't exactly hit an item.
3243 if ( event
.LeftDown() )
3248 // here we process only the messages which happen on tree items
3252 if (item
== NULL
) return; /* we hit the blank area */
3254 if ( event
.RightDown() )
3256 // If the item is already selected, do not update the selection.
3257 // Multi-selections should not be cleared if a selected item is clicked.
3258 if (!IsSelected(item
))
3260 DoSelectItem(item
, true, false);
3263 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, this, item
);
3264 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3265 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3267 // Consistent with MSW (for now), send the ITEM_MENU *after*
3268 // the RIGHT_CLICK event. TODO: This behavior may change.
3269 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
3270 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3271 GetEventHandler()->ProcessEvent(nevent2
);
3273 else if ( event
.MiddleDown() )
3275 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
, this, item
);
3276 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3277 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3279 else if ( event
.LeftUp() )
3281 // this facilitates multiple-item drag-and-drop
3283 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3285 wxArrayTreeItemIds selections
;
3286 size_t count
= GetSelections(selections
);
3292 DoSelectItem(item
, true, false);
3298 if ( (item
== m_current
) &&
3299 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3300 HasFlag(wxTR_EDIT_LABELS
) )
3302 if ( m_renameTimer
)
3304 if ( m_renameTimer
->IsRunning() )
3305 m_renameTimer
->Stop();
3309 m_renameTimer
= new wxTreeRenameTimer( this );
3312 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3315 m_lastOnSame
= false;
3318 else // !RightDown() && !MiddleDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3320 if ( event
.LeftDown() )
3322 m_lastOnSame
= item
== m_current
;
3325 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3327 // only toggle the item for a single click, double click on
3328 // the button doesn't do anything (it toggles the item twice)
3329 if ( event
.LeftDown() )
3334 // don't select the item if the button was clicked
3339 // clear the previously selected items, if the
3340 // user clicked outside of the present selection.
3341 // otherwise, perform the deselection on mouse-up.
3342 // this allows multiple drag and drop to work.
3343 // but if Cmd is down, toggle selection of the clicked item
3344 if (!IsSelected(item
) || event
.CmdDown())
3346 // how should the selection work for this event?
3347 bool is_multiple
, extended_select
, unselect_others
;
3348 EventFlagsToSelType(GetWindowStyleFlag(),
3351 is_multiple
, extended_select
, unselect_others
);
3353 DoSelectItem(item
, unselect_others
, extended_select
);
3357 // For some reason, Windows isn't recognizing a left double-click,
3358 // so we need to simulate it here. Allow 200 milliseconds for now.
3359 if ( event
.LeftDClick() )
3361 // double clicking should not start editing the item label
3362 if ( m_renameTimer
)
3363 m_renameTimer
->Stop();
3365 m_lastOnSame
= false;
3367 // send activate event first
3368 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, item
);
3369 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3370 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3372 // if the user code didn't process the activate event,
3373 // handle it ourselves by toggling the item when it is
3375 if ( item
->HasPlus() )
3385 void wxGenericTreeCtrl::OnInternalIdle()
3387 wxWindow::OnInternalIdle();
3389 // Check if we need to select the root item
3390 // because nothing else has been selected.
3391 // Delaying it means that we can invoke event handlers
3392 // as required, when a first item is selected.
3393 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3396 SelectItem(m_select_me
);
3397 else if (GetRootItem().IsOk())
3398 SelectItem(GetRootItem());
3401 // after all changes have been done to the tree control,
3402 // actually redraw the tree when everything is over
3404 DoDirtyProcessing();
3407 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3412 wxTreeItemAttr
*attr
= item
->GetAttributes();
3413 if ( attr
&& attr
->HasFont() )
3414 dc
.SetFont(attr
->GetFont());
3415 else if ( item
->IsBold() )
3416 dc
.SetFont(m_boldFont
);
3418 dc
.SetFont(m_normalFont
);
3420 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3423 // restore normal font
3424 dc
.SetFont( m_normalFont
);
3428 int image
= item
->GetCurrentImage();
3429 if ( image
!= NO_IMAGE
)
3431 if ( m_imageListNormal
)
3433 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3434 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3438 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3441 total_h
+= 2; // at least 2 pixels
3443 total_h
+= total_h
/10; // otherwise 10% extra spacing
3445 item
->SetHeight(total_h
);
3446 if (total_h
>m_lineHeight
)
3447 m_lineHeight
=total_h
;
3449 item
->SetWidth(image_w
+text_w
+2);
3452 // -----------------------------------------------------------------------------
3453 // for developper : y is now the top of the level
3454 // not the middle of it !
3455 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3457 int x
= level
*m_indent
;
3458 if (!HasFlag(wxTR_HIDE_ROOT
))
3462 else if (level
== 0)
3464 // a hidden root is not evaluated, but its
3465 // children are always calculated
3469 CalculateSize( item
, dc
);
3472 item
->SetX( x
+m_spacing
);
3474 y
+= GetLineHeight(item
);
3476 if ( !item
->IsExpanded() )
3478 // we don't need to calculate collapsed branches
3483 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3484 size_t n
, count
= children
.GetCount();
3486 for (n
= 0; n
< count
; ++n
)
3487 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3490 void wxGenericTreeCtrl::CalculatePositions()
3492 if ( !m_anchor
) return;
3494 wxClientDC
dc(this);
3497 dc
.SetFont( m_normalFont
);
3499 dc
.SetPen( m_dottedPen
);
3500 //if(GetImageList() == NULL)
3501 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3504 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3507 void wxGenericTreeCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
3510 wxTreeCtrlBase::Refresh(eraseBackground
, rect
);
3513 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3515 if (m_dirty
|| IsFrozen() )
3518 wxSize client
= GetClientSize();
3521 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3522 rect
.width
= client
.x
;
3523 rect
.height
= client
.y
;
3525 Refresh(true, &rect
);
3527 AdjustMyScrollbars();
3530 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3532 if (m_dirty
|| IsFrozen() )
3536 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3537 rect
.width
= GetClientSize().x
;
3538 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3540 Refresh(true, &rect
);
3543 void wxGenericTreeCtrl::RefreshSelected()
3548 // TODO: this is awfully inefficient, we should keep the list of all
3549 // selected items internally, should be much faster
3551 RefreshSelectedUnder(m_anchor
);
3554 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3559 if ( item
->IsSelected() )
3562 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3563 size_t count
= children
.GetCount();
3564 for ( size_t n
= 0; n
< count
; n
++ )
3566 RefreshSelectedUnder(children
[n
]);
3570 void wxGenericTreeCtrl::DoThaw()
3573 DoDirtyProcessing();
3578 // ----------------------------------------------------------------------------
3579 // changing colours: we need to refresh the tree control
3580 // ----------------------------------------------------------------------------
3582 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3584 if ( !wxWindow::SetBackgroundColour(colour
) )
3592 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3594 if ( !wxWindow::SetForegroundColour(colour
) )
3602 // Process the tooltip event, to speed up event processing.
3603 // Doesn't actually get a tooltip.
3604 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3610 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3611 // be removed, as well as the #else case below.
3612 #define _USE_VISATTR 0
3617 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3619 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3623 // Use the same color scheme as wxListBox
3624 return wxListBox::GetClassDefaultAttributes(variant
);
3626 wxVisualAttributes attr
;
3627 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3628 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3629 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3634 void wxGenericTreeCtrl::DoDirtyProcessing()
3641 CalculatePositions();
3643 AdjustMyScrollbars();
3646 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
3648 // make sure all positions are calculated as normally this only done during
3649 // idle time but we need them for base class DoGetBestSize() to return the
3651 wxConstCast(this, wxGenericTreeCtrl
)->CalculatePositions();
3653 wxSize size
= wxTreeCtrlBase::DoGetBestSize();
3655 // there seems to be an implicit extra border around the items, although
3656 // I'm not really sure where does it come from -- but without this, the
3657 // scrollbars appear in a tree with default/best size
3660 // and the border has to be rounded up to a multiple of PIXELS_PER_UNIT or
3661 // scrollbars still appear
3662 const wxSize
& borderSize
= GetWindowBorderSize();
3664 int dx
= (size
.x
- borderSize
.x
) % PIXELS_PER_UNIT
;
3666 size
.x
+= PIXELS_PER_UNIT
- dx
;
3667 int dy
= (size
.y
- borderSize
.y
) % PIXELS_PER_UNIT
;
3669 size
.y
+= PIXELS_PER_UNIT
- dy
;
3671 // we need to update the cache too as the base class cached its own value
3672 CacheBestSize(size
);
3677 #endif // wxUSE_TREECTRL