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 WXDLLEXPORT 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
= false)
103 m_aboutToFinish
= true;
105 // Notify the owner about the changes
108 // Even if vetoed, close the control (consistent with MSW)
116 m_owner
->OnRenameCancelled(m_itemEdited
);
118 const wxGenericTreeItem
* item() const { return m_itemEdited
; }
121 void OnChar( wxKeyEvent
&event
);
122 void OnKeyUp( wxKeyEvent
&event
);
123 void OnKillFocus( wxFocusEvent
&event
);
125 bool AcceptChanges();
129 wxGenericTreeCtrl
*m_owner
;
130 wxGenericTreeItem
*m_itemEdited
;
131 wxString m_startValue
;
133 bool m_aboutToFinish
;
135 DECLARE_EVENT_TABLE()
136 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
139 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
140 // for a sufficiently long time
141 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
144 // reset the current prefix after half a second of inactivity
145 enum { DELAY
= 500 };
147 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
149 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
152 wxGenericTreeCtrl
*m_owner
;
154 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
158 class WXDLLEXPORT wxGenericTreeItem
162 wxGenericTreeItem() { m_data
= NULL
; }
163 wxGenericTreeItem( wxGenericTreeItem
*parent
,
164 const wxString
& text
,
167 wxTreeItemData
*data
);
169 ~wxGenericTreeItem();
172 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
174 const wxString
& GetText() const { return m_text
; }
175 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
176 { return m_images
[which
]; }
177 wxTreeItemData
*GetData() const { return m_data
; }
179 // returns the current image for the item (depending on its
180 // selected/expanded/whatever state)
181 int GetCurrentImage() const;
183 void SetText( const wxString
&text
);
184 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
185 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
187 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
189 void SetBold(bool bold
) { m_isBold
= bold
; }
191 int GetX() const { return m_x
; }
192 int GetY() const { return m_y
; }
194 void SetX(int x
) { m_x
= x
; }
195 void SetY(int y
) { m_y
= y
; }
197 int GetHeight() const { return m_height
; }
198 int GetWidth() const { return m_width
; }
200 void SetHeight(int h
) { m_height
= h
; }
201 void SetWidth(int w
) { m_width
= w
; }
203 wxGenericTreeItem
*GetParent() const { return m_parent
; }
207 // deletes all children notifying the treectrl about it
208 void DeleteChildren(wxGenericTreeCtrl
*tree
);
210 // get count of all children (and grand children if 'recursively')
211 size_t GetChildrenCount(bool recursively
= true) const;
213 void Insert(wxGenericTreeItem
*child
, size_t index
)
214 { m_children
.Insert(child
, index
); }
216 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
218 // return the item at given position (or NULL if no item), onButton is
219 // true if the point belongs to the item's button, otherwise it lies
220 // on the item's label
221 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
222 const wxGenericTreeCtrl
*,
226 void Expand() { m_isCollapsed
= false; }
227 void Collapse() { m_isCollapsed
= true; }
229 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
232 bool HasChildren() const { return !m_children
.IsEmpty(); }
233 bool IsSelected() const { return m_hasHilight
!= 0; }
234 bool IsExpanded() const { return !m_isCollapsed
; }
235 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
236 bool IsBold() const { return m_isBold
!= 0; }
239 // get them - may be NULL
240 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
241 // get them ensuring that the pointer is not NULL
242 wxTreeItemAttr
& Attr()
246 m_attr
= new wxTreeItemAttr
;
252 void SetAttributes(wxTreeItemAttr
*attr
)
254 if ( m_ownsAttr
) delete m_attr
;
258 // set them and delete when done
259 void AssignAttributes(wxTreeItemAttr
*attr
)
266 // since there can be very many of these, we save size by chosing
267 // the smallest representation for the elements and by ordering
268 // the members to avoid padding.
269 wxString m_text
; // label to be rendered for item
271 wxTreeItemData
*m_data
; // user-provided data
273 wxArrayGenericTreeItems m_children
; // list of children
274 wxGenericTreeItem
*m_parent
; // parent of this item
276 wxTreeItemAttr
*m_attr
; // attributes???
278 // tree ctrl images for the normal, selected, expanded and
279 // expanded+selected states
280 int m_images
[wxTreeItemIcon_Max
];
282 wxCoord m_x
; // (virtual) offset from top
283 wxCoord m_y
; // (virtual) offset from left
284 int m_width
; // width of this item
285 int m_height
; // height of this item
287 // use bitfields to save size
288 unsigned int m_isCollapsed
:1;
289 unsigned int m_hasHilight
:1; // same as focused
290 unsigned int m_hasPlus
:1; // used for item which doesn't have
291 // children but has a [+] button
292 unsigned int m_isBold
:1; // render the label in bold font
293 unsigned int m_ownsAttr
:1; // delete attribute when done
295 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
298 // =============================================================================
300 // =============================================================================
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
306 // translate the key or mouse event flags to the type of selection we're
308 static void EventFlagsToSelType(long style
,
312 bool &extended_select
,
313 bool &unselect_others
)
315 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
316 extended_select
= shiftDown
&& is_multiple
;
317 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
320 // check if the given item is under another one
321 static bool IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
325 if ( item
== parent
)
327 // item is a descendant of parent
331 item
= item
->GetParent();
337 // -----------------------------------------------------------------------------
338 // wxTreeRenameTimer (internal)
339 // -----------------------------------------------------------------------------
341 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
346 void wxTreeRenameTimer::Notify()
348 m_owner
->OnRenameTimer();
351 //-----------------------------------------------------------------------------
352 // wxTreeTextCtrl (internal)
353 //-----------------------------------------------------------------------------
355 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
356 EVT_CHAR (wxTreeTextCtrl::OnChar
)
357 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
358 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
361 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
362 wxGenericTreeItem
*item
)
363 : m_itemEdited(item
), m_startValue(item
->GetText())
367 m_aboutToFinish
= false;
369 int w
= m_itemEdited
->GetWidth(),
370 h
= m_itemEdited
->GetHeight();
373 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
378 int image
= item
->GetCurrentImage();
379 if ( image
!= NO_IMAGE
)
381 if ( m_owner
->m_imageListNormal
)
383 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
384 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
388 wxFAIL_MSG(_T("you must create an image list to use images!"));
392 // FIXME: what are all these hardcoded 4, 8 and 11s really?
396 wxSize bs
= DoGetBestSize() ;
397 // edit control height
400 int diff
= h
- ( bs
.y
- 8 ) ;
406 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
407 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
410 bool wxTreeTextCtrl::AcceptChanges()
412 const wxString value
= GetValue();
414 if ( value
== m_startValue
)
416 // nothing changed, always accept
417 // when an item remains unchanged, the owner
418 // needs to be notified that the user decided
419 // not to change the tree item label, and that
420 // the edit has been cancelled
422 m_owner
->OnRenameCancelled(m_itemEdited
);
426 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
428 // vetoed by the user
432 // accepted, do rename the item
433 m_owner
->SetItemText(m_itemEdited
, value
);
438 void wxTreeTextCtrl::Finish()
442 m_owner
->ResetTextControl();
444 wxPendingDelete
.Append(this);
452 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
454 switch ( event
.m_keyCode
)
469 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
473 // auto-grow the textctrl:
474 wxSize parentSize
= m_owner
->GetSize();
475 wxPoint myPos
= GetPosition();
476 wxSize mySize
= GetSize();
478 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
479 if (myPos
.x
+ sx
> parentSize
.x
)
480 sx
= parentSize
.x
- myPos
.x
;
483 SetSize(sx
, wxDefaultCoord
);
489 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
491 if ( !m_finished
&& !m_aboutToFinish
)
493 // We must finish regardless of success, otherwise we'll get
497 if ( !AcceptChanges() )
498 m_owner
->OnRenameCancelled( m_itemEdited
);
501 // We must let the native text control handle focus, too, otherwise
502 // it could have problems with the cursor (e.g., in wxGTK).
506 // -----------------------------------------------------------------------------
508 // -----------------------------------------------------------------------------
510 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
511 const wxString
& text
,
512 int image
, int selImage
,
513 wxTreeItemData
*data
)
516 m_images
[wxTreeItemIcon_Normal
] = image
;
517 m_images
[wxTreeItemIcon_Selected
] = selImage
;
518 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
519 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
524 m_isCollapsed
= true;
525 m_hasHilight
= false;
531 m_attr
= (wxTreeItemAttr
*)NULL
;
534 // We don't know the height here yet.
539 wxGenericTreeItem::~wxGenericTreeItem()
543 if (m_ownsAttr
) delete m_attr
;
545 wxASSERT_MSG( m_children
.IsEmpty(),
546 wxT("please call DeleteChildren() before deleting the item") );
549 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
551 size_t count
= m_children
.Count();
552 for ( size_t n
= 0; n
< count
; n
++ )
554 wxGenericTreeItem
*child
= m_children
[n
];
555 tree
->SendDeleteEvent(child
);
557 child
->DeleteChildren(tree
);
558 if ( child
== tree
->m_select_me
)
559 tree
->m_select_me
= NULL
;
566 void wxGenericTreeItem::SetText( const wxString
&text
)
571 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
573 size_t count
= m_children
.Count();
577 size_t total
= count
;
578 for (size_t n
= 0; n
< count
; ++n
)
580 total
+= m_children
[n
]->GetChildrenCount();
586 void wxGenericTreeItem::GetSize( int &x
, int &y
,
587 const wxGenericTreeCtrl
*theButton
)
589 int bottomY
=m_y
+theButton
->GetLineHeight(this);
590 if ( y
< bottomY
) y
= bottomY
;
591 int width
= m_x
+ m_width
;
592 if ( x
< width
) x
= width
;
596 size_t count
= m_children
.Count();
597 for ( size_t n
= 0; n
< count
; ++n
)
599 m_children
[n
]->GetSize( x
, y
, theButton
);
604 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
605 const wxGenericTreeCtrl
*theCtrl
,
609 // for a hidden root node, don't evaluate it, but do evaluate children
610 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
613 int h
= theCtrl
->GetLineHeight(this);
614 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
616 int y_mid
= m_y
+ h
/2;
617 if (point
.y
< y_mid
)
618 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
620 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
622 int xCross
= m_x
- theCtrl
->GetSpacing();
624 // according to the drawing code the triangels are drawn
625 // at -4 , -4 from the position up to +10/+10 max
626 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
627 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
628 HasPlus() && theCtrl
->HasButtons() )
630 // 5 is the size of the plus sign
631 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
632 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
633 HasPlus() && theCtrl
->HasButtons() )
636 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
640 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
645 // assuming every image (normal and selected) has the same size!
646 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
647 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
650 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
651 flags
|= wxTREE_HITTEST_ONITEMICON
;
653 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
659 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
660 if (point
.x
> m_x
+m_width
)
661 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
666 // if children are expanded, fall through to evaluate them
667 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
671 size_t count
= m_children
.Count();
672 for ( size_t n
= 0; n
< count
; n
++ )
674 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
682 return (wxGenericTreeItem
*) NULL
;
685 int wxGenericTreeItem::GetCurrentImage() const
687 int image
= NO_IMAGE
;
692 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
695 if ( image
== NO_IMAGE
)
697 // we usually fall back to the normal item, but try just the
698 // expanded one (and not selected) first in this case
699 image
= GetImage(wxTreeItemIcon_Expanded
);
705 image
= GetImage(wxTreeItemIcon_Selected
);
708 // maybe it doesn't have the specific image we want,
709 // try the default one instead
710 if ( image
== NO_IMAGE
) image
= GetImage();
715 // -----------------------------------------------------------------------------
716 // wxGenericTreeCtrl implementation
717 // -----------------------------------------------------------------------------
719 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxControl
)
721 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
, wxTreeCtrlBase
)
722 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
723 EVT_SIZE (wxGenericTreeCtrl::OnSize
)
724 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
725 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
726 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
727 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
728 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
731 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
733 * wxTreeCtrl has to be a real class or we have problems with
734 * the run-time information.
737 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
740 // -----------------------------------------------------------------------------
741 // construction/destruction
742 // -----------------------------------------------------------------------------
744 void wxGenericTreeCtrl::Init()
749 m_select_me
= (wxGenericTreeItem
*) NULL
;
758 // OS X sel item highlight color differs from text highlight color, which is
759 // what wxSYS_COLOUR_HIGHLIGHT returns.
761 GetThemeBrushAsColor(kThemeBrushAlternatePrimaryHighlightColor
, 32, true, &hilight
);
762 m_hilightBrush
= new wxBrush( wxColour(hilight
.red
, hilight
.green
, hilight
.blue
), wxSOLID
);
764 m_hilightBrush
= new wxBrush
766 wxSystemSettings::GetColour
768 wxSYS_COLOUR_HIGHLIGHT
775 // on Mac, this color also differs from the wxSYS_COLOUR_BTNSHADOW, enough to be noticable.
776 // I don't know if BTNSHADOW is appropriate in other contexts, so I'm just changing it here.
777 GetThemeBrushAsColor(kThemeBrushSecondaryHighlightColor
, 32, true, &hilight
);
778 m_hilightUnfocusedBrush
= new wxBrush( wxColour(hilight
.red
, hilight
.green
, hilight
.blue
), wxSOLID
);
780 m_hilightUnfocusedBrush
= new wxBrush
782 wxSystemSettings::GetColour
784 wxSYS_COLOUR_BTNSHADOW
789 m_imageListButtons
= NULL
;
790 m_ownsImageListButtons
= false;
793 m_isDragging
= false;
794 m_dropTarget
= m_oldSelection
= NULL
;
798 m_renameTimer
= NULL
;
803 m_dropEffectAboveItem
= false;
805 m_lastOnSame
= false;
807 #ifdef __WXMAC_CARBON__
808 m_normalFont
.MacCreateThemeFont( kThemeViewsFont
) ;
810 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
812 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
813 m_normalFont
.GetFamily(),
814 m_normalFont
.GetStyle(),
816 m_normalFont
.GetUnderlined(),
817 m_normalFont
.GetFaceName(),
818 m_normalFont
.GetEncoding());
821 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
826 const wxValidator
& validator
,
827 const wxString
& name
)
831 wxGetOsVersion( &major
, &minor
);
833 style
&= ~wxTR_LINES_AT_ROOT
;
834 style
|= wxTR_NO_LINES
;
836 style
|= wxTR_ROW_LINES
;
838 if (style
== 0 || style
& wxTR_DEFAULT_STYLE
)
839 style
|= wxTR_FULL_ROW_HIGHLIGHT
;
843 style
|= wxTR_NO_LINES
;
846 if ( !wxControl::Create( parent
, id
, pos
, size
,
847 style
|wxHSCROLL
|wxVSCROLL
,
852 // If the tree display has no buttons, but does have
853 // connecting lines, we can use a narrower layout.
854 // It may not be a good idea to force this...
855 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
861 wxVisualAttributes attr
= GetDefaultAttributes();
862 SetOwnForegroundColour( attr
.colFg
);
863 SetOwnBackgroundColour( attr
.colBg
);
865 SetOwnFont(attr
.font
);
867 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
869 SetInitialSize(size
);
874 wxGenericTreeCtrl::~wxGenericTreeCtrl()
876 delete m_hilightBrush
;
877 delete m_hilightUnfocusedBrush
;
881 delete m_renameTimer
;
884 if (m_ownsImageListButtons
)
885 delete m_imageListButtons
;
888 // -----------------------------------------------------------------------------
890 // -----------------------------------------------------------------------------
892 unsigned int wxGenericTreeCtrl::GetCount() const
900 unsigned int count
= m_anchor
->GetChildrenCount();
901 if ( !HasFlag(wxTR_HIDE_ROOT
) )
903 // take the root itself into account
910 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
912 m_indent
= (unsigned short) indent
;
917 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
918 bool recursively
) const
920 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
922 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
925 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
927 // Do not try to expand the root node if it hasn't been created yet
928 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
930 // if we will hide the root, make sure children are visible
931 m_anchor
->SetHasPlus();
933 CalculatePositions();
936 // right now, just sets the styles. Eventually, we may
937 // want to update the inherited styles, but right now
938 // none of the parents has updatable styles
939 m_windowStyle
= styles
;
943 // -----------------------------------------------------------------------------
944 // functions to work with tree items
945 // -----------------------------------------------------------------------------
947 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
949 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
951 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
954 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
955 wxTreeItemIcon which
) const
957 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
959 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
962 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
964 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
966 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
969 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
971 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
973 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
974 return pItem
->Attr().GetTextColour();
977 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
979 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
981 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
982 return pItem
->Attr().GetBackgroundColour();
985 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
987 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
989 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
990 return pItem
->Attr().GetFont();
993 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
995 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
998 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
999 pItem
->SetText(text
);
1000 CalculateSize(pItem
, dc
);
1004 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
1006 wxTreeItemIcon which
)
1008 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1010 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1011 pItem
->SetImage(image
, which
);
1013 wxClientDC
dc(this);
1014 CalculateSize(pItem
, dc
);
1018 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1020 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1023 data
->SetId( item
);
1025 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1028 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1030 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1032 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1033 pItem
->SetHasPlus(has
);
1037 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1039 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1041 // avoid redrawing the tree if no real change
1042 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1043 if ( pItem
->IsBold() != bold
)
1045 pItem
->SetBold(bold
);
1047 // recalculate the item size as bold and non bold fonts have different
1049 wxClientDC
dc(this);
1050 CalculateSize(pItem
, dc
);
1056 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1059 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1065 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1066 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1069 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1070 pItem
->Attr().SetTextColour(fg
);
1071 pItem
->Attr().SetBackgroundColour(bg
);
1075 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1076 const wxColour
& col
)
1078 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1080 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1081 pItem
->Attr().SetTextColour(col
);
1085 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1086 const wxColour
& col
)
1088 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1090 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1091 pItem
->Attr().SetBackgroundColour(col
);
1095 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1097 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1099 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1100 pItem
->Attr().SetFont(font
);
1104 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1106 wxTreeCtrlBase::SetFont(font
);
1108 m_normalFont
= font
;
1109 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1110 m_normalFont
.GetFamily(),
1111 m_normalFont
.GetStyle(),
1113 m_normalFont
.GetUnderlined(),
1114 m_normalFont
.GetFaceName(),
1115 m_normalFont
.GetEncoding());
1121 // -----------------------------------------------------------------------------
1122 // item status inquiries
1123 // -----------------------------------------------------------------------------
1125 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1127 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1129 // An item is only visible if it's not a descendant of a collapsed item
1130 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1131 wxGenericTreeItem
* parent
= pItem
->GetParent();
1134 if (!parent
->IsExpanded())
1136 parent
= parent
->GetParent();
1140 GetViewStart(& startX
, & startY
);
1142 wxSize clientSize
= GetClientSize();
1145 if (!GetBoundingRect(item
, rect
))
1147 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1149 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1151 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1157 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1159 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1161 // consider that the item does have children if it has the "+" button: it
1162 // might not have them (if it had never been expanded yet) but then it
1163 // could have them as well and it's better to err on this side rather than
1164 // disabling some operations which are restricted to the items with
1165 // children for an item which does have them
1166 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1169 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1171 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1173 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1176 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1178 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1180 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1183 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1185 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1187 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1190 // -----------------------------------------------------------------------------
1192 // -----------------------------------------------------------------------------
1194 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1196 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1198 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1201 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1202 wxTreeItemIdValue
& cookie
) const
1204 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1207 return GetNextChild(item
, cookie
);
1210 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1211 wxTreeItemIdValue
& cookie
) const
1213 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1215 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1217 // it's ok to cast cookie to size_t, we never have indices big enough to
1218 // overflow "void *"
1219 size_t *pIndex
= (size_t *)&cookie
;
1220 if ( *pIndex
< children
.Count() )
1222 return children
.Item((*pIndex
)++);
1226 // there are no more of them
1227 return wxTreeItemId();
1231 #if WXWIN_COMPATIBILITY_2_4
1233 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1236 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1239 return GetNextChild(item
, cookie
);
1242 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1245 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1247 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1248 if ( (size_t)cookie
< children
.Count() )
1250 return children
.Item((size_t)cookie
++);
1254 // there are no more of them
1255 return wxTreeItemId();
1259 #endif // WXWIN_COMPATIBILITY_2_4
1261 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1263 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1265 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1266 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1269 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1271 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1273 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1274 wxGenericTreeItem
*parent
= i
->GetParent();
1275 if ( parent
== NULL
)
1277 // root item doesn't have any siblings
1278 return wxTreeItemId();
1281 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1282 int index
= siblings
.Index(i
);
1283 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1285 size_t n
= (size_t)(index
+ 1);
1286 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1289 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1291 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1293 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1294 wxGenericTreeItem
*parent
= i
->GetParent();
1295 if ( parent
== NULL
)
1297 // root item doesn't have any siblings
1298 return wxTreeItemId();
1301 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1302 int index
= siblings
.Index(i
);
1303 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1305 return index
== 0 ? wxTreeItemId()
1306 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1309 // Only for internal use right now, but should probably be public
1310 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1312 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1314 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1316 // First see if there are any children.
1317 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1318 if (children
.GetCount() > 0)
1320 return children
.Item(0);
1324 // Try a sibling of this or ancestor instead
1325 wxTreeItemId p
= item
;
1326 wxTreeItemId toFind
;
1329 toFind
= GetNextSibling(p
);
1330 p
= GetItemParent(p
);
1331 } while (p
.IsOk() && !toFind
.IsOk());
1336 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1338 wxTreeItemId id
= GetRootItem();
1347 } while (id
.IsOk());
1349 return wxTreeItemId();
1352 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1354 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1356 wxTreeItemId id
= item
;
1359 while (id
= GetNext(id
), id
.IsOk())
1365 return wxTreeItemId();
1368 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1370 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1372 wxFAIL_MSG(wxT("not implemented"));
1374 return wxTreeItemId();
1377 // called by wxTextTreeCtrl when it marks itself for deletion
1378 void wxGenericTreeCtrl::ResetTextControl()
1383 // find the first item starting with the given prefix after the given item
1384 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1385 const wxString
& prefixOrig
) const
1387 // match is case insensitive as this is more convenient to the user: having
1388 // to press Shift-letter to go to the item starting with a capital letter
1389 // would be too bothersome
1390 wxString prefix
= prefixOrig
.Lower();
1392 // determine the starting point: we shouldn't take the current item (this
1393 // allows to switch between two items starting with the same letter just by
1394 // pressing it) but we shouldn't jump to the next one if the user is
1395 // continuing to type as otherwise he might easily skip the item he wanted
1396 wxTreeItemId id
= idParent
;
1397 if ( prefix
.length() == 1 )
1402 // look for the item starting with the given prefix after it
1403 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1408 // if we haven't found anything...
1411 // ... wrap to the beginning
1413 if ( HasFlag(wxTR_HIDE_ROOT
) )
1415 // can't select virtual root
1419 // and try all the items (stop when we get to the one we started from)
1420 while (id
.IsOk() && id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1424 // If we haven't found the item, id.IsOk() will be false, as per
1431 // -----------------------------------------------------------------------------
1433 // -----------------------------------------------------------------------------
1435 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1437 const wxString
& text
,
1440 wxTreeItemData
*data
)
1442 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1445 // should we give a warning here?
1446 return AddRoot(text
, image
, selImage
, data
);
1449 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1451 wxGenericTreeItem
*item
=
1452 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1456 data
->m_pItem
= item
;
1459 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1465 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1468 wxTreeItemData
*data
)
1470 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1472 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1474 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1475 image
, selImage
, data
);
1478 data
->m_pItem
= m_anchor
;
1481 if (HasFlag(wxTR_HIDE_ROOT
))
1483 // if root is hidden, make sure we can navigate
1485 m_anchor
->SetHasPlus();
1487 CalculatePositions();
1490 if (!HasFlag(wxTR_MULTIPLE
))
1492 m_current
= m_key_current
= m_anchor
;
1493 m_current
->SetHilight( true );
1499 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1500 const wxTreeItemId
& idPrevious
,
1501 const wxString
& text
,
1502 int image
, int selImage
,
1503 wxTreeItemData
*data
)
1505 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1508 // should we give a warning here?
1509 return AddRoot(text
, image
, selImage
, data
);
1513 if (idPrevious
.IsOk())
1515 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1516 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1517 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1520 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1524 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1526 wxTreeEvent
event(wxEVT_COMMAND_TREE_DELETE_ITEM
, this, item
);
1527 ProcessEvent( event
);
1530 // Don't leave edit or selection on a child which is about to disappear
1531 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1533 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1534 m_textCtrl
->StopEditing();
1536 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1537 m_key_current
= NULL
;
1539 if (IsDescendantOf(item
, m_select_me
)) {
1542 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1543 m_current
->SetHilight( false );
1549 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1551 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1553 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1554 ChildrenClosing(item
);
1555 item
->DeleteChildren(this);
1558 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1560 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1562 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1564 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1566 // can't delete the item being edited, cancel editing it first
1567 m_textCtrl
->StopEditing();
1570 wxGenericTreeItem
*parent
= item
->GetParent();
1572 // don't keep stale pointers around!
1573 if ( IsDescendantOf(item
, m_key_current
) )
1575 // Don't silently change the selection:
1576 // do it properly in idle time, so event
1577 // handlers get called.
1579 // m_key_current = parent;
1580 m_key_current
= NULL
;
1583 // m_select_me records whether we need to select
1584 // a different item, in idle time.
1585 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1587 m_select_me
= parent
;
1590 if ( IsDescendantOf(item
, m_current
) )
1592 // Don't silently change the selection:
1593 // do it properly in idle time, so event
1594 // handlers get called.
1596 // m_current = parent;
1598 m_select_me
= parent
;
1601 // remove the item from the tree
1604 parent
->GetChildren().Remove( item
); // remove by value
1606 else // deleting the root
1608 // nothing will be left in the tree
1612 // and delete all of its children and the item itself now
1613 item
->DeleteChildren(this);
1614 SendDeleteEvent(item
);
1616 if (item
== m_select_me
)
1622 void wxGenericTreeCtrl::DeleteAllItems()
1630 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1632 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1634 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1635 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1636 _T("can't expand hidden root") );
1638 if ( !item
->HasPlus() )
1641 if ( item
->IsExpanded() )
1644 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_EXPANDING
, this, item
);
1646 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1648 // cancelled by program
1653 CalculatePositions();
1655 RefreshSubtree(item
);
1657 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1658 ProcessEvent( event
);
1661 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1663 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1664 _T("can't collapse hidden root") );
1666 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1668 if ( !item
->IsExpanded() )
1671 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, this, item
);
1672 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1674 // cancelled by program
1678 ChildrenClosing(item
);
1681 #if 0 // TODO why should items be collapsed recursively?
1682 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1683 size_t count
= children
.Count();
1684 for ( size_t n
= 0; n
< count
; n
++ )
1686 Collapse(children
[n
]);
1690 CalculatePositions();
1692 RefreshSubtree(item
);
1694 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1695 ProcessEvent( event
);
1698 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1701 DeleteChildren(item
);
1704 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1706 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1708 if (item
->IsExpanded())
1714 void wxGenericTreeCtrl::Unselect()
1718 m_current
->SetHilight( false );
1719 RefreshLine( m_current
);
1726 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1728 if (item
->IsSelected())
1730 item
->SetHilight(false);
1734 if (item
->HasChildren())
1736 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1737 size_t count
= children
.Count();
1738 for ( size_t n
= 0; n
< count
; ++n
)
1740 UnselectAllChildren(children
[n
]);
1745 void wxGenericTreeCtrl::UnselectAll()
1747 wxTreeItemId rootItem
= GetRootItem();
1749 // the tree might not have the root item at all
1752 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1756 // Recursive function !
1757 // To stop we must have crt_item<last_item
1759 // Tag all next children, when no more children,
1760 // Move to parent (not to tag)
1761 // Keep going... if we found last_item, we stop.
1762 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1764 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1766 if (parent
== NULL
) // This is root item
1767 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1769 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1770 int index
= children
.Index(crt_item
);
1771 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1773 size_t count
= children
.Count();
1774 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1776 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1779 return TagNextChildren(parent
, last_item
, select
);
1782 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1784 crt_item
->SetHilight(select
);
1785 RefreshLine(crt_item
);
1787 if (crt_item
==last_item
)
1790 if (crt_item
->HasChildren())
1792 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1793 size_t count
= children
.Count();
1794 for ( size_t n
= 0; n
< count
; ++n
)
1796 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1804 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1808 // item2 is not necessary after item1
1809 // choice first' and 'last' between item1 and item2
1810 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1811 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1813 bool select
= m_current
->IsSelected();
1815 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1818 TagNextChildren(first
,last
,select
);
1821 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1822 bool unselect_others
,
1823 bool extended_select
)
1825 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1829 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1830 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1832 //wxCHECK_RET( ( (!unselect_others) && is_single),
1833 // wxT("this is a single selection tree") );
1835 // to keep going anyhow !!!
1838 if (item
->IsSelected())
1839 return; // nothing to do
1840 unselect_others
= true;
1841 extended_select
= false;
1843 else if ( unselect_others
&& item
->IsSelected() )
1845 // selection change if there is more than one item currently selected
1846 wxArrayTreeItemIds selected_items
;
1847 if ( GetSelections(selected_items
) == 1 )
1851 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1852 event
.m_itemOld
= m_current
;
1853 // TODO : Here we don't send any selection mode yet !
1855 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1858 wxTreeItemId parent
= GetItemParent( itemId
);
1859 while (parent
.IsOk())
1861 if (!IsExpanded(parent
))
1864 parent
= GetItemParent( parent
);
1868 if (unselect_others
)
1870 if (is_single
) Unselect(); // to speed up thing
1875 if (extended_select
)
1879 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1882 // don't change the mark (m_current)
1883 SelectItemRange(m_current
, item
);
1887 bool select
= true; // the default
1889 // Check if we need to toggle hilight (ctrl mode)
1890 if (!unselect_others
)
1891 select
=!item
->IsSelected();
1893 m_current
= m_key_current
= item
;
1894 m_current
->SetHilight(select
);
1895 RefreshLine( m_current
);
1898 // This can cause idle processing to select the root
1899 // if no item is selected, so it must be after the
1901 EnsureVisible( itemId
);
1903 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1904 GetEventHandler()->ProcessEvent( event
);
1907 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1911 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
1915 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1916 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1918 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1919 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1922 item
->SetHilight(false);
1925 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1926 GetEventHandler()->ProcessEvent( event
);
1930 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1931 wxArrayTreeItemIds
&array
) const
1933 if ( item
->IsSelected() )
1934 array
.Add(wxTreeItemId(item
));
1936 if ( item
->HasChildren() )
1938 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1939 size_t count
= children
.GetCount();
1940 for ( size_t n
= 0; n
< count
; ++n
)
1941 FillArray(children
[n
], array
);
1945 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1948 wxTreeItemId idRoot
= GetRootItem();
1949 if ( idRoot
.IsOk() )
1951 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1953 //else: the tree is empty, so no selections
1955 return array
.Count();
1958 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1960 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1962 if (!item
.IsOk()) return;
1964 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1966 // first expand all parent branches
1967 wxGenericTreeItem
*parent
= gitem
->GetParent();
1969 if ( HasFlag(wxTR_HIDE_ROOT
) )
1971 while ( parent
&& parent
!= m_anchor
)
1974 parent
= parent
->GetParent();
1982 parent
= parent
->GetParent();
1986 //if (parent) CalculatePositions();
1991 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1993 if (!item
.IsOk()) return;
1995 // We have to call this here because the label in
1996 // question might just have been added and no screen
1997 // update taken place.
1999 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2002 DoDirtyProcessing();
2004 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2006 // now scroll to the item
2007 int item_y
= gitem
->GetY();
2011 GetViewStart( &start_x
, &start_y
);
2012 start_y
*= PIXELS_PER_UNIT
;
2016 GetClientSize( &client_w
, &client_h
);
2018 if (item_y
< start_y
+3)
2023 m_anchor
->GetSize( x
, y
, this );
2024 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2025 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2026 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2027 // Item should appear at top
2028 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2030 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2035 m_anchor
->GetSize( x
, y
, this );
2036 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2037 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2038 item_y
+= PIXELS_PER_UNIT
+2;
2039 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2040 // Item should appear at bottom
2041 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
);
2045 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2046 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2048 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2049 wxGenericTreeItem
**item2
)
2051 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2053 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2056 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2058 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2060 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2062 wxCHECK_RET( !s_treeBeingSorted
,
2063 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2065 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2066 if ( children
.Count() > 1 )
2070 s_treeBeingSorted
= this;
2071 children
.Sort(tree_ctrl_compare_func
);
2072 s_treeBeingSorted
= NULL
;
2074 //else: don't make the tree dirty as nothing changed
2077 void wxGenericTreeCtrl::CalculateLineHeight()
2079 wxClientDC
dc(this);
2080 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2082 if ( m_imageListNormal
)
2084 // Calculate a m_lineHeight value from the normal Image sizes.
2085 // May be toggle off. Then wxGenericTreeCtrl will spread when
2086 // necessary (which might look ugly).
2087 int n
= m_imageListNormal
->GetImageCount();
2088 for (int i
= 0; i
< n
; i
++)
2090 int width
= 0, height
= 0;
2091 m_imageListNormal
->GetSize(i
, width
, height
);
2092 if (height
> m_lineHeight
) m_lineHeight
= height
;
2096 if (m_imageListButtons
)
2098 // Calculate a m_lineHeight value from the Button image sizes.
2099 // May be toggle off. Then wxGenericTreeCtrl will spread when
2100 // necessary (which might look ugly).
2101 int n
= m_imageListButtons
->GetImageCount();
2102 for (int i
= 0; i
< n
; i
++)
2104 int width
= 0, height
= 0;
2105 m_imageListButtons
->GetSize(i
, width
, height
);
2106 if (height
> m_lineHeight
) m_lineHeight
= height
;
2110 if (m_lineHeight
< 30)
2111 m_lineHeight
+= 2; // at least 2 pixels
2113 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2116 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2118 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2119 m_imageListNormal
= imageList
;
2120 m_ownsImageListNormal
= false;
2122 // Don't do any drawing if we're setting the list to NULL,
2123 // since we may be in the process of deleting the tree control.
2125 CalculateLineHeight();
2128 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2130 if (m_ownsImageListState
) delete m_imageListState
;
2131 m_imageListState
= imageList
;
2132 m_ownsImageListState
= false;
2135 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2137 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2138 m_imageListButtons
= imageList
;
2139 m_ownsImageListButtons
= false;
2141 CalculateLineHeight();
2144 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2146 SetButtonsImageList(imageList
);
2147 m_ownsImageListButtons
= true;
2150 // -----------------------------------------------------------------------------
2152 // -----------------------------------------------------------------------------
2154 void wxGenericTreeCtrl::AdjustMyScrollbars()
2159 m_anchor
->GetSize( x
, y
, this );
2160 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2161 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2162 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2163 int y_pos
= GetScrollPos( wxVERTICAL
);
2164 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2168 SetScrollbars( 0, 0, 0, 0 );
2172 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2174 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2175 return item
->GetHeight();
2177 return m_lineHeight
;
2180 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2182 // TODO implement "state" icon on items
2184 wxTreeItemAttr
*attr
= item
->GetAttributes();
2185 if ( attr
&& attr
->HasFont() )
2186 dc
.SetFont(attr
->GetFont());
2187 else if (item
->IsBold())
2188 dc
.SetFont(m_boldFont
);
2190 long text_w
= 0, text_h
= 0;
2191 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2193 int image_h
= 0, image_w
= 0;
2194 int image
= item
->GetCurrentImage();
2195 if ( image
!= NO_IMAGE
)
2197 if ( m_imageListNormal
)
2199 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2200 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2208 int total_h
= GetLineHeight(item
);
2209 bool drawItemBackground
= false;
2211 if ( item
->IsSelected() )
2213 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2214 drawItemBackground
= true;
2219 if ( attr
&& attr
->HasBackgroundColour() )
2221 drawItemBackground
= true;
2222 colBg
= attr
->GetBackgroundColour();
2226 colBg
= GetBackgroundColour();
2228 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2231 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2233 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2237 GetVirtualSize(&w
, &h
);
2238 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2240 dc
.DrawRectangle(rect
);
2242 if (!item
->IsSelected())
2244 dc
.DrawRectangle(rect
);
2248 int flags
= wxCONTROL_SELECTED
;
2250 flags
|= wxCONTROL_FOCUSED
;
2251 if ((item
== m_current
) && (m_hasFocus
))
2252 flags
|= wxCONTROL_CURRENT
;
2253 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2259 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2261 // If it's selected, and there's an image, then we should
2262 // take care to leave the area under the image painted in the
2263 // background colour.
2264 wxRect
rect( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2265 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2267 dc
.DrawRectangle( rect
);
2272 int flags
= wxCONTROL_SELECTED
;
2274 flags
|= wxCONTROL_FOCUSED
;
2275 if ((item
== m_current
) && (m_hasFocus
))
2276 flags
|= wxCONTROL_CURRENT
;
2277 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2280 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2281 // don't allow backgrounds to be customized. Not drawing the background,
2282 // except for custom item backgrounds, works for both kinds of theme.
2283 else if (drawItemBackground
)
2285 wxRect
rect( item
->GetX()-2, item
->GetY()+offset
,
2286 item
->GetWidth()+2, total_h
-offset
);
2288 dc
.DrawRectangle( rect
);
2290 if ( attr
&& attr
->HasBackgroundColour() )
2292 dc
.DrawRectangle( rect
);
2299 int flags
= wxCONTROL_SELECTED
;
2301 flags
|= wxCONTROL_FOCUSED
;
2302 if ((item
== m_current
) && (m_hasFocus
))
2303 flags
|= wxCONTROL_CURRENT
;
2304 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2310 if ( image
!= NO_IMAGE
)
2312 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2313 m_imageListNormal
->Draw( image
, dc
,
2315 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2316 wxIMAGELIST_DRAW_TRANSPARENT
);
2317 dc
.DestroyClippingRegion();
2320 dc
.SetBackgroundMode(wxTRANSPARENT
);
2321 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2322 dc
.DrawText( item
->GetText(),
2323 (wxCoord
)(image_w
+ item
->GetX()),
2324 (wxCoord
)(item
->GetY() + extraH
));
2326 // restore normal font
2327 dc
.SetFont( m_normalFont
);
2330 // Now y stands for the top of the item, whereas it used to stand for middle !
2331 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2333 int x
= level
*m_indent
;
2334 if (!HasFlag(wxTR_HIDE_ROOT
))
2338 else if (level
== 0)
2340 // always expand hidden root
2342 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2343 int count
= children
.Count();
2349 PaintLevel(children
[n
], dc
, 1, y
);
2350 } while (++n
< count
);
2352 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2354 // draw line down to last child
2355 origY
+= GetLineHeight(children
[0])>>1;
2356 oldY
+= GetLineHeight(children
[n
-1])>>1;
2357 dc
.DrawLine(3, origY
, 3, oldY
);
2363 item
->SetX(x
+m_spacing
);
2366 int h
= GetLineHeight(item
);
2368 int y_mid
= y_top
+ (h
>>1);
2371 int exposed_x
= dc
.LogicalToDeviceX(0);
2372 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2374 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2378 // don't draw rect outline if we already have the
2379 // background color under Mac
2380 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2381 #endif // !__WXMAC__
2385 if ( item
->IsSelected()
2387 // On wxMac, if the tree doesn't have the focus we draw an empty
2388 // rectangle, so we want to make sure that the text is visible
2389 // against the normal background, not the highlightbackground, so
2390 // don't use the highlight text colour unless we have the focus.
2398 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2403 wxTreeItemAttr
*attr
= item
->GetAttributes();
2404 if (attr
&& attr
->HasTextColour())
2405 colText
= attr
->GetTextColour();
2407 colText
= GetForegroundColour();
2411 dc
.SetTextForeground(colText
);
2415 PaintItem(item
, dc
);
2417 if (HasFlag(wxTR_ROW_LINES
))
2419 // if the background colour is white, choose a
2420 // contrasting color for the lines
2421 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2422 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2423 dc
.DrawLine(0, y_top
, 10000, y_top
);
2424 dc
.DrawLine(0, y
, 10000, y
);
2427 // restore DC objects
2428 dc
.SetBrush(*wxWHITE_BRUSH
);
2429 dc
.SetPen(m_dottedPen
);
2430 dc
.SetTextForeground(*wxBLACK
);
2432 if ( !HasFlag(wxTR_NO_LINES
) )
2434 // draw the horizontal line here
2436 if (x
> (signed)m_indent
)
2437 x_start
-= m_indent
;
2438 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2440 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2443 // should the item show a button?
2444 if ( item
->HasPlus() && HasButtons() )
2446 if ( m_imageListButtons
)
2448 // draw the image button here
2451 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2452 : wxTreeItemIcon_Normal
;
2453 if ( item
->IsSelected() )
2454 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2456 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2457 int xx
= x
- image_w
/2;
2458 int yy
= y_mid
- image_h
/2;
2460 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2461 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2462 wxIMAGELIST_DRAW_TRANSPARENT
);
2464 else // no custom buttons
2466 static const int wImage
= 9;
2467 static const int hImage
= 9;
2470 if (item
->IsExpanded())
2471 flag
|= wxCONTROL_EXPANDED
;
2472 if (item
== m_underMouse
)
2473 flag
|= wxCONTROL_CURRENT
;
2475 wxRendererNative::Get().DrawTreeItemButton
2479 wxRect(x
- wImage
/2,
2488 if (item
->IsExpanded())
2490 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2491 int count
= children
.Count();
2498 PaintLevel(children
[n
], dc
, level
, y
);
2499 } while (++n
< count
);
2501 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2503 // draw line down to last child
2504 oldY
+= GetLineHeight(children
[n
-1])>>1;
2505 if (HasButtons()) y_mid
+= 5;
2507 // Only draw the portion of the line that is visible, in case it is huge
2508 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2509 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2510 yOrigin
= abs(yOrigin
);
2511 GetClientSize(&width
, &height
);
2513 // Move end points to the begining/end of the view?
2514 if (y_mid
< yOrigin
)
2516 if (oldY
> yOrigin
+ height
)
2517 oldY
= yOrigin
+ height
;
2519 // after the adjustments if y_mid is larger than oldY then the line
2520 // isn't visible at all so don't draw anything
2522 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2528 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2532 if ( item
->HasPlus() )
2534 // it's a folder, indicate it by a border
2539 // draw a line under the drop target because the item will be
2541 DrawLine(item
, !m_dropEffectAboveItem
);
2544 SetCursor(wxCURSOR_BULLSEYE
);
2549 SetCursor(wxCURSOR_NO_ENTRY
);
2553 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2555 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2557 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2559 wxClientDC
dc(this);
2561 dc
.SetLogicalFunction(wxINVERT
);
2562 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2564 int w
= i
->GetWidth() + 2;
2565 int h
= GetLineHeight(i
) + 2;
2567 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2570 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2572 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2574 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2576 wxClientDC
dc(this);
2578 dc
.SetLogicalFunction(wxINVERT
);
2584 y
+= GetLineHeight(i
) - 1;
2587 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2590 // -----------------------------------------------------------------------------
2591 // wxWidgets callbacks
2592 // -----------------------------------------------------------------------------
2594 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
2597 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
2598 RefreshLine( m_current
);
2604 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2612 dc
.SetFont( m_normalFont
);
2613 dc
.SetPen( m_dottedPen
);
2615 // this is now done dynamically
2616 //if(GetImageList() == NULL)
2617 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2620 PaintLevel( m_anchor
, dc
, 0, y
);
2623 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2632 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2641 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2643 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2644 te
.m_evtKey
= event
;
2645 if ( GetEventHandler()->ProcessEvent( te
) )
2647 // intercepted by the user code
2651 if ( (m_current
== 0) || (m_key_current
== 0) )
2657 // how should the selection work for this event?
2658 bool is_multiple
, extended_select
, unselect_others
;
2659 EventFlagsToSelType(GetWindowStyleFlag(),
2662 is_multiple
, extended_select
, unselect_others
);
2664 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2666 if (event
.GetKeyCode() == WXK_RIGHT
)
2667 event
.m_keyCode
= WXK_LEFT
;
2668 else if (event
.GetKeyCode() == WXK_LEFT
)
2669 event
.m_keyCode
= WXK_RIGHT
;
2674 // * : Expand all/Collapse all
2675 // ' ' | return : activate
2676 // up : go up (not last children!)
2678 // left : go to parent
2679 // right : open if parent and go next
2680 // home : go to root
2681 // end : go to last item without opening parents
2682 // alnum : start or continue searching for the item with this prefix
2683 int keyCode
= event
.GetKeyCode();
2688 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2696 if ( !IsExpanded(m_current
) )
2699 ExpandAllChildren(m_current
);
2702 //else: fall through to Collapse() it
2706 if (IsExpanded(m_current
))
2708 Collapse(m_current
);
2714 // Use the item's bounding rectangle to determine position for the event
2716 GetBoundingRect(m_current
, ItemRect
, true);
2718 wxTreeEvent
eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU
, this, m_current
);
2719 // Use the left edge, vertical middle
2720 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
2721 ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2722 GetEventHandler()->ProcessEvent( eventMenu
);
2728 if ( !event
.HasModifiers() )
2730 wxTreeEvent
eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, m_current
);
2731 GetEventHandler()->ProcessEvent( eventAct
);
2734 // in any case, also generate the normal key event for this key,
2735 // even if we generated the ACTIVATED event above: this is what
2736 // wxMSW does and it makes sense because you might not want to
2737 // process ACTIVATED event at all and handle Space and Return
2738 // directly (and differently) which would be impossible otherwise
2742 // up goes to the previous sibling or to the last
2743 // of its children if it's expanded
2746 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2749 prev
= GetItemParent( m_key_current
);
2750 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2752 break; // don't go to root if it is hidden
2756 wxTreeItemIdValue cookie
;
2757 wxTreeItemId current
= m_key_current
;
2758 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2759 if (current
== GetFirstChild( prev
, cookie
))
2761 // otherwise we return to where we came from
2762 DoSelectItem( prev
, unselect_others
, extended_select
);
2763 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2770 while ( IsExpanded(prev
) && HasChildren(prev
) )
2772 wxTreeItemId child
= GetLastChild(prev
);
2779 DoSelectItem( prev
, unselect_others
, extended_select
);
2780 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2785 // left arrow goes to the parent
2788 wxTreeItemId prev
= GetItemParent( m_current
);
2789 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2791 // don't go to root if it is hidden
2792 prev
= GetPrevSibling( m_current
);
2796 DoSelectItem( prev
, unselect_others
, extended_select
);
2802 // this works the same as the down arrow except that we
2803 // also expand the item if it wasn't expanded yet
2809 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2811 wxTreeItemIdValue cookie
;
2812 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2813 DoSelectItem( child
, unselect_others
, extended_select
);
2814 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2818 wxTreeItemId next
= GetNextSibling( m_key_current
);
2821 wxTreeItemId current
= m_key_current
;
2822 while (current
.IsOk() && !next
)
2824 current
= GetItemParent( current
);
2825 if (current
) next
= GetNextSibling( current
);
2830 DoSelectItem( next
, unselect_others
, extended_select
);
2831 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2837 // <End> selects the last visible tree item
2840 wxTreeItemId last
= GetRootItem();
2842 while ( last
.IsOk() && IsExpanded(last
) )
2844 wxTreeItemId lastChild
= GetLastChild(last
);
2846 // it may happen if the item was expanded but then all of
2847 // its children have been deleted - so IsExpanded() returned
2848 // true, but GetLastChild() returned invalid item
2857 DoSelectItem( last
, unselect_others
, extended_select
);
2862 // <Home> selects the root item
2865 wxTreeItemId prev
= GetRootItem();
2869 if ( HasFlag(wxTR_HIDE_ROOT
) )
2871 wxTreeItemIdValue cookie
;
2872 prev
= GetFirstChild(prev
, cookie
);
2877 DoSelectItem( prev
, unselect_others
, extended_select
);
2882 // do not use wxIsalnum() here
2883 if ( !event
.HasModifiers() &&
2884 ((keyCode
>= '0' && keyCode
<= '9') ||
2885 (keyCode
>= 'a' && keyCode
<= 'z') ||
2886 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2888 // find the next item starting with the given prefix
2889 wxChar ch
= (wxChar
)keyCode
;
2891 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2902 // also start the timer to reset the current prefix if the user
2903 // doesn't press any more alnum keys soon -- we wouldn't want
2904 // to use this prefix for a new item search
2907 m_findTimer
= new wxTreeFindTimer(this);
2910 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2920 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2925 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2926 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2927 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2928 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2929 if (flags
) return wxTreeItemId();
2931 if (m_anchor
== NULL
)
2933 flags
= wxTREE_HITTEST_NOWHERE
;
2934 return wxTreeItemId();
2937 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2941 flags
= wxTREE_HITTEST_NOWHERE
;
2942 return wxTreeItemId();
2947 // get the bounding rectangle of the item (or of its label only)
2948 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2950 bool textOnly
) const
2952 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2954 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2959 rect
.width
= i
->GetWidth();
2961 if ( m_imageListNormal
)
2963 int image_w
, image_h
;
2964 m_imageListNormal
->GetSize( 0, image_w
, image_h
);
2965 rect
.width
+= image_w
+ MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2968 else // the entire line
2971 rect
.width
= GetClientSize().x
;
2975 rect
.height
= GetLineHeight(i
);
2977 // we have to return the logical coordinates, not physical ones
2978 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
2983 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
2984 wxClassInfo
* WXUNUSED(textCtrlClass
))
2986 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
2988 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2990 wxTreeEvent
te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
2991 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2997 // We have to call this here because the label in
2998 // question might just have been added and no screen
2999 // update taken place.
3001 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3004 DoDirtyProcessing();
3007 // TODO: use textCtrlClass here to create the control of correct class
3008 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
3010 m_textCtrl
->SetFocus();
3015 // returns a pointer to the text edit control if the item is being
3016 // edited, NULL otherwise (it's assumed that no more than one item may
3017 // be edited simultaneously)
3018 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3023 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3024 bool discardChanges
)
3026 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
3028 m_textCtrl
->EndEdit(discardChanges
);
3031 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3032 const wxString
& value
)
3034 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3036 le
.m_editCancelled
= false;
3038 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3041 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3043 // let owner know that the edit was cancelled
3044 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3045 le
.m_label
= wxEmptyString
;
3046 le
.m_editCancelled
= true;
3048 GetEventHandler()->ProcessEvent( le
);
3051 void wxGenericTreeCtrl::OnRenameTimer()
3053 EditLabel( m_current
);
3056 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3058 if ( !m_anchor
)return;
3060 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3062 // Is the mouse over a tree item button?
3064 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3065 wxGenericTreeItem
*underMouse
= thisItem
;
3067 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3068 #endif // wxUSE_TOOLTIPS
3071 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3072 (!event
.LeftIsDown()) &&
3074 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3082 if (underMouse
!= m_underMouse
)
3086 // unhighlight old item
3087 wxGenericTreeItem
*tmp
= m_underMouse
;
3088 m_underMouse
= NULL
;
3092 m_underMouse
= underMouse
;
3094 RefreshLine( m_underMouse
);
3098 // Determines what item we are hovering over and need a tooltip for
3099 wxTreeItemId hoverItem
= thisItem
;
3101 // We do not want a tooltip if we are dragging, or if the rename timer is running
3102 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3104 // Ask the tree control what tooltip (if any) should be shown
3105 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3107 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3109 SetToolTip(hevent
.m_label
);
3114 // we process left mouse up event (enables in-place edit), middle/right down
3115 // (pass to the user code), left dbl click (activate item) and
3116 // dragging/moving events for items drag-and-drop
3117 if ( !(event
.LeftDown() ||
3119 event
.MiddleDown() ||
3120 event
.RightDown() ||
3121 event
.LeftDClick() ||
3123 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3132 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3134 if ( event
.Dragging() && !m_isDragging
)
3136 if (m_dragCount
== 0)
3141 if (m_dragCount
!= 3)
3143 // wait until user drags a bit further...
3147 wxEventType command
= event
.RightIsDown()
3148 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3149 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3151 wxTreeEvent
nevent(command
, this, m_current
);
3152 nevent
.SetPoint(CalcScrolledPosition(pt
));
3154 // by default the dragging is not supported, the user code must
3155 // explicitly allow the event for it to take place
3158 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3160 // we're going to drag this item
3161 m_isDragging
= true;
3163 // remember the old cursor because we will change it while
3165 m_oldCursor
= m_cursor
;
3167 // in a single selection control, hide the selection temporarily
3168 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3170 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3172 if ( m_oldSelection
)
3174 m_oldSelection
->SetHilight(false);
3175 RefreshLine(m_oldSelection
);
3182 else if ( event
.Dragging() )
3184 if ( item
!= m_dropTarget
)
3186 // unhighlight the previous drop target
3187 DrawDropEffect(m_dropTarget
);
3189 m_dropTarget
= item
;
3191 // highlight the current drop target if any
3192 DrawDropEffect(m_dropTarget
);
3194 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3201 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3205 // erase the highlighting
3206 DrawDropEffect(m_dropTarget
);
3208 if ( m_oldSelection
)
3210 m_oldSelection
->SetHilight(true);
3211 RefreshLine(m_oldSelection
);
3212 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3215 // generate the drag end event
3216 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, this, item
);
3218 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3220 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3222 m_isDragging
= false;
3223 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3225 SetCursor(m_oldCursor
);
3227 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3235 // If we got to this point, we are not dragging or moving the mouse.
3236 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3237 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3238 // We skip even if we didn't hit an item because we still should
3239 // restore focus to the tree control even if we didn't exactly hit an item.
3240 if ( event
.LeftDown() )
3245 // here we process only the messages which happen on tree items
3249 if (item
== NULL
) return; /* we hit the blank area */
3251 if ( event
.RightDown() )
3253 // If the item is already selected, do not update the selection.
3254 // Multi-selections should not be cleared if a selected item is clicked.
3255 if (!IsSelected(item
))
3257 DoSelectItem(item
, true, false);
3260 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, this, item
);
3261 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3262 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3264 // Consistent with MSW (for now), send the ITEM_MENU *after*
3265 // the RIGHT_CLICK event. TODO: This behavior may change.
3266 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
3267 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3268 GetEventHandler()->ProcessEvent(nevent2
);
3270 else if ( event
.MiddleDown() )
3272 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
, this, item
);
3273 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3274 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3276 else if ( event
.LeftUp() )
3278 // this facilitates multiple-item drag-and-drop
3280 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3282 wxArrayTreeItemIds selections
;
3283 size_t count
= GetSelections(selections
);
3289 DoSelectItem(item
, true, false);
3295 if ( (item
== m_current
) &&
3296 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3297 HasFlag(wxTR_EDIT_LABELS
) )
3299 if ( m_renameTimer
)
3301 if ( m_renameTimer
->IsRunning() )
3302 m_renameTimer
->Stop();
3306 m_renameTimer
= new wxTreeRenameTimer( this );
3309 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3312 m_lastOnSame
= false;
3315 else // !RightDown() && !MiddleDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3317 if ( event
.LeftDown() )
3319 m_lastOnSame
= item
== m_current
;
3322 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3324 // only toggle the item for a single click, double click on
3325 // the button doesn't do anything (it toggles the item twice)
3326 if ( event
.LeftDown() )
3331 // don't select the item if the button was clicked
3336 // clear the previously selected items, if the
3337 // user clicked outside of the present selection.
3338 // otherwise, perform the deselection on mouse-up.
3339 // this allows multiple drag and drop to work.
3340 // but if Cmd is down, toggle selection of the clicked item
3341 if (!IsSelected(item
) || event
.CmdDown())
3343 // how should the selection work for this event?
3344 bool is_multiple
, extended_select
, unselect_others
;
3345 EventFlagsToSelType(GetWindowStyleFlag(),
3348 is_multiple
, extended_select
, unselect_others
);
3350 DoSelectItem(item
, unselect_others
, extended_select
);
3354 // For some reason, Windows isn't recognizing a left double-click,
3355 // so we need to simulate it here. Allow 200 milliseconds for now.
3356 if ( event
.LeftDClick() )
3358 // double clicking should not start editing the item label
3359 if ( m_renameTimer
)
3360 m_renameTimer
->Stop();
3362 m_lastOnSame
= false;
3364 // send activate event first
3365 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, item
);
3366 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3367 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3369 // if the user code didn't process the activate event,
3370 // handle it ourselves by toggling the item when it is
3372 if ( item
->HasPlus() )
3382 void wxGenericTreeCtrl::OnInternalIdle()
3384 wxWindow::OnInternalIdle();
3386 // Check if we need to select the root item
3387 // because nothing else has been selected.
3388 // Delaying it means that we can invoke event handlers
3389 // as required, when a first item is selected.
3390 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3393 SelectItem(m_select_me
);
3394 else if (GetRootItem().IsOk())
3395 SelectItem(GetRootItem());
3398 // after all changes have been done to the tree control,
3399 // actually redraw the tree when everything is over
3401 DoDirtyProcessing();
3404 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3409 wxTreeItemAttr
*attr
= item
->GetAttributes();
3410 if ( attr
&& attr
->HasFont() )
3411 dc
.SetFont(attr
->GetFont());
3412 else if ( item
->IsBold() )
3413 dc
.SetFont(m_boldFont
);
3415 dc
.SetFont(m_normalFont
);
3417 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3420 // restore normal font
3421 dc
.SetFont( m_normalFont
);
3425 int image
= item
->GetCurrentImage();
3426 if ( image
!= NO_IMAGE
)
3428 if ( m_imageListNormal
)
3430 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3431 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3435 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3438 total_h
+= 2; // at least 2 pixels
3440 total_h
+= total_h
/10; // otherwise 10% extra spacing
3442 item
->SetHeight(total_h
);
3443 if (total_h
>m_lineHeight
)
3444 m_lineHeight
=total_h
;
3446 item
->SetWidth(image_w
+text_w
+2);
3449 // -----------------------------------------------------------------------------
3450 // for developper : y is now the top of the level
3451 // not the middle of it !
3452 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3454 int x
= level
*m_indent
;
3455 if (!HasFlag(wxTR_HIDE_ROOT
))
3459 else if (level
== 0)
3461 // a hidden root is not evaluated, but its
3462 // children are always calculated
3466 CalculateSize( item
, dc
);
3469 item
->SetX( x
+m_spacing
);
3471 y
+= GetLineHeight(item
);
3473 if ( !item
->IsExpanded() )
3475 // we don't need to calculate collapsed branches
3480 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3481 size_t n
, count
= children
.Count();
3483 for (n
= 0; n
< count
; ++n
)
3484 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3487 void wxGenericTreeCtrl::CalculatePositions()
3489 if ( !m_anchor
) return;
3491 wxClientDC
dc(this);
3494 dc
.SetFont( m_normalFont
);
3496 dc
.SetPen( m_dottedPen
);
3497 //if(GetImageList() == NULL)
3498 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3501 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3504 void wxGenericTreeCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
3506 if ( !m_freezeCount
)
3507 wxTreeCtrlBase::Refresh(eraseBackground
, rect
);
3510 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3512 if (m_dirty
|| m_freezeCount
)
3515 wxSize client
= GetClientSize();
3518 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3519 rect
.width
= client
.x
;
3520 rect
.height
= client
.y
;
3522 Refresh(true, &rect
);
3524 AdjustMyScrollbars();
3527 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3529 if (m_dirty
|| m_freezeCount
)
3533 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3534 rect
.width
= GetClientSize().x
;
3535 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3537 Refresh(true, &rect
);
3540 void wxGenericTreeCtrl::RefreshSelected()
3545 // TODO: this is awfully inefficient, we should keep the list of all
3546 // selected items internally, should be much faster
3548 RefreshSelectedUnder(m_anchor
);
3551 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3556 if ( item
->IsSelected() )
3559 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3560 size_t count
= children
.GetCount();
3561 for ( size_t n
= 0; n
< count
; n
++ )
3563 RefreshSelectedUnder(children
[n
]);
3567 void wxGenericTreeCtrl::Freeze()
3572 void wxGenericTreeCtrl::Thaw()
3574 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen tree control?") );
3576 if ( --m_freezeCount
== 0 )
3582 // ----------------------------------------------------------------------------
3583 // changing colours: we need to refresh the tree control
3584 // ----------------------------------------------------------------------------
3586 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3588 if ( !wxWindow::SetBackgroundColour(colour
) )
3596 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3598 if ( !wxWindow::SetForegroundColour(colour
) )
3606 // Process the tooltip event, to speed up event processing.
3607 // Doesn't actually get a tooltip.
3608 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3614 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3615 // be removed, as well as the #else case below.
3616 #define _USE_VISATTR 0
3621 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3623 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3627 // Use the same color scheme as wxListBox
3628 return wxListBox::GetClassDefaultAttributes(variant
);
3630 wxVisualAttributes attr
;
3631 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3632 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3633 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3638 #if WXWIN_COMPATIBILITY_2_4
3640 int wxGenericTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
3642 return GetItemImage(item
, wxTreeItemIcon_Selected
);
3645 void wxGenericTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
3647 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
3650 #endif // WXWIN_COMPATIBILITY_2_4
3652 void wxGenericTreeCtrl::DoDirtyProcessing()
3659 CalculatePositions();
3661 AdjustMyScrollbars();
3664 #endif // wxUSE_TREECTRL