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
);
809 style
|= wxTR_ROW_LINES
;
812 if ( !wxControl::Create( parent
, id
, pos
, size
,
813 style
|wxHSCROLL
|wxVSCROLL
,
818 // If the tree display has no buttons, but does have
819 // connecting lines, we can use a narrower layout.
820 // It may not be a good idea to force this...
821 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
827 wxVisualAttributes attr
= GetDefaultAttributes();
828 SetOwnForegroundColour( attr
.colFg
);
829 SetOwnBackgroundColour( attr
.colBg
);
831 SetOwnFont(attr
.font
);
833 // this is a misnomer: it's called "dotted pen" but uses (default) wxSOLID
834 // style because we apparently get performance problems when using dotted
835 // pen for drawing in some ports -- but under MSW it seems to work fine
837 m_dottedPen
= wxPen(*wxLIGHT_GREY
, 0, wxDOT
);
839 m_dottedPen
= *wxGREY_PEN
;
842 SetInitialSize(size
);
847 wxGenericTreeCtrl::~wxGenericTreeCtrl()
849 delete m_hilightBrush
;
850 delete m_hilightUnfocusedBrush
;
854 delete m_renameTimer
;
857 if (m_ownsImageListButtons
)
858 delete m_imageListButtons
;
861 // -----------------------------------------------------------------------------
863 // -----------------------------------------------------------------------------
865 unsigned int wxGenericTreeCtrl::GetCount() const
873 unsigned int count
= m_anchor
->GetChildrenCount();
874 if ( !HasFlag(wxTR_HIDE_ROOT
) )
876 // take the root itself into account
883 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
885 m_indent
= (unsigned short) indent
;
890 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
891 bool recursively
) const
893 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
895 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
898 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
900 // Do not try to expand the root node if it hasn't been created yet
901 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
903 // if we will hide the root, make sure children are visible
904 m_anchor
->SetHasPlus();
906 CalculatePositions();
909 // right now, just sets the styles. Eventually, we may
910 // want to update the inherited styles, but right now
911 // none of the parents has updatable styles
912 m_windowStyle
= styles
;
916 // -----------------------------------------------------------------------------
917 // functions to work with tree items
918 // -----------------------------------------------------------------------------
920 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
922 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
924 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
927 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
928 wxTreeItemIcon which
) const
930 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
932 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
935 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
937 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
939 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
942 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
944 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
946 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
947 return pItem
->Attr().GetTextColour();
950 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
952 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
954 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
955 return pItem
->Attr().GetBackgroundColour();
958 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
960 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
962 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
963 return pItem
->Attr().GetFont();
966 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
968 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
971 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
972 pItem
->SetText(text
);
973 CalculateSize(pItem
, dc
);
977 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
979 wxTreeItemIcon which
)
981 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
983 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
984 pItem
->SetImage(image
, which
);
987 CalculateSize(pItem
, dc
);
991 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
993 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
998 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1001 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1003 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1005 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1006 pItem
->SetHasPlus(has
);
1010 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1012 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1014 // avoid redrawing the tree if no real change
1015 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1016 if ( pItem
->IsBold() != bold
)
1018 pItem
->SetBold(bold
);
1020 // recalculate the item size as bold and non bold fonts have different
1022 wxClientDC
dc(this);
1023 CalculateSize(pItem
, dc
);
1029 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1032 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1038 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1039 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1042 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1043 pItem
->Attr().SetTextColour(fg
);
1044 pItem
->Attr().SetBackgroundColour(bg
);
1048 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1049 const wxColour
& col
)
1051 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1053 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1054 pItem
->Attr().SetTextColour(col
);
1058 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1059 const wxColour
& col
)
1061 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1063 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1064 pItem
->Attr().SetBackgroundColour(col
);
1068 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1070 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1072 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1073 pItem
->Attr().SetFont(font
);
1077 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1079 wxTreeCtrlBase::SetFont(font
);
1081 m_normalFont
= font
;
1082 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1083 m_normalFont
.GetFamily(),
1084 m_normalFont
.GetStyle(),
1086 m_normalFont
.GetUnderlined(),
1087 m_normalFont
.GetFaceName(),
1088 m_normalFont
.GetEncoding());
1094 // -----------------------------------------------------------------------------
1095 // item status inquiries
1096 // -----------------------------------------------------------------------------
1098 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1100 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1102 // An item is only visible if it's not a descendant of a collapsed item
1103 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1104 wxGenericTreeItem
* parent
= pItem
->GetParent();
1107 if (!parent
->IsExpanded())
1109 parent
= parent
->GetParent();
1113 GetViewStart(& startX
, & startY
);
1115 wxSize clientSize
= GetClientSize();
1118 if (!GetBoundingRect(item
, rect
))
1120 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1122 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1124 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1130 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1132 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1134 // consider that the item does have children if it has the "+" button: it
1135 // might not have them (if it had never been expanded yet) but then it
1136 // could have them as well and it's better to err on this side rather than
1137 // disabling some operations which are restricted to the items with
1138 // children for an item which does have them
1139 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1142 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1144 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1146 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1149 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1151 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1153 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1156 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1158 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1160 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1163 // -----------------------------------------------------------------------------
1165 // -----------------------------------------------------------------------------
1167 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1169 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1171 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1174 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1175 wxTreeItemIdValue
& cookie
) const
1177 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1180 return GetNextChild(item
, cookie
);
1183 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1184 wxTreeItemIdValue
& cookie
) const
1186 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1188 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1190 // it's ok to cast cookie to size_t, we never have indices big enough to
1191 // overflow "void *"
1192 size_t *pIndex
= (size_t *)&cookie
;
1193 if ( *pIndex
< children
.GetCount() )
1195 return children
.Item((*pIndex
)++);
1199 // there are no more of them
1200 return wxTreeItemId();
1204 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1206 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1208 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1209 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1212 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1214 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1216 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1217 wxGenericTreeItem
*parent
= i
->GetParent();
1218 if ( parent
== NULL
)
1220 // root item doesn't have any siblings
1221 return wxTreeItemId();
1224 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1225 int index
= siblings
.Index(i
);
1226 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1228 size_t n
= (size_t)(index
+ 1);
1229 return n
== siblings
.GetCount() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1232 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1234 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1236 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1237 wxGenericTreeItem
*parent
= i
->GetParent();
1238 if ( parent
== NULL
)
1240 // root item doesn't have any siblings
1241 return wxTreeItemId();
1244 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1245 int index
= siblings
.Index(i
);
1246 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1248 return index
== 0 ? wxTreeItemId()
1249 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1252 // Only for internal use right now, but should probably be public
1253 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1255 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1257 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1259 // First see if there are any children.
1260 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1261 if (children
.GetCount() > 0)
1263 return children
.Item(0);
1267 // Try a sibling of this or ancestor instead
1268 wxTreeItemId p
= item
;
1269 wxTreeItemId toFind
;
1272 toFind
= GetNextSibling(p
);
1273 p
= GetItemParent(p
);
1274 } while (p
.IsOk() && !toFind
.IsOk());
1279 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1281 wxTreeItemId id
= GetRootItem();
1290 } while (id
.IsOk());
1292 return wxTreeItemId();
1295 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1297 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1298 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1300 wxTreeItemId id
= item
;
1303 while (id
= GetNext(id
), id
.IsOk())
1309 return wxTreeItemId();
1312 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1314 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1315 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1317 // find out the starting point
1318 wxTreeItemId prevItem
= GetPrevSibling(item
);
1319 if ( !prevItem
.IsOk() )
1321 prevItem
= GetItemParent(item
);
1324 // find the first visible item after it
1325 while ( prevItem
.IsOk() && !IsVisible(prevItem
) )
1327 prevItem
= GetNext(prevItem
);
1328 if ( !prevItem
.IsOk() || prevItem
== item
)
1330 // there are no visible items before item
1331 return wxTreeItemId();
1335 // from there we must be able to navigate until this item
1336 while ( prevItem
.IsOk() )
1338 const wxTreeItemId nextItem
= GetNextVisible(prevItem
);
1339 if ( !nextItem
.IsOk() || nextItem
== item
)
1342 prevItem
= nextItem
;
1348 // called by wxTextTreeCtrl when it marks itself for deletion
1349 void wxGenericTreeCtrl::ResetTextControl()
1354 // find the first item starting with the given prefix after the given item
1355 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1356 const wxString
& prefixOrig
) const
1358 // match is case insensitive as this is more convenient to the user: having
1359 // to press Shift-letter to go to the item starting with a capital letter
1360 // would be too bothersome
1361 wxString prefix
= prefixOrig
.Lower();
1363 // determine the starting point: we shouldn't take the current item (this
1364 // allows to switch between two items starting with the same letter just by
1365 // pressing it) but we shouldn't jump to the next one if the user is
1366 // continuing to type as otherwise he might easily skip the item he wanted
1367 wxTreeItemId id
= idParent
;
1368 if ( prefix
.length() == 1 )
1373 // look for the item starting with the given prefix after it
1374 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1379 // if we haven't found anything...
1382 // ... wrap to the beginning
1384 if ( HasFlag(wxTR_HIDE_ROOT
) )
1386 // can't select virtual root
1390 // and try all the items (stop when we get to the one we started from)
1391 while (id
.IsOk() && id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1395 // If we haven't found the item, id.IsOk() will be false, as per
1402 // -----------------------------------------------------------------------------
1404 // -----------------------------------------------------------------------------
1406 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1408 const wxString
& text
,
1411 wxTreeItemData
*data
)
1413 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1416 // should we give a warning here?
1417 return AddRoot(text
, image
, selImage
, data
);
1420 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1422 wxGenericTreeItem
*item
=
1423 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1427 data
->m_pItem
= item
;
1430 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1433 InvalidateBestSize();
1437 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1440 wxTreeItemData
*data
)
1442 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1444 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1446 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1447 image
, selImage
, data
);
1450 data
->m_pItem
= m_anchor
;
1453 if (HasFlag(wxTR_HIDE_ROOT
))
1455 // if root is hidden, make sure we can navigate
1457 m_anchor
->SetHasPlus();
1459 CalculatePositions();
1462 if (!HasFlag(wxTR_MULTIPLE
))
1464 m_current
= m_key_current
= m_anchor
;
1465 m_current
->SetHilight( true );
1468 InvalidateBestSize();
1472 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1473 const wxTreeItemId
& idPrevious
,
1474 const wxString
& text
,
1475 int image
, int selImage
,
1476 wxTreeItemData
*data
)
1478 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1481 // should we give a warning here?
1482 return AddRoot(text
, image
, selImage
, data
);
1486 if (idPrevious
.IsOk())
1488 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1489 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1490 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1493 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1497 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1499 wxTreeEvent
event(wxEVT_COMMAND_TREE_DELETE_ITEM
, this, item
);
1500 GetEventHandler()->ProcessEvent( event
);
1503 // Don't leave edit or selection on a child which is about to disappear
1504 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1506 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1507 m_textCtrl
->EndEdit( true );
1509 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1510 m_key_current
= NULL
;
1512 if (IsDescendantOf(item
, m_select_me
)) {
1515 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1516 m_current
->SetHilight( false );
1522 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1524 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1526 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1527 ChildrenClosing(item
);
1528 item
->DeleteChildren(this);
1529 InvalidateBestSize();
1532 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1534 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1536 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1538 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1540 // can't delete the item being edited, cancel editing it first
1541 m_textCtrl
->EndEdit( true );
1544 wxGenericTreeItem
*parent
= item
->GetParent();
1546 // don't keep stale pointers around!
1547 if ( IsDescendantOf(item
, m_key_current
) )
1549 // Don't silently change the selection:
1550 // do it properly in idle time, so event
1551 // handlers get called.
1553 // m_key_current = parent;
1554 m_key_current
= NULL
;
1557 // m_select_me records whether we need to select
1558 // a different item, in idle time.
1559 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1561 m_select_me
= parent
;
1564 if ( IsDescendantOf(item
, m_current
) )
1566 // Don't silently change the selection:
1567 // do it properly in idle time, so event
1568 // handlers get called.
1570 // m_current = parent;
1572 m_select_me
= parent
;
1575 // remove the item from the tree
1578 parent
->GetChildren().Remove( item
); // remove by value
1580 else // deleting the root
1582 // nothing will be left in the tree
1586 // and delete all of its children and the item itself now
1587 item
->DeleteChildren(this);
1588 SendDeleteEvent(item
);
1590 if (item
== m_select_me
)
1595 InvalidateBestSize();
1598 void wxGenericTreeCtrl::DeleteAllItems()
1606 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1608 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1610 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1611 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1612 _T("can't expand hidden root") );
1614 if ( !item
->HasPlus() )
1617 if ( item
->IsExpanded() )
1620 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_EXPANDING
, this, item
);
1622 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1624 // cancelled by program
1631 CalculatePositions();
1633 RefreshSubtree(item
);
1640 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1641 GetEventHandler()->ProcessEvent( event
);
1644 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1646 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1647 _T("can't collapse hidden root") );
1649 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1651 if ( !item
->IsExpanded() )
1654 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, this, item
);
1655 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1657 // cancelled by program
1661 ChildrenClosing(item
);
1664 #if 0 // TODO why should items be collapsed recursively?
1665 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1666 size_t count
= children
.GetCount();
1667 for ( size_t n
= 0; n
< count
; n
++ )
1669 Collapse(children
[n
]);
1673 CalculatePositions();
1675 RefreshSubtree(item
);
1677 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1678 GetEventHandler()->ProcessEvent( event
);
1681 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1684 DeleteChildren(item
);
1687 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1689 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1691 if (item
->IsExpanded())
1697 void wxGenericTreeCtrl::Unselect()
1701 m_current
->SetHilight( false );
1702 RefreshLine( m_current
);
1709 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1711 if (item
->IsSelected())
1713 item
->SetHilight(false);
1717 if (item
->HasChildren())
1719 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1720 size_t count
= children
.GetCount();
1721 for ( size_t n
= 0; n
< count
; ++n
)
1723 UnselectAllChildren(children
[n
]);
1728 void wxGenericTreeCtrl::UnselectAll()
1730 wxTreeItemId rootItem
= GetRootItem();
1732 // the tree might not have the root item at all
1735 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1739 // Recursive function !
1740 // To stop we must have crt_item<last_item
1742 // Tag all next children, when no more children,
1743 // Move to parent (not to tag)
1744 // Keep going... if we found last_item, we stop.
1745 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1747 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1749 if (parent
== NULL
) // This is root item
1750 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1752 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1753 int index
= children
.Index(crt_item
);
1754 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1756 size_t count
= children
.GetCount();
1757 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1759 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1762 return TagNextChildren(parent
, last_item
, select
);
1765 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1767 crt_item
->SetHilight(select
);
1768 RefreshLine(crt_item
);
1770 if (crt_item
==last_item
)
1773 if (crt_item
->HasChildren())
1775 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1776 size_t count
= children
.GetCount();
1777 for ( size_t n
= 0; n
< count
; ++n
)
1779 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1787 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1791 // item2 is not necessary after item1
1792 // choice first' and 'last' between item1 and item2
1793 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1794 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1796 bool select
= m_current
->IsSelected();
1798 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1801 TagNextChildren(first
,last
,select
);
1804 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1805 bool unselect_others
,
1806 bool extended_select
)
1808 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1812 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1813 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1815 //wxCHECK_RET( ( (!unselect_others) && is_single),
1816 // wxT("this is a single selection tree") );
1818 // to keep going anyhow !!!
1821 if (item
->IsSelected())
1822 return; // nothing to do
1823 unselect_others
= true;
1824 extended_select
= false;
1826 else if ( unselect_others
&& item
->IsSelected() )
1828 // selection change if there is more than one item currently selected
1829 wxArrayTreeItemIds selected_items
;
1830 if ( GetSelections(selected_items
) == 1 )
1834 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1835 event
.m_itemOld
= m_current
;
1836 // TODO : Here we don't send any selection mode yet !
1838 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1841 wxTreeItemId parent
= GetItemParent( itemId
);
1842 while (parent
.IsOk())
1844 if (!IsExpanded(parent
))
1847 parent
= GetItemParent( parent
);
1851 if (unselect_others
)
1853 if (is_single
) Unselect(); // to speed up thing
1858 if (extended_select
)
1862 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1865 // don't change the mark (m_current)
1866 SelectItemRange(m_current
, item
);
1870 bool select
= true; // the default
1872 // Check if we need to toggle hilight (ctrl mode)
1873 if (!unselect_others
)
1874 select
=!item
->IsSelected();
1876 m_current
= m_key_current
= item
;
1877 m_current
->SetHilight(select
);
1878 RefreshLine( m_current
);
1881 // This can cause idle processing to select the root
1882 // if no item is selected, so it must be after the
1884 EnsureVisible( itemId
);
1886 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1887 GetEventHandler()->ProcessEvent( event
);
1890 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1894 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
1898 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1899 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1901 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1902 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1905 item
->SetHilight(false);
1908 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1909 GetEventHandler()->ProcessEvent( event
);
1913 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1914 wxArrayTreeItemIds
&array
) const
1916 if ( item
->IsSelected() )
1917 array
.Add(wxTreeItemId(item
));
1919 if ( item
->HasChildren() )
1921 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1922 size_t count
= children
.GetCount();
1923 for ( size_t n
= 0; n
< count
; ++n
)
1924 FillArray(children
[n
], array
);
1928 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1931 wxTreeItemId idRoot
= GetRootItem();
1932 if ( idRoot
.IsOk() )
1934 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1936 //else: the tree is empty, so no selections
1938 return array
.GetCount();
1941 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1943 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1945 if (!item
.IsOk()) return;
1947 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1949 // first expand all parent branches
1950 wxGenericTreeItem
*parent
= gitem
->GetParent();
1952 if ( HasFlag(wxTR_HIDE_ROOT
) )
1954 while ( parent
&& parent
!= m_anchor
)
1957 parent
= parent
->GetParent();
1965 parent
= parent
->GetParent();
1969 //if (parent) CalculatePositions();
1974 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1976 if (!item
.IsOk()) return;
1978 // We have to call this here because the label in
1979 // question might just have been added and no screen
1980 // update taken place.
1982 #if defined( __WXMSW__ ) || defined(__WXMAC__)
1985 DoDirtyProcessing();
1987 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1989 // now scroll to the item
1990 int item_y
= gitem
->GetY();
1994 GetViewStart( &start_x
, &start_y
);
1995 start_y
*= PIXELS_PER_UNIT
;
1999 GetClientSize( &client_w
, &client_h
);
2001 if (item_y
< start_y
+3)
2006 m_anchor
->GetSize( x
, y
, this );
2007 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2008 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2009 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2010 // Item should appear at top
2011 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2013 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2018 m_anchor
->GetSize( x
, y
, this );
2019 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2020 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2021 item_y
+= PIXELS_PER_UNIT
+2;
2022 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2023 // Item should appear at bottom
2024 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
);
2028 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2029 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2031 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2032 wxGenericTreeItem
**item2
)
2034 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2036 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2039 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2041 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2043 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2045 wxCHECK_RET( !s_treeBeingSorted
,
2046 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2048 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2049 if ( children
.GetCount() > 1 )
2053 s_treeBeingSorted
= this;
2054 children
.Sort(tree_ctrl_compare_func
);
2055 s_treeBeingSorted
= NULL
;
2057 //else: don't make the tree dirty as nothing changed
2060 void wxGenericTreeCtrl::CalculateLineHeight()
2062 wxClientDC
dc(this);
2063 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2065 if ( m_imageListNormal
)
2067 // Calculate a m_lineHeight value from the normal Image sizes.
2068 // May be toggle off. Then wxGenericTreeCtrl will spread when
2069 // necessary (which might look ugly).
2070 int n
= m_imageListNormal
->GetImageCount();
2071 for (int i
= 0; i
< n
; i
++)
2073 int width
= 0, height
= 0;
2074 m_imageListNormal
->GetSize(i
, width
, height
);
2075 if (height
> m_lineHeight
) m_lineHeight
= height
;
2079 if (m_imageListButtons
)
2081 // Calculate a m_lineHeight value from the Button image sizes.
2082 // May be toggle off. Then wxGenericTreeCtrl will spread when
2083 // necessary (which might look ugly).
2084 int n
= m_imageListButtons
->GetImageCount();
2085 for (int i
= 0; i
< n
; i
++)
2087 int width
= 0, height
= 0;
2088 m_imageListButtons
->GetSize(i
, width
, height
);
2089 if (height
> m_lineHeight
) m_lineHeight
= height
;
2093 if (m_lineHeight
< 30)
2094 m_lineHeight
+= 2; // at least 2 pixels
2096 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2099 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2101 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2102 m_imageListNormal
= imageList
;
2103 m_ownsImageListNormal
= false;
2105 // Don't do any drawing if we're setting the list to NULL,
2106 // since we may be in the process of deleting the tree control.
2108 CalculateLineHeight();
2111 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2113 if (m_ownsImageListState
) delete m_imageListState
;
2114 m_imageListState
= imageList
;
2115 m_ownsImageListState
= false;
2118 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2120 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2121 m_imageListButtons
= imageList
;
2122 m_ownsImageListButtons
= false;
2124 CalculateLineHeight();
2127 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2129 SetButtonsImageList(imageList
);
2130 m_ownsImageListButtons
= true;
2133 // -----------------------------------------------------------------------------
2135 // -----------------------------------------------------------------------------
2137 void wxGenericTreeCtrl::AdjustMyScrollbars()
2142 m_anchor
->GetSize( x
, y
, this );
2143 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2144 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2145 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2146 int y_pos
= GetScrollPos( wxVERTICAL
);
2147 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2151 SetScrollbars( 0, 0, 0, 0 );
2155 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2157 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2158 return item
->GetHeight();
2160 return m_lineHeight
;
2163 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2165 // TODO implement "state" icon on items
2167 wxTreeItemAttr
*attr
= item
->GetAttributes();
2168 if ( attr
&& attr
->HasFont() )
2169 dc
.SetFont(attr
->GetFont());
2170 else if (item
->IsBold())
2171 dc
.SetFont(m_boldFont
);
2173 wxCoord text_w
= 0, text_h
= 0;
2174 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2176 int image_h
= 0, image_w
= 0;
2177 int image
= item
->GetCurrentImage();
2178 if ( image
!= NO_IMAGE
)
2180 if ( m_imageListNormal
)
2182 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2183 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2191 int total_h
= GetLineHeight(item
);
2192 bool drawItemBackground
= false;
2194 if ( item
->IsSelected() )
2196 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2197 drawItemBackground
= true;
2202 if ( attr
&& attr
->HasBackgroundColour() )
2204 drawItemBackground
= true;
2205 colBg
= attr
->GetBackgroundColour();
2209 colBg
= GetBackgroundColour();
2211 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2214 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2216 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2220 GetVirtualSize(&w
, &h
);
2221 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2222 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2223 dc
.DrawRectangle(rect
);
2225 if (!item
->IsSelected())
2227 dc
.DrawRectangle(rect
);
2231 int flags
= wxCONTROL_SELECTED
;
2233 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
2234 && IsControlActive( (ControlRef
)GetHandle() )
2237 flags
|= wxCONTROL_FOCUSED
;
2238 if ((item
== m_current
) && (m_hasFocus
))
2239 flags
|= wxCONTROL_CURRENT
;
2240 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2246 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2248 // If it's selected, and there's an image, then we should
2249 // take care to leave the area under the image painted in the
2250 // background colour.
2251 wxRect
rect( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2252 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2253 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2254 dc
.DrawRectangle( rect
);
2259 int flags
= wxCONTROL_SELECTED
;
2261 flags
|= wxCONTROL_FOCUSED
;
2262 if ((item
== m_current
) && (m_hasFocus
))
2263 flags
|= wxCONTROL_CURRENT
;
2264 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2267 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2268 // don't allow backgrounds to be customized. Not drawing the background,
2269 // except for custom item backgrounds, works for both kinds of theme.
2270 else if (drawItemBackground
)
2272 wxRect
rect( item
->GetX()-2, item
->GetY()+offset
,
2273 item
->GetWidth()+2, total_h
-offset
);
2274 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2275 dc
.DrawRectangle( rect
);
2277 if ( attr
&& attr
->HasBackgroundColour() )
2279 dc
.DrawRectangle( rect
);
2286 int flags
= wxCONTROL_SELECTED
;
2288 flags
|= wxCONTROL_FOCUSED
;
2289 if ((item
== m_current
) && (m_hasFocus
))
2290 flags
|= wxCONTROL_CURRENT
;
2291 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2297 if ( image
!= NO_IMAGE
)
2299 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2300 m_imageListNormal
->Draw( image
, dc
,
2302 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2303 wxIMAGELIST_DRAW_TRANSPARENT
);
2304 dc
.DestroyClippingRegion();
2307 dc
.SetBackgroundMode(wxTRANSPARENT
);
2308 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2309 dc
.DrawText( item
->GetText(),
2310 (wxCoord
)(image_w
+ item
->GetX()),
2311 (wxCoord
)(item
->GetY() + extraH
));
2313 // restore normal font
2314 dc
.SetFont( m_normalFont
);
2317 // Now y stands for the top of the item, whereas it used to stand for middle !
2318 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2320 int x
= level
*m_indent
;
2321 if (!HasFlag(wxTR_HIDE_ROOT
))
2325 else if (level
== 0)
2327 // always expand hidden root
2329 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2330 int count
= children
.GetCount();
2336 PaintLevel(children
[n
], dc
, 1, y
);
2337 } while (++n
< count
);
2339 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2341 // draw line down to last child
2342 origY
+= GetLineHeight(children
[0])>>1;
2343 oldY
+= GetLineHeight(children
[n
-1])>>1;
2344 dc
.DrawLine(3, origY
, 3, oldY
);
2350 item
->SetX(x
+m_spacing
);
2353 int h
= GetLineHeight(item
);
2355 int y_mid
= y_top
+ (h
>>1);
2358 int exposed_x
= dc
.LogicalToDeviceX(0);
2359 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2361 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2365 // don't draw rect outline if we already have the
2366 // background color under Mac
2367 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2368 #endif // !__WXMAC__
2372 if ( item
->IsSelected()
2373 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
2374 // On wxMac, if the tree doesn't have the focus we draw an empty
2375 // rectangle, so we want to make sure that the text is visible
2376 // against the normal background, not the highlightbackground, so
2377 // don't use the highlight text colour unless we have the focus.
2378 && m_hasFocus
&& IsControlActive( (ControlRef
)GetHandle() )
2385 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2390 wxTreeItemAttr
*attr
= item
->GetAttributes();
2391 if (attr
&& attr
->HasTextColour())
2392 colText
= attr
->GetTextColour();
2394 colText
= GetForegroundColour();
2398 dc
.SetTextForeground(colText
);
2402 PaintItem(item
, dc
);
2404 if (HasFlag(wxTR_ROW_LINES
))
2406 // if the background colour is white, choose a
2407 // contrasting color for the lines
2408 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2409 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2410 dc
.DrawLine(0, y_top
, 10000, y_top
);
2411 dc
.DrawLine(0, y
, 10000, y
);
2414 // restore DC objects
2415 dc
.SetBrush(*wxWHITE_BRUSH
);
2416 dc
.SetPen(m_dottedPen
);
2417 dc
.SetTextForeground(*wxBLACK
);
2419 if ( !HasFlag(wxTR_NO_LINES
) )
2421 // draw the horizontal line here
2423 if (x
> (signed)m_indent
)
2424 x_start
-= m_indent
;
2425 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2427 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2430 // should the item show a button?
2431 if ( item
->HasPlus() && HasButtons() )
2433 if ( m_imageListButtons
)
2435 // draw the image button here
2438 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2439 : wxTreeItemIcon_Normal
;
2440 if ( item
->IsSelected() )
2441 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2443 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2444 int xx
= x
- image_w
/2;
2445 int yy
= y_mid
- image_h
/2;
2447 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2448 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2449 wxIMAGELIST_DRAW_TRANSPARENT
);
2451 else // no custom buttons
2453 static const int wImage
= 9;
2454 static const int hImage
= 9;
2457 if (item
->IsExpanded())
2458 flag
|= wxCONTROL_EXPANDED
;
2459 if (item
== m_underMouse
)
2460 flag
|= wxCONTROL_CURRENT
;
2462 wxRendererNative::Get().DrawTreeItemButton
2466 wxRect(x
- wImage
/2,
2475 if (item
->IsExpanded())
2477 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2478 int count
= children
.GetCount();
2485 PaintLevel(children
[n
], dc
, level
, y
);
2486 } while (++n
< count
);
2488 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2490 // draw line down to last child
2491 oldY
+= GetLineHeight(children
[n
-1])>>1;
2492 if (HasButtons()) y_mid
+= 5;
2494 // Only draw the portion of the line that is visible, in case it is huge
2495 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2496 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2497 yOrigin
= abs(yOrigin
);
2498 GetClientSize(&width
, &height
);
2500 // Move end points to the begining/end of the view?
2501 if (y_mid
< yOrigin
)
2503 if (oldY
> yOrigin
+ height
)
2504 oldY
= yOrigin
+ height
;
2506 // after the adjustments if y_mid is larger than oldY then the line
2507 // isn't visible at all so don't draw anything
2509 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2515 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2519 if ( item
->HasPlus() )
2521 // it's a folder, indicate it by a border
2526 // draw a line under the drop target because the item will be
2528 DrawLine(item
, !m_dropEffectAboveItem
);
2531 SetCursor(wxCURSOR_BULLSEYE
);
2536 SetCursor(wxCURSOR_NO_ENTRY
);
2540 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2542 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2544 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2546 wxClientDC
dc(this);
2548 dc
.SetLogicalFunction(wxINVERT
);
2549 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2551 int w
= i
->GetWidth() + 2;
2552 int h
= GetLineHeight(i
) + 2;
2554 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2557 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2559 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2561 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2563 wxClientDC
dc(this);
2565 dc
.SetLogicalFunction(wxINVERT
);
2571 y
+= GetLineHeight(i
) - 1;
2574 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2577 // -----------------------------------------------------------------------------
2578 // wxWidgets callbacks
2579 // -----------------------------------------------------------------------------
2581 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
2584 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
2585 RefreshLine( m_current
);
2591 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2599 dc
.SetFont( m_normalFont
);
2600 dc
.SetPen( m_dottedPen
);
2602 // this is now done dynamically
2603 //if(GetImageList() == NULL)
2604 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2607 PaintLevel( m_anchor
, dc
, 0, y
);
2610 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2619 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2628 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2630 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2631 te
.m_evtKey
= event
;
2632 if ( GetEventHandler()->ProcessEvent( te
) )
2634 // intercepted by the user code
2638 if ( (m_current
== 0) || (m_key_current
== 0) )
2644 // how should the selection work for this event?
2645 bool is_multiple
, extended_select
, unselect_others
;
2646 EventFlagsToSelType(GetWindowStyleFlag(),
2649 is_multiple
, extended_select
, unselect_others
);
2651 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2653 if (event
.GetKeyCode() == WXK_RIGHT
)
2654 event
.m_keyCode
= WXK_LEFT
;
2655 else if (event
.GetKeyCode() == WXK_LEFT
)
2656 event
.m_keyCode
= WXK_RIGHT
;
2661 // * : Expand all/Collapse all
2662 // ' ' | return : activate
2663 // up : go up (not last children!)
2665 // left : go to parent
2666 // right : open if parent and go next
2667 // home : go to root
2668 // end : go to last item without opening parents
2669 // alnum : start or continue searching for the item with this prefix
2670 int keyCode
= event
.GetKeyCode();
2675 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2683 if ( !IsExpanded(m_current
) )
2686 ExpandAllChildren(m_current
);
2689 //else: fall through to Collapse() it
2693 if (IsExpanded(m_current
))
2695 Collapse(m_current
);
2701 // Use the item's bounding rectangle to determine position for the event
2703 GetBoundingRect(m_current
, ItemRect
, true);
2705 wxTreeEvent
eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU
, this, m_current
);
2706 // Use the left edge, vertical middle
2707 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
2708 ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2709 GetEventHandler()->ProcessEvent( eventMenu
);
2715 if ( !event
.HasModifiers() )
2717 wxTreeEvent
eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, m_current
);
2718 GetEventHandler()->ProcessEvent( eventAct
);
2721 // in any case, also generate the normal key event for this key,
2722 // even if we generated the ACTIVATED event above: this is what
2723 // wxMSW does and it makes sense because you might not want to
2724 // process ACTIVATED event at all and handle Space and Return
2725 // directly (and differently) which would be impossible otherwise
2729 // up goes to the previous sibling or to the last
2730 // of its children if it's expanded
2733 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2736 prev
= GetItemParent( m_key_current
);
2737 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2739 break; // don't go to root if it is hidden
2743 wxTreeItemIdValue cookie
;
2744 wxTreeItemId current
= m_key_current
;
2745 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2746 if (current
== GetFirstChild( prev
, cookie
))
2748 // otherwise we return to where we came from
2749 DoSelectItem( prev
, unselect_others
, extended_select
);
2750 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2757 while ( IsExpanded(prev
) && HasChildren(prev
) )
2759 wxTreeItemId child
= GetLastChild(prev
);
2766 DoSelectItem( prev
, unselect_others
, extended_select
);
2767 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2772 // left arrow goes to the parent
2775 wxTreeItemId prev
= GetItemParent( m_current
);
2776 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2778 // don't go to root if it is hidden
2779 prev
= GetPrevSibling( m_current
);
2783 DoSelectItem( prev
, unselect_others
, extended_select
);
2789 // this works the same as the down arrow except that we
2790 // also expand the item if it wasn't expanded yet
2791 if (m_current
!= GetRootItem().m_pItem
|| !HasFlag(wxTR_HIDE_ROOT
))
2793 //else: don't try to expand hidden root item (which can be the
2794 // current one when the tree is empty)
2800 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2802 wxTreeItemIdValue cookie
;
2803 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2807 DoSelectItem( child
, unselect_others
, extended_select
);
2808 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2812 wxTreeItemId next
= GetNextSibling( m_key_current
);
2815 wxTreeItemId current
= m_key_current
;
2816 while (current
.IsOk() && !next
)
2818 current
= GetItemParent( current
);
2819 if (current
) next
= GetNextSibling( current
);
2824 DoSelectItem( next
, unselect_others
, extended_select
);
2825 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2831 // <End> selects the last visible tree item
2834 wxTreeItemId last
= GetRootItem();
2836 while ( last
.IsOk() && IsExpanded(last
) )
2838 wxTreeItemId lastChild
= GetLastChild(last
);
2840 // it may happen if the item was expanded but then all of
2841 // its children have been deleted - so IsExpanded() returned
2842 // true, but GetLastChild() returned invalid item
2851 DoSelectItem( last
, unselect_others
, extended_select
);
2856 // <Home> selects the root item
2859 wxTreeItemId prev
= GetRootItem();
2863 if ( HasFlag(wxTR_HIDE_ROOT
) )
2865 wxTreeItemIdValue cookie
;
2866 prev
= GetFirstChild(prev
, cookie
);
2871 DoSelectItem( prev
, unselect_others
, extended_select
);
2876 // do not use wxIsalnum() here
2877 if ( !event
.HasModifiers() &&
2878 ((keyCode
>= '0' && keyCode
<= '9') ||
2879 (keyCode
>= 'a' && keyCode
<= 'z') ||
2880 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2882 // find the next item starting with the given prefix
2883 wxChar ch
= (wxChar
)keyCode
;
2885 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2896 // also start the timer to reset the current prefix if the user
2897 // doesn't press any more alnum keys soon -- we wouldn't want
2898 // to use this prefix for a new item search
2901 m_findTimer
= new wxTreeFindTimer(this);
2904 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2914 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2919 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2920 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2921 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2922 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2923 if (flags
) return wxTreeItemId();
2925 if (m_anchor
== NULL
)
2927 flags
= wxTREE_HITTEST_NOWHERE
;
2928 return wxTreeItemId();
2931 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2935 flags
= wxTREE_HITTEST_NOWHERE
;
2936 return wxTreeItemId();
2941 // get the bounding rectangle of the item (or of its label only)
2942 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2944 bool textOnly
) const
2946 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2948 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2953 rect
.width
= i
->GetWidth();
2955 if ( m_imageListNormal
)
2957 int image_w
, image_h
;
2958 m_imageListNormal
->GetSize( 0, image_w
, image_h
);
2959 rect
.width
+= image_w
+ MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2962 else // the entire line
2965 rect
.width
= GetClientSize().x
;
2969 rect
.height
= GetLineHeight(i
);
2971 // we have to return the logical coordinates, not physical ones
2972 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
2977 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
2978 wxClassInfo
* WXUNUSED(textCtrlClass
))
2980 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
2982 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2984 wxTreeEvent
te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
2985 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2991 // We have to call this here because the label in
2992 // question might just have been added and no screen
2993 // update taken place.
2995 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2998 DoDirtyProcessing();
3001 // TODO: use textCtrlClass here to create the control of correct class
3002 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
3004 m_textCtrl
->SetFocus();
3009 // returns a pointer to the text edit control if the item is being
3010 // edited, NULL otherwise (it's assumed that no more than one item may
3011 // be edited simultaneously)
3012 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3017 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3018 bool discardChanges
)
3020 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
3022 m_textCtrl
->EndEdit(discardChanges
);
3025 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3026 const wxString
& value
)
3028 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3030 le
.m_editCancelled
= false;
3032 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3035 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3037 // let owner know that the edit was cancelled
3038 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3039 le
.m_label
= wxEmptyString
;
3040 le
.m_editCancelled
= true;
3042 GetEventHandler()->ProcessEvent( le
);
3045 void wxGenericTreeCtrl::OnRenameTimer()
3047 EditLabel( m_current
);
3050 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3052 if ( !m_anchor
)return;
3054 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3056 // Is the mouse over a tree item button?
3058 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3059 wxGenericTreeItem
*underMouse
= thisItem
;
3061 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3062 #endif // wxUSE_TOOLTIPS
3065 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3066 (!event
.LeftIsDown()) &&
3068 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3076 if (underMouse
!= m_underMouse
)
3080 // unhighlight old item
3081 wxGenericTreeItem
*tmp
= m_underMouse
;
3082 m_underMouse
= NULL
;
3086 m_underMouse
= underMouse
;
3088 RefreshLine( m_underMouse
);
3092 // Determines what item we are hovering over and need a tooltip for
3093 wxTreeItemId hoverItem
= thisItem
;
3095 // We do not want a tooltip if we are dragging, or if the rename timer is running
3096 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3098 // Ask the tree control what tooltip (if any) should be shown
3099 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3101 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3103 SetToolTip(hevent
.m_label
);
3108 // we process left mouse up event (enables in-place edit), middle/right down
3109 // (pass to the user code), left dbl click (activate item) and
3110 // dragging/moving events for items drag-and-drop
3111 if ( !(event
.LeftDown() ||
3113 event
.MiddleDown() ||
3114 event
.RightDown() ||
3115 event
.LeftDClick() ||
3117 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3126 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3128 if ( event
.Dragging() && !m_isDragging
)
3130 if (m_dragCount
== 0)
3135 if (m_dragCount
!= 3)
3137 // wait until user drags a bit further...
3141 wxEventType command
= event
.RightIsDown()
3142 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3143 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3145 wxTreeEvent
nevent(command
, this, m_current
);
3146 nevent
.SetPoint(CalcScrolledPosition(pt
));
3148 // by default the dragging is not supported, the user code must
3149 // explicitly allow the event for it to take place
3152 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3154 // we're going to drag this item
3155 m_isDragging
= true;
3157 // remember the old cursor because we will change it while
3159 m_oldCursor
= m_cursor
;
3161 // in a single selection control, hide the selection temporarily
3162 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3164 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3166 if ( m_oldSelection
)
3168 m_oldSelection
->SetHilight(false);
3169 RefreshLine(m_oldSelection
);
3176 else if ( event
.Dragging() )
3178 if ( item
!= m_dropTarget
)
3180 // unhighlight the previous drop target
3181 DrawDropEffect(m_dropTarget
);
3183 m_dropTarget
= item
;
3185 // highlight the current drop target if any
3186 DrawDropEffect(m_dropTarget
);
3188 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3195 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3199 // erase the highlighting
3200 DrawDropEffect(m_dropTarget
);
3202 if ( m_oldSelection
)
3204 m_oldSelection
->SetHilight(true);
3205 RefreshLine(m_oldSelection
);
3206 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3209 // generate the drag end event
3210 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, this, item
);
3212 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3214 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3216 m_isDragging
= false;
3217 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3219 SetCursor(m_oldCursor
);
3221 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3229 // If we got to this point, we are not dragging or moving the mouse.
3230 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3231 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3232 // We skip even if we didn't hit an item because we still should
3233 // restore focus to the tree control even if we didn't exactly hit an item.
3234 if ( event
.LeftDown() )
3239 // here we process only the messages which happen on tree items
3243 if (item
== NULL
) return; /* we hit the blank area */
3245 if ( event
.RightDown() )
3247 // If the item is already selected, do not update the selection.
3248 // Multi-selections should not be cleared if a selected item is clicked.
3249 if (!IsSelected(item
))
3251 DoSelectItem(item
, true, false);
3254 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, this, item
);
3255 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3256 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3258 // Consistent with MSW (for now), send the ITEM_MENU *after*
3259 // the RIGHT_CLICK event. TODO: This behavior may change.
3260 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
3261 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3262 GetEventHandler()->ProcessEvent(nevent2
);
3264 else if ( event
.MiddleDown() )
3266 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
, this, item
);
3267 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3268 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3270 else if ( event
.LeftUp() )
3272 // this facilitates multiple-item drag-and-drop
3274 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3276 wxArrayTreeItemIds selections
;
3277 size_t count
= GetSelections(selections
);
3283 DoSelectItem(item
, true, false);
3289 if ( (item
== m_current
) &&
3290 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3291 HasFlag(wxTR_EDIT_LABELS
) )
3293 if ( m_renameTimer
)
3295 if ( m_renameTimer
->IsRunning() )
3296 m_renameTimer
->Stop();
3300 m_renameTimer
= new wxTreeRenameTimer( this );
3303 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3306 m_lastOnSame
= false;
3309 else // !RightDown() && !MiddleDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3311 if ( event
.LeftDown() )
3313 m_lastOnSame
= item
== m_current
;
3316 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3318 // only toggle the item for a single click, double click on
3319 // the button doesn't do anything (it toggles the item twice)
3320 if ( event
.LeftDown() )
3325 // don't select the item if the button was clicked
3330 // clear the previously selected items, if the
3331 // user clicked outside of the present selection.
3332 // otherwise, perform the deselection on mouse-up.
3333 // this allows multiple drag and drop to work.
3334 // but if Cmd is down, toggle selection of the clicked item
3335 if (!IsSelected(item
) || event
.CmdDown())
3337 // how should the selection work for this event?
3338 bool is_multiple
, extended_select
, unselect_others
;
3339 EventFlagsToSelType(GetWindowStyleFlag(),
3342 is_multiple
, extended_select
, unselect_others
);
3344 DoSelectItem(item
, unselect_others
, extended_select
);
3348 // For some reason, Windows isn't recognizing a left double-click,
3349 // so we need to simulate it here. Allow 200 milliseconds for now.
3350 if ( event
.LeftDClick() )
3352 // double clicking should not start editing the item label
3353 if ( m_renameTimer
)
3354 m_renameTimer
->Stop();
3356 m_lastOnSame
= false;
3358 // send activate event first
3359 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, item
);
3360 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3361 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3363 // if the user code didn't process the activate event,
3364 // handle it ourselves by toggling the item when it is
3366 if ( item
->HasPlus() )
3376 void wxGenericTreeCtrl::OnInternalIdle()
3378 wxWindow::OnInternalIdle();
3380 // Check if we need to select the root item
3381 // because nothing else has been selected.
3382 // Delaying it means that we can invoke event handlers
3383 // as required, when a first item is selected.
3384 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3387 SelectItem(m_select_me
);
3388 else if (GetRootItem().IsOk())
3389 SelectItem(GetRootItem());
3392 // after all changes have been done to the tree control,
3393 // actually redraw the tree when everything is over
3395 DoDirtyProcessing();
3398 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3403 wxTreeItemAttr
*attr
= item
->GetAttributes();
3404 if ( attr
&& attr
->HasFont() )
3405 dc
.SetFont(attr
->GetFont());
3406 else if ( item
->IsBold() )
3407 dc
.SetFont(m_boldFont
);
3409 dc
.SetFont(m_normalFont
);
3411 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3414 // restore normal font
3415 dc
.SetFont( m_normalFont
);
3419 int image
= item
->GetCurrentImage();
3420 if ( image
!= NO_IMAGE
)
3422 if ( m_imageListNormal
)
3424 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3425 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3429 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3432 total_h
+= 2; // at least 2 pixels
3434 total_h
+= total_h
/10; // otherwise 10% extra spacing
3436 item
->SetHeight(total_h
);
3437 if (total_h
>m_lineHeight
)
3438 m_lineHeight
=total_h
;
3440 item
->SetWidth(image_w
+text_w
+2);
3443 // -----------------------------------------------------------------------------
3444 // for developper : y is now the top of the level
3445 // not the middle of it !
3446 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3448 int x
= level
*m_indent
;
3449 if (!HasFlag(wxTR_HIDE_ROOT
))
3453 else if (level
== 0)
3455 // a hidden root is not evaluated, but its
3456 // children are always calculated
3460 CalculateSize( item
, dc
);
3463 item
->SetX( x
+m_spacing
);
3465 y
+= GetLineHeight(item
);
3467 if ( !item
->IsExpanded() )
3469 // we don't need to calculate collapsed branches
3474 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3475 size_t n
, count
= children
.GetCount();
3477 for (n
= 0; n
< count
; ++n
)
3478 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3481 void wxGenericTreeCtrl::CalculatePositions()
3483 if ( !m_anchor
) return;
3485 wxClientDC
dc(this);
3488 dc
.SetFont( m_normalFont
);
3490 dc
.SetPen( m_dottedPen
);
3491 //if(GetImageList() == NULL)
3492 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3495 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3498 void wxGenericTreeCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
3501 wxTreeCtrlBase::Refresh(eraseBackground
, rect
);
3504 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3506 if (m_dirty
|| IsFrozen() )
3509 wxSize client
= GetClientSize();
3512 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3513 rect
.width
= client
.x
;
3514 rect
.height
= client
.y
;
3516 Refresh(true, &rect
);
3518 AdjustMyScrollbars();
3521 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3523 if (m_dirty
|| IsFrozen() )
3527 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3528 rect
.width
= GetClientSize().x
;
3529 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3531 Refresh(true, &rect
);
3534 void wxGenericTreeCtrl::RefreshSelected()
3539 // TODO: this is awfully inefficient, we should keep the list of all
3540 // selected items internally, should be much faster
3542 RefreshSelectedUnder(m_anchor
);
3545 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3550 if ( item
->IsSelected() )
3553 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3554 size_t count
= children
.GetCount();
3555 for ( size_t n
= 0; n
< count
; n
++ )
3557 RefreshSelectedUnder(children
[n
]);
3561 void wxGenericTreeCtrl::DoThaw()
3564 DoDirtyProcessing();
3569 // ----------------------------------------------------------------------------
3570 // changing colours: we need to refresh the tree control
3571 // ----------------------------------------------------------------------------
3573 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3575 if ( !wxWindow::SetBackgroundColour(colour
) )
3583 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3585 if ( !wxWindow::SetForegroundColour(colour
) )
3593 // Process the tooltip event, to speed up event processing.
3594 // Doesn't actually get a tooltip.
3595 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3601 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3602 // be removed, as well as the #else case below.
3603 #define _USE_VISATTR 0
3608 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3610 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3614 // Use the same color scheme as wxListBox
3615 return wxListBox::GetClassDefaultAttributes(variant
);
3617 wxVisualAttributes attr
;
3618 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3619 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3620 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3625 void wxGenericTreeCtrl::DoDirtyProcessing()
3632 CalculatePositions();
3634 AdjustMyScrollbars();
3637 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
3639 // make sure all positions are calculated as normally this only done during
3640 // idle time but we need them for base class DoGetBestSize() to return the
3642 wxConstCast(this, wxGenericTreeCtrl
)->CalculatePositions();
3644 wxSize size
= wxTreeCtrlBase::DoGetBestSize();
3646 // there seems to be an implicit extra border around the items, although
3647 // I'm not really sure where does it come from -- but without this, the
3648 // scrollbars appear in a tree with default/best size
3651 // and the border has to be rounded up to a multiple of PIXELS_PER_UNIT or
3652 // scrollbars still appear
3653 const wxSize
& borderSize
= GetWindowBorderSize();
3655 int dx
= (size
.x
- borderSize
.x
) % PIXELS_PER_UNIT
;
3657 size
.x
+= PIXELS_PER_UNIT
- dx
;
3658 int dy
= (size
.y
- borderSize
.y
) % PIXELS_PER_UNIT
;
3660 size
.y
+= PIXELS_PER_UNIT
- dy
;
3662 // we need to update the cache too as the base class cached its own value
3663 CacheBestSize(size
);
3668 #endif // wxUSE_TREECTRL