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
;
757 m_hilightBrush
= new wxBrush
759 wxSystemSettings::GetColour
761 wxSYS_COLOUR_HIGHLIGHT
766 m_hilightUnfocusedBrush
= new wxBrush
768 wxSystemSettings::GetColour
770 wxSYS_COLOUR_BTNSHADOW
775 m_imageListButtons
= NULL
;
776 m_ownsImageListButtons
= false;
779 m_isDragging
= false;
780 m_dropTarget
= m_oldSelection
= NULL
;
784 m_renameTimer
= NULL
;
789 m_dropEffectAboveItem
= false;
791 m_lastOnSame
= false;
793 #ifdef __WXMAC_CARBON__
794 m_normalFont
.MacCreateThemeFont( kThemeViewsFont
) ;
796 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
798 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
799 m_normalFont
.GetFamily(),
800 m_normalFont
.GetStyle(),
802 m_normalFont
.GetUnderlined(),
803 m_normalFont
.GetFaceName(),
804 m_normalFont
.GetEncoding());
807 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
812 const wxValidator
& validator
,
813 const wxString
& name
)
817 wxGetOsVersion( &major
, &minor
);
819 style
&= ~wxTR_LINES_AT_ROOT
;
820 style
|= wxTR_NO_LINES
;
822 style
|= wxTR_ROW_LINES
;
824 if (style
== 0 || style
& wxTR_DEFAULT_STYLE
)
825 style
|= wxTR_FULL_ROW_HIGHLIGHT
;
829 style
|= wxTR_NO_LINES
;
832 if ( !wxControl::Create( parent
, id
, pos
, size
,
833 style
|wxHSCROLL
|wxVSCROLL
,
838 // If the tree display has no buttons, but does have
839 // connecting lines, we can use a narrower layout.
840 // It may not be a good idea to force this...
841 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
847 wxVisualAttributes attr
= GetDefaultAttributes();
848 SetOwnForegroundColour( attr
.colFg
);
849 SetOwnBackgroundColour( attr
.colBg
);
851 SetOwnFont(attr
.font
);
853 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
855 SetInitialSize(size
);
860 wxGenericTreeCtrl::~wxGenericTreeCtrl()
862 delete m_hilightBrush
;
863 delete m_hilightUnfocusedBrush
;
867 delete m_renameTimer
;
870 if (m_ownsImageListButtons
)
871 delete m_imageListButtons
;
874 // -----------------------------------------------------------------------------
876 // -----------------------------------------------------------------------------
878 unsigned int wxGenericTreeCtrl::GetCount() const
886 unsigned int count
= m_anchor
->GetChildrenCount();
887 if ( !HasFlag(wxTR_HIDE_ROOT
) )
889 // take the root itself into account
896 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
898 m_indent
= (unsigned short) indent
;
903 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
904 bool recursively
) const
906 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
908 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
911 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
913 // Do not try to expand the root node if it hasn't been created yet
914 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
916 // if we will hide the root, make sure children are visible
917 m_anchor
->SetHasPlus();
919 CalculatePositions();
922 // right now, just sets the styles. Eventually, we may
923 // want to update the inherited styles, but right now
924 // none of the parents has updatable styles
925 m_windowStyle
= styles
;
929 // -----------------------------------------------------------------------------
930 // functions to work with tree items
931 // -----------------------------------------------------------------------------
933 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
935 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
937 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
940 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
941 wxTreeItemIcon which
) const
943 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
945 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
948 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
950 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
952 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
955 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
957 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
959 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
960 return pItem
->Attr().GetTextColour();
963 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
965 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
967 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
968 return pItem
->Attr().GetBackgroundColour();
971 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
973 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
975 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
976 return pItem
->Attr().GetFont();
979 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
981 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
984 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
985 pItem
->SetText(text
);
986 CalculateSize(pItem
, dc
);
990 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
992 wxTreeItemIcon which
)
994 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
996 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
997 pItem
->SetImage(image
, which
);
1000 CalculateSize(pItem
, dc
);
1004 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1006 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1009 data
->SetId( item
);
1011 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1014 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1016 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1018 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1019 pItem
->SetHasPlus(has
);
1023 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1025 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1027 // avoid redrawing the tree if no real change
1028 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1029 if ( pItem
->IsBold() != bold
)
1031 pItem
->SetBold(bold
);
1033 // recalculate the item size as bold and non bold fonts have different
1035 wxClientDC
dc(this);
1036 CalculateSize(pItem
, dc
);
1042 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1045 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1051 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1052 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1055 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1056 pItem
->Attr().SetTextColour(fg
);
1057 pItem
->Attr().SetBackgroundColour(bg
);
1061 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1062 const wxColour
& col
)
1064 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1066 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1067 pItem
->Attr().SetTextColour(col
);
1071 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1072 const wxColour
& col
)
1074 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1076 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1077 pItem
->Attr().SetBackgroundColour(col
);
1081 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1083 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1085 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1086 pItem
->Attr().SetFont(font
);
1090 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1092 wxTreeCtrlBase::SetFont(font
);
1094 m_normalFont
= font
;
1095 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1096 m_normalFont
.GetFamily(),
1097 m_normalFont
.GetStyle(),
1099 m_normalFont
.GetUnderlined(),
1100 m_normalFont
.GetFaceName(),
1101 m_normalFont
.GetEncoding());
1107 // -----------------------------------------------------------------------------
1108 // item status inquiries
1109 // -----------------------------------------------------------------------------
1111 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1113 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1115 // An item is only visible if it's not a descendant of a collapsed item
1116 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1117 wxGenericTreeItem
* parent
= pItem
->GetParent();
1120 if (!parent
->IsExpanded())
1122 parent
= parent
->GetParent();
1126 GetViewStart(& startX
, & startY
);
1128 wxSize clientSize
= GetClientSize();
1131 if (!GetBoundingRect(item
, rect
))
1133 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1135 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1137 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1143 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1145 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1147 // consider that the item does have children if it has the "+" button: it
1148 // might not have them (if it had never been expanded yet) but then it
1149 // could have them as well and it's better to err on this side rather than
1150 // disabling some operations which are restricted to the items with
1151 // children for an item which does have them
1152 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1155 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1157 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1159 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1162 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1164 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1166 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1169 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1171 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1173 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1176 // -----------------------------------------------------------------------------
1178 // -----------------------------------------------------------------------------
1180 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1182 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1184 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1187 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1188 wxTreeItemIdValue
& cookie
) const
1190 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1193 return GetNextChild(item
, cookie
);
1196 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1197 wxTreeItemIdValue
& cookie
) const
1199 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1201 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1203 // it's ok to cast cookie to size_t, we never have indices big enough to
1204 // overflow "void *"
1205 size_t *pIndex
= (size_t *)&cookie
;
1206 if ( *pIndex
< children
.Count() )
1208 return children
.Item((*pIndex
)++);
1212 // there are no more of them
1213 return wxTreeItemId();
1217 #if WXWIN_COMPATIBILITY_2_4
1219 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1222 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1225 return GetNextChild(item
, cookie
);
1228 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1231 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1233 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1234 if ( (size_t)cookie
< children
.Count() )
1236 return children
.Item((size_t)cookie
++);
1240 // there are no more of them
1241 return wxTreeItemId();
1245 #endif // WXWIN_COMPATIBILITY_2_4
1247 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1249 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1251 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1252 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1255 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1257 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1259 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1260 wxGenericTreeItem
*parent
= i
->GetParent();
1261 if ( parent
== NULL
)
1263 // root item doesn't have any siblings
1264 return wxTreeItemId();
1267 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1268 int index
= siblings
.Index(i
);
1269 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1271 size_t n
= (size_t)(index
+ 1);
1272 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1275 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1277 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1279 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1280 wxGenericTreeItem
*parent
= i
->GetParent();
1281 if ( parent
== NULL
)
1283 // root item doesn't have any siblings
1284 return wxTreeItemId();
1287 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1288 int index
= siblings
.Index(i
);
1289 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1291 return index
== 0 ? wxTreeItemId()
1292 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1295 // Only for internal use right now, but should probably be public
1296 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1298 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1300 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1302 // First see if there are any children.
1303 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1304 if (children
.GetCount() > 0)
1306 return children
.Item(0);
1310 // Try a sibling of this or ancestor instead
1311 wxTreeItemId p
= item
;
1312 wxTreeItemId toFind
;
1315 toFind
= GetNextSibling(p
);
1316 p
= GetItemParent(p
);
1317 } while (p
.IsOk() && !toFind
.IsOk());
1322 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1324 wxTreeItemId id
= GetRootItem();
1333 } while (id
.IsOk());
1335 return wxTreeItemId();
1338 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1340 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1342 wxTreeItemId id
= item
;
1345 while (id
= GetNext(id
), id
.IsOk())
1351 return wxTreeItemId();
1354 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1356 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1358 wxFAIL_MSG(wxT("not implemented"));
1360 return wxTreeItemId();
1363 // called by wxTextTreeCtrl when it marks itself for deletion
1364 void wxGenericTreeCtrl::ResetTextControl()
1369 // find the first item starting with the given prefix after the given item
1370 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1371 const wxString
& prefixOrig
) const
1373 // match is case insensitive as this is more convenient to the user: having
1374 // to press Shift-letter to go to the item starting with a capital letter
1375 // would be too bothersome
1376 wxString prefix
= prefixOrig
.Lower();
1378 // determine the starting point: we shouldn't take the current item (this
1379 // allows to switch between two items starting with the same letter just by
1380 // pressing it) but we shouldn't jump to the next one if the user is
1381 // continuing to type as otherwise he might easily skip the item he wanted
1382 wxTreeItemId id
= idParent
;
1383 if ( prefix
.length() == 1 )
1388 // look for the item starting with the given prefix after it
1389 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1394 // if we haven't found anything...
1397 // ... wrap to the beginning
1399 if ( HasFlag(wxTR_HIDE_ROOT
) )
1401 // can't select virtual root
1405 // and try all the items (stop when we get to the one we started from)
1406 while (id
.IsOk() && id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1410 // If we haven't found the item, id.IsOk() will be false, as per
1417 // -----------------------------------------------------------------------------
1419 // -----------------------------------------------------------------------------
1421 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1423 const wxString
& text
,
1426 wxTreeItemData
*data
)
1428 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1431 // should we give a warning here?
1432 return AddRoot(text
, image
, selImage
, data
);
1435 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1437 wxGenericTreeItem
*item
=
1438 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1442 data
->m_pItem
= item
;
1445 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1451 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1454 wxTreeItemData
*data
)
1456 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1458 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1460 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1461 image
, selImage
, data
);
1464 data
->m_pItem
= m_anchor
;
1467 if (HasFlag(wxTR_HIDE_ROOT
))
1469 // if root is hidden, make sure we can navigate
1471 m_anchor
->SetHasPlus();
1473 CalculatePositions();
1476 if (!HasFlag(wxTR_MULTIPLE
))
1478 m_current
= m_key_current
= m_anchor
;
1479 m_current
->SetHilight( true );
1485 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1486 const wxTreeItemId
& idPrevious
,
1487 const wxString
& text
,
1488 int image
, int selImage
,
1489 wxTreeItemData
*data
)
1491 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1494 // should we give a warning here?
1495 return AddRoot(text
, image
, selImage
, data
);
1499 if (idPrevious
.IsOk())
1501 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1502 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1503 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1506 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1510 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1512 wxTreeEvent
event(wxEVT_COMMAND_TREE_DELETE_ITEM
, this, item
);
1513 ProcessEvent( event
);
1516 // Don't leave edit or selection on a child which is about to disappear
1517 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1519 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1520 m_textCtrl
->StopEditing();
1522 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1523 m_key_current
= NULL
;
1525 if (IsDescendantOf(item
, m_select_me
)) {
1528 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1529 m_current
->SetHilight( false );
1535 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1537 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1539 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1540 ChildrenClosing(item
);
1541 item
->DeleteChildren(this);
1544 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1546 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1548 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1550 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1552 // can't delete the item being edited, cancel editing it first
1553 m_textCtrl
->StopEditing();
1556 wxGenericTreeItem
*parent
= item
->GetParent();
1558 // don't keep stale pointers around!
1559 if ( IsDescendantOf(item
, m_key_current
) )
1561 // Don't silently change the selection:
1562 // do it properly in idle time, so event
1563 // handlers get called.
1565 // m_key_current = parent;
1566 m_key_current
= NULL
;
1569 // m_select_me records whether we need to select
1570 // a different item, in idle time.
1571 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1573 m_select_me
= parent
;
1576 if ( IsDescendantOf(item
, m_current
) )
1578 // Don't silently change the selection:
1579 // do it properly in idle time, so event
1580 // handlers get called.
1582 // m_current = parent;
1584 m_select_me
= parent
;
1587 // remove the item from the tree
1590 parent
->GetChildren().Remove( item
); // remove by value
1592 else // deleting the root
1594 // nothing will be left in the tree
1598 // and delete all of its children and the item itself now
1599 item
->DeleteChildren(this);
1600 SendDeleteEvent(item
);
1602 if (item
== m_select_me
)
1608 void wxGenericTreeCtrl::DeleteAllItems()
1616 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1618 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1620 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1621 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1622 _T("can't expand hidden root") );
1624 if ( !item
->HasPlus() )
1627 if ( item
->IsExpanded() )
1630 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_EXPANDING
, this, item
);
1632 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1634 // cancelled by program
1639 CalculatePositions();
1641 RefreshSubtree(item
);
1643 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1644 ProcessEvent( event
);
1647 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1649 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1650 _T("can't collapse hidden root") );
1652 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1654 if ( !item
->IsExpanded() )
1657 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, this, item
);
1658 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1660 // cancelled by program
1664 ChildrenClosing(item
);
1667 #if 0 // TODO why should items be collapsed recursively?
1668 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1669 size_t count
= children
.Count();
1670 for ( size_t n
= 0; n
< count
; n
++ )
1672 Collapse(children
[n
]);
1676 CalculatePositions();
1678 RefreshSubtree(item
);
1680 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1681 ProcessEvent( event
);
1684 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1687 DeleteChildren(item
);
1690 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1692 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1694 if (item
->IsExpanded())
1700 void wxGenericTreeCtrl::Unselect()
1704 m_current
->SetHilight( false );
1705 RefreshLine( m_current
);
1712 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1714 if (item
->IsSelected())
1716 item
->SetHilight(false);
1720 if (item
->HasChildren())
1722 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1723 size_t count
= children
.Count();
1724 for ( size_t n
= 0; n
< count
; ++n
)
1726 UnselectAllChildren(children
[n
]);
1731 void wxGenericTreeCtrl::UnselectAll()
1733 wxTreeItemId rootItem
= GetRootItem();
1735 // the tree might not have the root item at all
1738 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1742 // Recursive function !
1743 // To stop we must have crt_item<last_item
1745 // Tag all next children, when no more children,
1746 // Move to parent (not to tag)
1747 // Keep going... if we found last_item, we stop.
1748 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1750 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1752 if (parent
== NULL
) // This is root item
1753 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1755 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1756 int index
= children
.Index(crt_item
);
1757 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1759 size_t count
= children
.Count();
1760 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1762 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1765 return TagNextChildren(parent
, last_item
, select
);
1768 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1770 crt_item
->SetHilight(select
);
1771 RefreshLine(crt_item
);
1773 if (crt_item
==last_item
)
1776 if (crt_item
->HasChildren())
1778 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1779 size_t count
= children
.Count();
1780 for ( size_t n
= 0; n
< count
; ++n
)
1782 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1790 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1794 // item2 is not necessary after item1
1795 // choice first' and 'last' between item1 and item2
1796 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1797 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1799 bool select
= m_current
->IsSelected();
1801 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1804 TagNextChildren(first
,last
,select
);
1807 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1808 bool unselect_others
,
1809 bool extended_select
)
1811 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1815 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1816 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1818 //wxCHECK_RET( ( (!unselect_others) && is_single),
1819 // wxT("this is a single selection tree") );
1821 // to keep going anyhow !!!
1824 if (item
->IsSelected())
1825 return; // nothing to do
1826 unselect_others
= true;
1827 extended_select
= false;
1829 else if ( unselect_others
&& item
->IsSelected() )
1831 // selection change if there is more than one item currently selected
1832 wxArrayTreeItemIds selected_items
;
1833 if ( GetSelections(selected_items
) == 1 )
1837 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1838 event
.m_itemOld
= m_current
;
1839 // TODO : Here we don't send any selection mode yet !
1841 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1844 wxTreeItemId parent
= GetItemParent( itemId
);
1845 while (parent
.IsOk())
1847 if (!IsExpanded(parent
))
1850 parent
= GetItemParent( parent
);
1854 if (unselect_others
)
1856 if (is_single
) Unselect(); // to speed up thing
1861 if (extended_select
)
1865 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1868 // don't change the mark (m_current)
1869 SelectItemRange(m_current
, item
);
1873 bool select
= true; // the default
1875 // Check if we need to toggle hilight (ctrl mode)
1876 if (!unselect_others
)
1877 select
=!item
->IsSelected();
1879 m_current
= m_key_current
= item
;
1880 m_current
->SetHilight(select
);
1881 RefreshLine( m_current
);
1884 // This can cause idle processing to select the root
1885 // if no item is selected, so it must be after the
1887 EnsureVisible( itemId
);
1889 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1890 GetEventHandler()->ProcessEvent( event
);
1893 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1897 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
1901 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1902 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1904 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1905 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1908 item
->SetHilight(false);
1911 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1912 GetEventHandler()->ProcessEvent( event
);
1916 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1917 wxArrayTreeItemIds
&array
) const
1919 if ( item
->IsSelected() )
1920 array
.Add(wxTreeItemId(item
));
1922 if ( item
->HasChildren() )
1924 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1925 size_t count
= children
.GetCount();
1926 for ( size_t n
= 0; n
< count
; ++n
)
1927 FillArray(children
[n
], array
);
1931 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1934 wxTreeItemId idRoot
= GetRootItem();
1935 if ( idRoot
.IsOk() )
1937 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1939 //else: the tree is empty, so no selections
1941 return array
.Count();
1944 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1946 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1948 if (!item
.IsOk()) return;
1950 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1952 // first expand all parent branches
1953 wxGenericTreeItem
*parent
= gitem
->GetParent();
1955 if ( HasFlag(wxTR_HIDE_ROOT
) )
1957 while ( parent
&& parent
!= m_anchor
)
1960 parent
= parent
->GetParent();
1968 parent
= parent
->GetParent();
1972 //if (parent) CalculatePositions();
1977 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1979 if (!item
.IsOk()) return;
1981 // We have to call this here because the label in
1982 // question might just have been added and no screen
1983 // update taken place.
1985 #if defined( __WXMSW__ ) || defined(__WXMAC__)
1988 DoDirtyProcessing();
1990 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1992 // now scroll to the item
1993 int item_y
= gitem
->GetY();
1997 GetViewStart( &start_x
, &start_y
);
1998 start_y
*= PIXELS_PER_UNIT
;
2002 GetClientSize( &client_w
, &client_h
);
2004 if (item_y
< start_y
+3)
2009 m_anchor
->GetSize( x
, y
, this );
2010 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2011 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2012 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2013 // Item should appear at top
2014 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2016 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2021 m_anchor
->GetSize( x
, y
, this );
2022 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2023 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2024 item_y
+= PIXELS_PER_UNIT
+2;
2025 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2026 // Item should appear at bottom
2027 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
);
2031 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2032 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2034 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2035 wxGenericTreeItem
**item2
)
2037 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2039 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2042 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2044 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2046 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2048 wxCHECK_RET( !s_treeBeingSorted
,
2049 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2051 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2052 if ( children
.Count() > 1 )
2056 s_treeBeingSorted
= this;
2057 children
.Sort(tree_ctrl_compare_func
);
2058 s_treeBeingSorted
= NULL
;
2060 //else: don't make the tree dirty as nothing changed
2063 void wxGenericTreeCtrl::CalculateLineHeight()
2065 wxClientDC
dc(this);
2066 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2068 if ( m_imageListNormal
)
2070 // Calculate a m_lineHeight value from the normal Image sizes.
2071 // May be toggle off. Then wxGenericTreeCtrl will spread when
2072 // necessary (which might look ugly).
2073 int n
= m_imageListNormal
->GetImageCount();
2074 for (int i
= 0; i
< n
; i
++)
2076 int width
= 0, height
= 0;
2077 m_imageListNormal
->GetSize(i
, width
, height
);
2078 if (height
> m_lineHeight
) m_lineHeight
= height
;
2082 if (m_imageListButtons
)
2084 // Calculate a m_lineHeight value from the Button image sizes.
2085 // May be toggle off. Then wxGenericTreeCtrl will spread when
2086 // necessary (which might look ugly).
2087 int n
= m_imageListButtons
->GetImageCount();
2088 for (int i
= 0; i
< n
; i
++)
2090 int width
= 0, height
= 0;
2091 m_imageListButtons
->GetSize(i
, width
, height
);
2092 if (height
> m_lineHeight
) m_lineHeight
= height
;
2096 if (m_lineHeight
< 30)
2097 m_lineHeight
+= 2; // at least 2 pixels
2099 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2102 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2104 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2105 m_imageListNormal
= imageList
;
2106 m_ownsImageListNormal
= false;
2108 // Don't do any drawing if we're setting the list to NULL,
2109 // since we may be in the process of deleting the tree control.
2111 CalculateLineHeight();
2114 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2116 if (m_ownsImageListState
) delete m_imageListState
;
2117 m_imageListState
= imageList
;
2118 m_ownsImageListState
= false;
2121 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2123 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2124 m_imageListButtons
= imageList
;
2125 m_ownsImageListButtons
= false;
2127 CalculateLineHeight();
2130 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2132 SetButtonsImageList(imageList
);
2133 m_ownsImageListButtons
= true;
2136 // -----------------------------------------------------------------------------
2138 // -----------------------------------------------------------------------------
2140 void wxGenericTreeCtrl::AdjustMyScrollbars()
2145 m_anchor
->GetSize( x
, y
, this );
2146 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2147 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2148 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2149 int y_pos
= GetScrollPos( wxVERTICAL
);
2150 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2154 SetScrollbars( 0, 0, 0, 0 );
2158 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2160 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2161 return item
->GetHeight();
2163 return m_lineHeight
;
2166 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2168 // TODO implement "state" icon on items
2170 wxTreeItemAttr
*attr
= item
->GetAttributes();
2171 if ( attr
&& attr
->HasFont() )
2172 dc
.SetFont(attr
->GetFont());
2173 else if (item
->IsBold())
2174 dc
.SetFont(m_boldFont
);
2176 long text_w
= 0, text_h
= 0;
2177 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2179 int image_h
= 0, image_w
= 0;
2180 int image
= item
->GetCurrentImage();
2181 if ( image
!= NO_IMAGE
)
2183 if ( m_imageListNormal
)
2185 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2186 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2194 int total_h
= GetLineHeight(item
);
2195 bool drawItemBackground
= false;
2197 if ( item
->IsSelected() )
2199 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2200 drawItemBackground
= true;
2205 if ( attr
&& attr
->HasBackgroundColour() )
2207 drawItemBackground
= true;
2208 colBg
= attr
->GetBackgroundColour();
2212 colBg
= GetBackgroundColour();
2214 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2217 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2219 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2223 GetVirtualSize(&w
, &h
);
2224 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2225 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2226 dc
.DrawRectangle(rect
);
2228 if (!item
->IsSelected())
2230 dc
.DrawRectangle(rect
);
2234 int flags
= wxCONTROL_SELECTED
;
2236 flags
|= wxCONTROL_FOCUSED
;
2237 if ((item
== m_current
) && (m_hasFocus
))
2238 flags
|= wxCONTROL_CURRENT
;
2239 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2245 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2247 // If it's selected, and there's an image, then we should
2248 // take care to leave the area under the image painted in the
2249 // background colour.
2250 wxRect
rect( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2251 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2252 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2253 dc
.DrawRectangle( rect
);
2258 int flags
= wxCONTROL_SELECTED
;
2260 flags
|= wxCONTROL_FOCUSED
;
2261 if ((item
== m_current
) && (m_hasFocus
))
2262 flags
|= wxCONTROL_CURRENT
;
2263 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2266 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2267 // don't allow backgrounds to be customized. Not drawing the background,
2268 // except for custom item backgrounds, works for both kinds of theme.
2269 else if (drawItemBackground
)
2271 wxRect
rect( item
->GetX()-2, item
->GetY()+offset
,
2272 item
->GetWidth()+2, total_h
-offset
);
2273 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2274 dc
.DrawRectangle( rect
);
2276 if ( attr
&& attr
->HasBackgroundColour() )
2278 dc
.DrawRectangle( rect
);
2285 int flags
= wxCONTROL_SELECTED
;
2287 flags
|= wxCONTROL_FOCUSED
;
2288 if ((item
== m_current
) && (m_hasFocus
))
2289 flags
|= wxCONTROL_CURRENT
;
2290 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, flags
);
2296 if ( image
!= NO_IMAGE
)
2298 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2299 m_imageListNormal
->Draw( image
, dc
,
2301 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2302 wxIMAGELIST_DRAW_TRANSPARENT
);
2303 dc
.DestroyClippingRegion();
2306 dc
.SetBackgroundMode(wxTRANSPARENT
);
2307 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2308 dc
.DrawText( item
->GetText(),
2309 (wxCoord
)(image_w
+ item
->GetX()),
2310 (wxCoord
)(item
->GetY() + extraH
));
2312 // restore normal font
2313 dc
.SetFont( m_normalFont
);
2316 // Now y stands for the top of the item, whereas it used to stand for middle !
2317 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2319 int x
= level
*m_indent
;
2320 if (!HasFlag(wxTR_HIDE_ROOT
))
2324 else if (level
== 0)
2326 // always expand hidden root
2328 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2329 int count
= children
.Count();
2335 PaintLevel(children
[n
], dc
, 1, y
);
2336 } while (++n
< count
);
2338 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2340 // draw line down to last child
2341 origY
+= GetLineHeight(children
[0])>>1;
2342 oldY
+= GetLineHeight(children
[n
-1])>>1;
2343 dc
.DrawLine(3, origY
, 3, oldY
);
2349 item
->SetX(x
+m_spacing
);
2352 int h
= GetLineHeight(item
);
2354 int y_mid
= y_top
+ (h
>>1);
2357 int exposed_x
= dc
.LogicalToDeviceX(0);
2358 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2360 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2364 // don't draw rect outline if we already have the
2365 // background color under Mac
2366 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2367 #endif // !__WXMAC__
2371 if ( item
->IsSelected()
2373 // On wxMac, if the tree doesn't have the focus we draw an empty
2374 // rectangle, so we want to make sure that the text is visible
2375 // against the normal background, not the highlightbackground, so
2376 // don't use the highlight text colour unless we have the focus.
2384 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2389 wxTreeItemAttr
*attr
= item
->GetAttributes();
2390 if (attr
&& attr
->HasTextColour())
2391 colText
= attr
->GetTextColour();
2393 colText
= GetForegroundColour();
2397 dc
.SetTextForeground(colText
);
2401 PaintItem(item
, dc
);
2403 if (HasFlag(wxTR_ROW_LINES
))
2405 // if the background colour is white, choose a
2406 // contrasting color for the lines
2407 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2408 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2409 dc
.DrawLine(0, y_top
, 10000, y_top
);
2410 dc
.DrawLine(0, y
, 10000, y
);
2413 // restore DC objects
2414 dc
.SetBrush(*wxWHITE_BRUSH
);
2415 dc
.SetPen(m_dottedPen
);
2416 dc
.SetTextForeground(*wxBLACK
);
2418 if ( !HasFlag(wxTR_NO_LINES
) )
2420 // draw the horizontal line here
2422 if (x
> (signed)m_indent
)
2423 x_start
-= m_indent
;
2424 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2426 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2429 // should the item show a button?
2430 if ( item
->HasPlus() && HasButtons() )
2432 if ( m_imageListButtons
)
2434 // draw the image button here
2437 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2438 : wxTreeItemIcon_Normal
;
2439 if ( item
->IsSelected() )
2440 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2442 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2443 int xx
= x
- image_w
/2;
2444 int yy
= y_mid
- image_h
/2;
2446 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2447 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2448 wxIMAGELIST_DRAW_TRANSPARENT
);
2450 else // no custom buttons
2452 static const int wImage
= 9;
2453 static const int hImage
= 9;
2456 if (item
->IsExpanded())
2457 flag
|= wxCONTROL_EXPANDED
;
2458 if (item
== m_underMouse
)
2459 flag
|= wxCONTROL_CURRENT
;
2461 wxRendererNative::Get().DrawTreeItemButton
2465 wxRect(x
- wImage
/2,
2474 if (item
->IsExpanded())
2476 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2477 int count
= children
.Count();
2484 PaintLevel(children
[n
], dc
, level
, y
);
2485 } while (++n
< count
);
2487 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2489 // draw line down to last child
2490 oldY
+= GetLineHeight(children
[n
-1])>>1;
2491 if (HasButtons()) y_mid
+= 5;
2493 // Only draw the portion of the line that is visible, in case it is huge
2494 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2495 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2496 yOrigin
= abs(yOrigin
);
2497 GetClientSize(&width
, &height
);
2499 // Move end points to the begining/end of the view?
2500 if (y_mid
< yOrigin
)
2502 if (oldY
> yOrigin
+ height
)
2503 oldY
= yOrigin
+ height
;
2505 // after the adjustments if y_mid is larger than oldY then the line
2506 // isn't visible at all so don't draw anything
2508 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2514 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2518 if ( item
->HasPlus() )
2520 // it's a folder, indicate it by a border
2525 // draw a line under the drop target because the item will be
2527 DrawLine(item
, !m_dropEffectAboveItem
);
2530 SetCursor(wxCURSOR_BULLSEYE
);
2535 SetCursor(wxCURSOR_NO_ENTRY
);
2539 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2541 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2543 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2545 wxClientDC
dc(this);
2547 dc
.SetLogicalFunction(wxINVERT
);
2548 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2550 int w
= i
->GetWidth() + 2;
2551 int h
= GetLineHeight(i
) + 2;
2553 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2556 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2558 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2560 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2562 wxClientDC
dc(this);
2564 dc
.SetLogicalFunction(wxINVERT
);
2570 y
+= GetLineHeight(i
) - 1;
2573 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2576 // -----------------------------------------------------------------------------
2577 // wxWidgets callbacks
2578 // -----------------------------------------------------------------------------
2580 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
2583 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
2584 RefreshLine( m_current
);
2590 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2598 dc
.SetFont( m_normalFont
);
2599 dc
.SetPen( m_dottedPen
);
2601 // this is now done dynamically
2602 //if(GetImageList() == NULL)
2603 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2606 PaintLevel( m_anchor
, dc
, 0, y
);
2609 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2618 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2627 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2629 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2630 te
.m_evtKey
= event
;
2631 if ( GetEventHandler()->ProcessEvent( te
) )
2633 // intercepted by the user code
2637 if ( (m_current
== 0) || (m_key_current
== 0) )
2643 // how should the selection work for this event?
2644 bool is_multiple
, extended_select
, unselect_others
;
2645 EventFlagsToSelType(GetWindowStyleFlag(),
2648 is_multiple
, extended_select
, unselect_others
);
2650 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2652 if (event
.GetKeyCode() == WXK_RIGHT
)
2653 event
.m_keyCode
= WXK_LEFT
;
2654 else if (event
.GetKeyCode() == WXK_LEFT
)
2655 event
.m_keyCode
= WXK_RIGHT
;
2660 // * : Expand all/Collapse all
2661 // ' ' | return : activate
2662 // up : go up (not last children!)
2664 // left : go to parent
2665 // right : open if parent and go next
2666 // home : go to root
2667 // end : go to last item without opening parents
2668 // alnum : start or continue searching for the item with this prefix
2669 int keyCode
= event
.GetKeyCode();
2674 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2682 if ( !IsExpanded(m_current
) )
2685 ExpandAllChildren(m_current
);
2688 //else: fall through to Collapse() it
2692 if (IsExpanded(m_current
))
2694 Collapse(m_current
);
2700 // Use the item's bounding rectangle to determine position for the event
2702 GetBoundingRect(m_current
, ItemRect
, true);
2704 wxTreeEvent
eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU
, this, m_current
);
2705 // Use the left edge, vertical middle
2706 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
2707 ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2708 GetEventHandler()->ProcessEvent( eventMenu
);
2714 if ( !event
.HasModifiers() )
2716 wxTreeEvent
eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, m_current
);
2717 GetEventHandler()->ProcessEvent( eventAct
);
2720 // in any case, also generate the normal key event for this key,
2721 // even if we generated the ACTIVATED event above: this is what
2722 // wxMSW does and it makes sense because you might not want to
2723 // process ACTIVATED event at all and handle Space and Return
2724 // directly (and differently) which would be impossible otherwise
2728 // up goes to the previous sibling or to the last
2729 // of its children if it's expanded
2732 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2735 prev
= GetItemParent( m_key_current
);
2736 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2738 break; // don't go to root if it is hidden
2742 wxTreeItemIdValue cookie
;
2743 wxTreeItemId current
= m_key_current
;
2744 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2745 if (current
== GetFirstChild( prev
, cookie
))
2747 // otherwise we return to where we came from
2748 DoSelectItem( prev
, unselect_others
, extended_select
);
2749 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2756 while ( IsExpanded(prev
) && HasChildren(prev
) )
2758 wxTreeItemId child
= GetLastChild(prev
);
2765 DoSelectItem( prev
, unselect_others
, extended_select
);
2766 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2771 // left arrow goes to the parent
2774 wxTreeItemId prev
= GetItemParent( m_current
);
2775 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2777 // don't go to root if it is hidden
2778 prev
= GetPrevSibling( m_current
);
2782 DoSelectItem( prev
, unselect_others
, extended_select
);
2788 // this works the same as the down arrow except that we
2789 // also expand the item if it wasn't expanded yet
2795 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2797 wxTreeItemIdValue cookie
;
2798 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2799 DoSelectItem( child
, unselect_others
, extended_select
);
2800 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2804 wxTreeItemId next
= GetNextSibling( m_key_current
);
2807 wxTreeItemId current
= m_key_current
;
2808 while (current
.IsOk() && !next
)
2810 current
= GetItemParent( current
);
2811 if (current
) next
= GetNextSibling( current
);
2816 DoSelectItem( next
, unselect_others
, extended_select
);
2817 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2823 // <End> selects the last visible tree item
2826 wxTreeItemId last
= GetRootItem();
2828 while ( last
.IsOk() && IsExpanded(last
) )
2830 wxTreeItemId lastChild
= GetLastChild(last
);
2832 // it may happen if the item was expanded but then all of
2833 // its children have been deleted - so IsExpanded() returned
2834 // true, but GetLastChild() returned invalid item
2843 DoSelectItem( last
, unselect_others
, extended_select
);
2848 // <Home> selects the root item
2851 wxTreeItemId prev
= GetRootItem();
2855 if ( HasFlag(wxTR_HIDE_ROOT
) )
2857 wxTreeItemIdValue cookie
;
2858 prev
= GetFirstChild(prev
, cookie
);
2863 DoSelectItem( prev
, unselect_others
, extended_select
);
2868 // do not use wxIsalnum() here
2869 if ( !event
.HasModifiers() &&
2870 ((keyCode
>= '0' && keyCode
<= '9') ||
2871 (keyCode
>= 'a' && keyCode
<= 'z') ||
2872 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2874 // find the next item starting with the given prefix
2875 wxChar ch
= (wxChar
)keyCode
;
2877 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2888 // also start the timer to reset the current prefix if the user
2889 // doesn't press any more alnum keys soon -- we wouldn't want
2890 // to use this prefix for a new item search
2893 m_findTimer
= new wxTreeFindTimer(this);
2896 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2906 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
2911 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2912 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2913 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2914 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2915 if (flags
) return wxTreeItemId();
2917 if (m_anchor
== NULL
)
2919 flags
= wxTREE_HITTEST_NOWHERE
;
2920 return wxTreeItemId();
2923 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2927 flags
= wxTREE_HITTEST_NOWHERE
;
2928 return wxTreeItemId();
2933 // get the bounding rectangle of the item (or of its label only)
2934 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2936 bool textOnly
) const
2938 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2940 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2945 rect
.width
= i
->GetWidth();
2947 if ( m_imageListNormal
)
2949 int image_w
, image_h
;
2950 m_imageListNormal
->GetSize( 0, image_w
, image_h
);
2951 rect
.width
+= image_w
+ MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2954 else // the entire line
2957 rect
.width
= GetClientSize().x
;
2961 rect
.height
= GetLineHeight(i
);
2963 // we have to return the logical coordinates, not physical ones
2964 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
2969 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
2970 wxClassInfo
* WXUNUSED(textCtrlClass
))
2972 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
2974 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2976 wxTreeEvent
te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
2977 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2983 // We have to call this here because the label in
2984 // question might just have been added and no screen
2985 // update taken place.
2987 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2990 DoDirtyProcessing();
2993 // TODO: use textCtrlClass here to create the control of correct class
2994 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2996 m_textCtrl
->SetFocus();
3001 // returns a pointer to the text edit control if the item is being
3002 // edited, NULL otherwise (it's assumed that no more than one item may
3003 // be edited simultaneously)
3004 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3009 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3010 bool discardChanges
)
3012 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
3014 m_textCtrl
->EndEdit(discardChanges
);
3017 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3018 const wxString
& value
)
3020 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3022 le
.m_editCancelled
= false;
3024 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3027 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3029 // let owner know that the edit was cancelled
3030 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3031 le
.m_label
= wxEmptyString
;
3032 le
.m_editCancelled
= true;
3034 GetEventHandler()->ProcessEvent( le
);
3037 void wxGenericTreeCtrl::OnRenameTimer()
3039 EditLabel( m_current
);
3042 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3044 if ( !m_anchor
)return;
3046 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3048 // Is the mouse over a tree item button?
3050 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3051 wxGenericTreeItem
*underMouse
= thisItem
;
3053 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3054 #endif // wxUSE_TOOLTIPS
3057 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3058 (!event
.LeftIsDown()) &&
3060 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3068 if (underMouse
!= m_underMouse
)
3072 // unhighlight old item
3073 wxGenericTreeItem
*tmp
= m_underMouse
;
3074 m_underMouse
= NULL
;
3078 m_underMouse
= underMouse
;
3080 RefreshLine( m_underMouse
);
3084 // Determines what item we are hovering over and need a tooltip for
3085 wxTreeItemId hoverItem
= thisItem
;
3087 // We do not want a tooltip if we are dragging, or if the rename timer is running
3088 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3090 // Ask the tree control what tooltip (if any) should be shown
3091 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3093 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3095 SetToolTip(hevent
.m_label
);
3100 // we process left mouse up event (enables in-place edit), middle/right down
3101 // (pass to the user code), left dbl click (activate item) and
3102 // dragging/moving events for items drag-and-drop
3103 if ( !(event
.LeftDown() ||
3105 event
.MiddleDown() ||
3106 event
.RightDown() ||
3107 event
.LeftDClick() ||
3109 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3118 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3120 if ( event
.Dragging() && !m_isDragging
)
3122 if (m_dragCount
== 0)
3127 if (m_dragCount
!= 3)
3129 // wait until user drags a bit further...
3133 wxEventType command
= event
.RightIsDown()
3134 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3135 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3137 wxTreeEvent
nevent(command
, this, m_current
);
3138 nevent
.SetPoint(CalcScrolledPosition(pt
));
3140 // by default the dragging is not supported, the user code must
3141 // explicitly allow the event for it to take place
3144 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3146 // we're going to drag this item
3147 m_isDragging
= true;
3149 // remember the old cursor because we will change it while
3151 m_oldCursor
= m_cursor
;
3153 // in a single selection control, hide the selection temporarily
3154 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3156 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3158 if ( m_oldSelection
)
3160 m_oldSelection
->SetHilight(false);
3161 RefreshLine(m_oldSelection
);
3168 else if ( event
.Dragging() )
3170 if ( item
!= m_dropTarget
)
3172 // unhighlight the previous drop target
3173 DrawDropEffect(m_dropTarget
);
3175 m_dropTarget
= item
;
3177 // highlight the current drop target if any
3178 DrawDropEffect(m_dropTarget
);
3180 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3187 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3191 // erase the highlighting
3192 DrawDropEffect(m_dropTarget
);
3194 if ( m_oldSelection
)
3196 m_oldSelection
->SetHilight(true);
3197 RefreshLine(m_oldSelection
);
3198 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3201 // generate the drag end event
3202 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, this, item
);
3204 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3206 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3208 m_isDragging
= false;
3209 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3211 SetCursor(m_oldCursor
);
3213 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3221 // If we got to this point, we are not dragging or moving the mouse.
3222 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3223 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3224 // We skip even if we didn't hit an item because we still should
3225 // restore focus to the tree control even if we didn't exactly hit an item.
3226 if ( event
.LeftDown() )
3231 // here we process only the messages which happen on tree items
3235 if (item
== NULL
) return; /* we hit the blank area */
3237 if ( event
.RightDown() )
3239 // If the item is already selected, do not update the selection.
3240 // Multi-selections should not be cleared if a selected item is clicked.
3241 if (!IsSelected(item
))
3243 DoSelectItem(item
, true, false);
3246 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, this, item
);
3247 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3248 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3250 // Consistent with MSW (for now), send the ITEM_MENU *after*
3251 // the RIGHT_CLICK event. TODO: This behavior may change.
3252 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
3253 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3254 GetEventHandler()->ProcessEvent(nevent2
);
3256 else if ( event
.MiddleDown() )
3258 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
, this, item
);
3259 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3260 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3262 else if ( event
.LeftUp() )
3264 // this facilitates multiple-item drag-and-drop
3266 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3268 wxArrayTreeItemIds selections
;
3269 size_t count
= GetSelections(selections
);
3275 DoSelectItem(item
, true, false);
3281 if ( (item
== m_current
) &&
3282 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3283 HasFlag(wxTR_EDIT_LABELS
) )
3285 if ( m_renameTimer
)
3287 if ( m_renameTimer
->IsRunning() )
3288 m_renameTimer
->Stop();
3292 m_renameTimer
= new wxTreeRenameTimer( this );
3295 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3298 m_lastOnSame
= false;
3301 else // !RightDown() && !MiddleDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3303 if ( event
.LeftDown() )
3305 m_lastOnSame
= item
== m_current
;
3308 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3310 // only toggle the item for a single click, double click on
3311 // the button doesn't do anything (it toggles the item twice)
3312 if ( event
.LeftDown() )
3317 // don't select the item if the button was clicked
3322 // clear the previously selected items, if the
3323 // user clicked outside of the present selection.
3324 // otherwise, perform the deselection on mouse-up.
3325 // this allows multiple drag and drop to work.
3326 // but if Cmd is down, toggle selection of the clicked item
3327 if (!IsSelected(item
) || event
.CmdDown())
3329 // how should the selection work for this event?
3330 bool is_multiple
, extended_select
, unselect_others
;
3331 EventFlagsToSelType(GetWindowStyleFlag(),
3334 is_multiple
, extended_select
, unselect_others
);
3336 DoSelectItem(item
, unselect_others
, extended_select
);
3340 // For some reason, Windows isn't recognizing a left double-click,
3341 // so we need to simulate it here. Allow 200 milliseconds for now.
3342 if ( event
.LeftDClick() )
3344 // double clicking should not start editing the item label
3345 if ( m_renameTimer
)
3346 m_renameTimer
->Stop();
3348 m_lastOnSame
= false;
3350 // send activate event first
3351 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, item
);
3352 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3353 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3355 // if the user code didn't process the activate event,
3356 // handle it ourselves by toggling the item when it is
3358 if ( item
->HasPlus() )
3368 void wxGenericTreeCtrl::OnInternalIdle()
3370 wxWindow::OnInternalIdle();
3372 // Check if we need to select the root item
3373 // because nothing else has been selected.
3374 // Delaying it means that we can invoke event handlers
3375 // as required, when a first item is selected.
3376 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3379 SelectItem(m_select_me
);
3380 else if (GetRootItem().IsOk())
3381 SelectItem(GetRootItem());
3384 // after all changes have been done to the tree control,
3385 // actually redraw the tree when everything is over
3387 DoDirtyProcessing();
3390 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3395 wxTreeItemAttr
*attr
= item
->GetAttributes();
3396 if ( attr
&& attr
->HasFont() )
3397 dc
.SetFont(attr
->GetFont());
3398 else if ( item
->IsBold() )
3399 dc
.SetFont(m_boldFont
);
3401 dc
.SetFont(m_normalFont
);
3403 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3406 // restore normal font
3407 dc
.SetFont( m_normalFont
);
3411 int image
= item
->GetCurrentImage();
3412 if ( image
!= NO_IMAGE
)
3414 if ( m_imageListNormal
)
3416 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3417 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3421 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3424 total_h
+= 2; // at least 2 pixels
3426 total_h
+= total_h
/10; // otherwise 10% extra spacing
3428 item
->SetHeight(total_h
);
3429 if (total_h
>m_lineHeight
)
3430 m_lineHeight
=total_h
;
3432 item
->SetWidth(image_w
+text_w
+2);
3435 // -----------------------------------------------------------------------------
3436 // for developper : y is now the top of the level
3437 // not the middle of it !
3438 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3440 int x
= level
*m_indent
;
3441 if (!HasFlag(wxTR_HIDE_ROOT
))
3445 else if (level
== 0)
3447 // a hidden root is not evaluated, but its
3448 // children are always calculated
3452 CalculateSize( item
, dc
);
3455 item
->SetX( x
+m_spacing
);
3457 y
+= GetLineHeight(item
);
3459 if ( !item
->IsExpanded() )
3461 // we don't need to calculate collapsed branches
3466 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3467 size_t n
, count
= children
.Count();
3469 for (n
= 0; n
< count
; ++n
)
3470 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3473 void wxGenericTreeCtrl::CalculatePositions()
3475 if ( !m_anchor
) return;
3477 wxClientDC
dc(this);
3480 dc
.SetFont( m_normalFont
);
3482 dc
.SetPen( m_dottedPen
);
3483 //if(GetImageList() == NULL)
3484 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3487 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3490 void wxGenericTreeCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
3492 if ( !m_freezeCount
)
3493 wxTreeCtrlBase::Refresh(eraseBackground
, rect
);
3496 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3498 if (m_dirty
|| m_freezeCount
)
3501 wxSize client
= GetClientSize();
3504 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3505 rect
.width
= client
.x
;
3506 rect
.height
= client
.y
;
3508 Refresh(true, &rect
);
3510 AdjustMyScrollbars();
3513 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3515 if (m_dirty
|| m_freezeCount
)
3519 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3520 rect
.width
= GetClientSize().x
;
3521 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3523 Refresh(true, &rect
);
3526 void wxGenericTreeCtrl::RefreshSelected()
3531 // TODO: this is awfully inefficient, we should keep the list of all
3532 // selected items internally, should be much faster
3534 RefreshSelectedUnder(m_anchor
);
3537 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3542 if ( item
->IsSelected() )
3545 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3546 size_t count
= children
.GetCount();
3547 for ( size_t n
= 0; n
< count
; n
++ )
3549 RefreshSelectedUnder(children
[n
]);
3553 void wxGenericTreeCtrl::Freeze()
3558 void wxGenericTreeCtrl::Thaw()
3560 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen tree control?") );
3562 if ( --m_freezeCount
== 0 )
3568 // ----------------------------------------------------------------------------
3569 // changing colours: we need to refresh the tree control
3570 // ----------------------------------------------------------------------------
3572 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3574 if ( !wxWindow::SetBackgroundColour(colour
) )
3582 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3584 if ( !wxWindow::SetForegroundColour(colour
) )
3592 // Process the tooltip event, to speed up event processing.
3593 // Doesn't actually get a tooltip.
3594 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3600 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3601 // be removed, as well as the #else case below.
3602 #define _USE_VISATTR 0
3607 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3609 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3613 // Use the same color scheme as wxListBox
3614 return wxListBox::GetClassDefaultAttributes(variant
);
3616 wxVisualAttributes attr
;
3617 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3618 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3619 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3624 #if WXWIN_COMPATIBILITY_2_4
3626 int wxGenericTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
3628 return GetItemImage(item
, wxTreeItemIcon_Selected
);
3631 void wxGenericTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
3633 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
3636 #endif // WXWIN_COMPATIBILITY_2_4
3638 void wxGenericTreeCtrl::DoDirtyProcessing()
3645 CalculatePositions();
3647 AdjustMyScrollbars();
3650 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
3652 // make sure all positions are calculated as normally this only done during
3653 // idle time but we need them for base class DoGetBestSize() to return the
3655 wxConstCast(this, wxGenericTreeCtrl
)->CalculatePositions();
3657 wxSize size
= wxTreeCtrlBase::DoGetBestSize();
3659 // there seems to be an implicit extra border around the items, although
3660 // I'm not really sure where does it come from -- but without this, the
3661 // scrollbars appear in a tree with default/best size
3664 // and the border has to be rounded up to a multiple of PIXELS_PER_UNIT or
3665 // scrollbars still appear
3666 const wxSize
& borderSize
= GetWindowBorderSize();
3668 int dx
= (size
.x
- borderSize
.x
) % PIXELS_PER_UNIT
;
3670 size
.x
+= PIXELS_PER_UNIT
- dx
;
3671 int dy
= (size
.y
- borderSize
.y
) % PIXELS_PER_UNIT
;
3673 size
.y
+= PIXELS_PER_UNIT
- dy
;
3675 // we need to update the cache too as the base class cached its own value
3676 CacheBestSize(size
);
3681 #endif // wxUSE_TREECTRL