1 /////////////////////////////////////////////////////////////////////////////
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "treectlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/treebase.h"
34 #include "wx/generic/treectlg.h"
36 #include "wx/textctrl.h"
37 #include "wx/imaglist.h"
38 #include "wx/settings.h"
39 #include "wx/dcclient.h"
41 #include "wx/renderer.h"
44 #include "wx/mac/private.h"
47 // -----------------------------------------------------------------------------
49 // -----------------------------------------------------------------------------
51 class WXDLLEXPORT wxGenericTreeItem
;
53 WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 static const int NO_IMAGE
= -1;
61 static const int PIXELS_PER_UNIT
= 10;
63 // -----------------------------------------------------------------------------
65 // -----------------------------------------------------------------------------
67 // timer used for enabling in-place edit
68 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
71 // start editing the current item after half a second (if the mouse hasn't
72 // been clicked/moved)
75 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
77 virtual void Notify();
80 wxGenericTreeCtrl
*m_owner
;
82 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
85 // control used for in-place edit
86 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
89 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
94 m_owner
->OnRenameCancelled(m_itemEdited
);
96 const wxGenericTreeItem
* item() const { return m_itemEdited
; }
99 void OnChar( wxKeyEvent
&event
);
100 void OnKeyUp( wxKeyEvent
&event
);
101 void OnKillFocus( wxFocusEvent
&event
);
103 bool AcceptChanges();
107 wxGenericTreeCtrl
*m_owner
;
108 wxGenericTreeItem
*m_itemEdited
;
109 wxString m_startValue
;
111 bool m_aboutToFinish
;
113 DECLARE_EVENT_TABLE()
114 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
117 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
118 // for a sufficiently long time
119 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
122 // reset the current prefix after half a second of inactivity
123 enum { DELAY
= 500 };
125 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
127 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
130 wxGenericTreeCtrl
*m_owner
;
132 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
136 class WXDLLEXPORT wxGenericTreeItem
140 wxGenericTreeItem() { m_data
= NULL
; }
141 wxGenericTreeItem( wxGenericTreeItem
*parent
,
142 const wxString
& text
,
145 wxTreeItemData
*data
);
147 ~wxGenericTreeItem();
150 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
152 const wxString
& GetText() const { return m_text
; }
153 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
154 { return m_images
[which
]; }
155 wxTreeItemData
*GetData() const { return m_data
; }
157 // returns the current image for the item (depending on its
158 // selected/expanded/whatever state)
159 int GetCurrentImage() const;
161 void SetText( const wxString
&text
);
162 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
163 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
165 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
167 void SetBold(bool bold
) { m_isBold
= bold
; }
169 int GetX() const { return m_x
; }
170 int GetY() const { return m_y
; }
172 void SetX(int x
) { m_x
= x
; }
173 void SetY(int y
) { m_y
= y
; }
175 int GetHeight() const { return m_height
; }
176 int GetWidth() const { return m_width
; }
178 void SetHeight(int h
) { m_height
= h
; }
179 void SetWidth(int w
) { m_width
= w
; }
181 wxGenericTreeItem
*GetParent() const { return m_parent
; }
184 // deletes all children notifying the treectrl about it if !NULL
186 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
188 // get count of all children (and grand children if 'recursively')
189 size_t GetChildrenCount(bool recursively
= true) const;
191 void Insert(wxGenericTreeItem
*child
, size_t index
)
192 { m_children
.Insert(child
, index
); }
194 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
196 // return the item at given position (or NULL if no item), onButton is
197 // true if the point belongs to the item's button, otherwise it lies
198 // on the item's label
199 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
200 const wxGenericTreeCtrl
*,
204 void Expand() { m_isCollapsed
= false; }
205 void Collapse() { m_isCollapsed
= true; }
207 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
210 bool HasChildren() const { return !m_children
.IsEmpty(); }
211 bool IsSelected() const { return m_hasHilight
!= 0; }
212 bool IsExpanded() const { return !m_isCollapsed
; }
213 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
214 bool IsBold() const { return m_isBold
!= 0; }
217 // get them - may be NULL
218 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
219 // get them ensuring that the pointer is not NULL
220 wxTreeItemAttr
& Attr()
224 m_attr
= new wxTreeItemAttr
;
230 void SetAttributes(wxTreeItemAttr
*attr
)
232 if ( m_ownsAttr
) delete m_attr
;
236 // set them and delete when done
237 void AssignAttributes(wxTreeItemAttr
*attr
)
244 // since there can be very many of these, we save size by chosing
245 // the smallest representation for the elements and by ordering
246 // the members to avoid padding.
247 wxString m_text
; // label to be rendered for item
249 wxTreeItemData
*m_data
; // user-provided data
251 wxArrayGenericTreeItems m_children
; // list of children
252 wxGenericTreeItem
*m_parent
; // parent of this item
254 wxTreeItemAttr
*m_attr
; // attributes???
256 // tree ctrl images for the normal, selected, expanded and
257 // expanded+selected states
258 int m_images
[wxTreeItemIcon_Max
];
260 wxCoord m_x
; // (virtual) offset from top
261 wxCoord m_y
; // (virtual) offset from left
262 int m_width
; // width of this item
263 int m_height
; // height of this item
265 // use bitfields to save size
266 int m_isCollapsed
:1;
267 int m_hasHilight
:1; // same as focused
268 int m_hasPlus
:1; // used for item which doesn't have
269 // children but has a [+] button
270 int m_isBold
:1; // render the label in bold font
271 int m_ownsAttr
:1; // delete attribute when done
273 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
276 // =============================================================================
278 // =============================================================================
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
284 // translate the key or mouse event flags to the type of selection we're
286 static void EventFlagsToSelType(long style
,
290 bool &extended_select
,
291 bool &unselect_others
)
293 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
294 extended_select
= shiftDown
&& is_multiple
;
295 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
298 // check if the given item is under another one
299 static bool IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
303 if ( item
== parent
)
305 // item is a descendant of parent
309 item
= item
->GetParent();
315 // -----------------------------------------------------------------------------
316 // wxTreeRenameTimer (internal)
317 // -----------------------------------------------------------------------------
319 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
324 void wxTreeRenameTimer::Notify()
326 m_owner
->OnRenameTimer();
329 //-----------------------------------------------------------------------------
330 // wxTreeTextCtrl (internal)
331 //-----------------------------------------------------------------------------
333 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
334 EVT_CHAR (wxTreeTextCtrl::OnChar
)
335 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
336 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
339 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
340 wxGenericTreeItem
*item
)
341 : m_itemEdited(item
), m_startValue(item
->GetText())
345 m_aboutToFinish
= false;
347 int w
= m_itemEdited
->GetWidth(),
348 h
= m_itemEdited
->GetHeight();
351 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
356 int image
= item
->GetCurrentImage();
357 if ( image
!= NO_IMAGE
)
359 if ( m_owner
->m_imageListNormal
)
361 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
366 wxFAIL_MSG(_T("you must create an image list to use images!"));
370 // FIXME: what are all these hardcoded 4, 8 and 11s really?
374 wxSize bs
= DoGetBestSize() ;
375 // edit control height
378 int diff
= h
- ( bs
.y
- 8 ) ;
384 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
385 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
388 bool wxTreeTextCtrl::AcceptChanges()
390 const wxString value
= GetValue();
392 if ( value
== m_startValue
)
394 // nothing changed, always accept
395 // when an item remains unchanged, the owner
396 // needs to be notified that the user decided
397 // not to change the tree item label, and that
398 // the edit has been cancelled
400 m_owner
->OnRenameCancelled(m_itemEdited
);
404 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
406 // vetoed by the user
410 // accepted, do rename the item
411 m_owner
->SetItemText(m_itemEdited
, value
);
416 void wxTreeTextCtrl::Finish()
420 m_owner
->ResetTextControl();
422 wxPendingDelete
.Append(this);
426 m_owner
->SetFocusIgnoringChildren();
430 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
432 switch ( event
.m_keyCode
)
435 m_aboutToFinish
= true;
436 // Notify the owner about the changes
438 // Even if vetoed, close the control (consistent with MSW)
451 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
455 // auto-grow the textctrl:
456 wxSize parentSize
= m_owner
->GetSize();
457 wxPoint myPos
= GetPosition();
458 wxSize mySize
= GetSize();
460 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
461 if (myPos
.x
+ sx
> parentSize
.x
)
462 sx
= parentSize
.x
- myPos
.x
;
465 SetSize(sx
, wxDefaultCoord
);
471 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
473 if ( !m_finished
&& !m_aboutToFinish
)
475 // We must finish regardless of success, otherwise we'll get
479 if ( !AcceptChanges() )
480 m_owner
->OnRenameCancelled( m_itemEdited
);
483 // We must let the native text control handle focus, too, otherwise
484 // it could have problems with the cursor (e.g., in wxGTK).
488 // -----------------------------------------------------------------------------
490 // -----------------------------------------------------------------------------
492 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
493 const wxString
& text
,
494 int image
, int selImage
,
495 wxTreeItemData
*data
)
498 m_images
[wxTreeItemIcon_Normal
] = image
;
499 m_images
[wxTreeItemIcon_Selected
] = selImage
;
500 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
501 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
506 m_isCollapsed
= true;
507 m_hasHilight
= false;
513 m_attr
= (wxTreeItemAttr
*)NULL
;
516 // We don't know the height here yet.
521 wxGenericTreeItem::~wxGenericTreeItem()
525 if (m_ownsAttr
) delete m_attr
;
527 wxASSERT_MSG( m_children
.IsEmpty(),
528 wxT("please call DeleteChildren() before deleting the item") );
531 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
533 size_t count
= m_children
.Count();
534 for ( size_t n
= 0; n
< count
; n
++ )
536 wxGenericTreeItem
*child
= m_children
[n
];
538 tree
->SendDeleteEvent(child
);
540 child
->DeleteChildren(tree
);
541 if (child
== tree
->m_select_me
)
542 tree
->m_select_me
= NULL
;
549 void wxGenericTreeItem::SetText( const wxString
&text
)
554 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
556 size_t count
= m_children
.Count();
560 size_t total
= count
;
561 for (size_t n
= 0; n
< count
; ++n
)
563 total
+= m_children
[n
]->GetChildrenCount();
569 void wxGenericTreeItem::GetSize( int &x
, int &y
,
570 const wxGenericTreeCtrl
*theButton
)
572 int bottomY
=m_y
+theButton
->GetLineHeight(this);
573 if ( y
< bottomY
) y
= bottomY
;
574 int width
= m_x
+ m_width
;
575 if ( x
< width
) x
= width
;
579 size_t count
= m_children
.Count();
580 for ( size_t n
= 0; n
< count
; ++n
)
582 m_children
[n
]->GetSize( x
, y
, theButton
);
587 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
588 const wxGenericTreeCtrl
*theCtrl
,
592 // for a hidden root node, don't evaluate it, but do evaluate children
593 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
596 int h
= theCtrl
->GetLineHeight(this);
597 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
599 int y_mid
= m_y
+ h
/2;
600 if (point
.y
< y_mid
)
601 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
603 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
605 int xCross
= m_x
- theCtrl
->GetSpacing();
607 // according to the drawing code the triangels are drawn
608 // at -4 , -4 from the position up to +10/+10 max
609 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
610 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
611 HasPlus() && theCtrl
->HasButtons() )
613 // 5 is the size of the plus sign
614 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
615 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
616 HasPlus() && theCtrl
->HasButtons() )
619 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
623 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
628 // assuming every image (normal and selected) has the same size!
629 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
630 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
633 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
634 flags
|= wxTREE_HITTEST_ONITEMICON
;
636 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
642 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
643 if (point
.x
> m_x
+m_width
)
644 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
649 // if children are expanded, fall through to evaluate them
650 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
654 size_t count
= m_children
.Count();
655 for ( size_t n
= 0; n
< count
; n
++ )
657 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
665 return (wxGenericTreeItem
*) NULL
;
668 int wxGenericTreeItem::GetCurrentImage() const
670 int image
= NO_IMAGE
;
675 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
678 if ( image
== NO_IMAGE
)
680 // we usually fall back to the normal item, but try just the
681 // expanded one (and not selected) first in this case
682 image
= GetImage(wxTreeItemIcon_Expanded
);
688 image
= GetImage(wxTreeItemIcon_Selected
);
691 // maybe it doesn't have the specific image we want,
692 // try the default one instead
693 if ( image
== NO_IMAGE
) image
= GetImage();
698 // -----------------------------------------------------------------------------
699 // wxGenericTreeCtrl implementation
700 // -----------------------------------------------------------------------------
702 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
704 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
705 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
706 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
707 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
708 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
709 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
710 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
713 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
715 * wxTreeCtrl has to be a real class or we have problems with
716 * the run-time information.
719 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
722 // -----------------------------------------------------------------------------
723 // construction/destruction
724 // -----------------------------------------------------------------------------
726 void wxGenericTreeCtrl::Init()
728 m_current
= m_key_current
= m_anchor
= m_select_me
= (wxGenericTreeItem
*) NULL
;
736 m_hilightBrush
= new wxBrush
738 wxSystemSettings::GetColour
740 wxSYS_COLOUR_HIGHLIGHT
745 m_hilightUnfocusedBrush
= new wxBrush
747 wxSystemSettings::GetColour
749 wxSYS_COLOUR_BTNSHADOW
754 m_imageListNormal
= m_imageListButtons
=
755 m_imageListState
= (wxImageList
*) NULL
;
756 m_ownsImageListNormal
= m_ownsImageListButtons
=
757 m_ownsImageListState
= false;
760 m_isDragging
= false;
761 m_dropTarget
= m_oldSelection
= NULL
;
765 m_renameTimer
= NULL
;
770 m_dropEffectAboveItem
= false;
772 m_lastOnSame
= false;
774 #ifdef __WXMAC_CARBON__
775 m_normalFont
.MacCreateThemeFont( kThemeViewsFont
) ;
777 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
779 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
780 m_normalFont
.GetFamily(),
781 m_normalFont
.GetStyle(),
783 m_normalFont
.GetUnderlined(),
784 m_normalFont
.GetFaceName(),
785 m_normalFont
.GetEncoding());
788 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
793 const wxValidator
& wxVALIDATOR_PARAM(validator
),
794 const wxString
& name
)
798 wxGetOsVersion( &major
, &minor
);
800 style
&= ~wxTR_LINES_AT_ROOT
;
801 style
|= wxTR_NO_LINES
;
803 style
|= wxTR_ROW_LINES
;
806 wxScrolledWindow::Create( parent
, id
, pos
, size
,
807 style
|wxHSCROLL
|wxVSCROLL
, name
);
809 // If the tree display has no buttons, but does have
810 // connecting lines, we can use a narrower layout.
811 // It may not be a good idea to force this...
812 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
819 SetValidator( validator
);
822 wxVisualAttributes attr
= GetDefaultAttributes();
823 SetOwnForegroundColour( attr
.colFg
);
824 SetOwnBackgroundColour( attr
.colBg
);
826 SetOwnFont(attr
.font
);
828 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
829 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
836 wxGenericTreeCtrl::~wxGenericTreeCtrl()
838 delete m_hilightBrush
;
839 delete m_hilightUnfocusedBrush
;
843 delete m_renameTimer
;
846 if (m_ownsImageListNormal
)
847 delete m_imageListNormal
;
848 if (m_ownsImageListState
)
849 delete m_imageListState
;
850 if (m_ownsImageListButtons
)
851 delete m_imageListButtons
;
854 // -----------------------------------------------------------------------------
856 // -----------------------------------------------------------------------------
858 size_t wxGenericTreeCtrl::GetCount() const
866 size_t count
= m_anchor
->GetChildrenCount();
867 if ( !HasFlag(wxTR_HIDE_ROOT
) )
869 // take the root itself into account
876 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
878 m_indent
= (unsigned short) indent
;
882 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
884 m_spacing
= (unsigned short) spacing
;
889 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
890 bool recursively
) const
892 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
894 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
897 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
899 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
901 // if we will hide the root, make sure children are visible
902 m_anchor
->SetHasPlus();
904 CalculatePositions();
907 // right now, just sets the styles. Eventually, we may
908 // want to update the inherited styles, but right now
909 // none of the parents has updatable styles
910 m_windowStyle
= styles
;
914 // -----------------------------------------------------------------------------
915 // functions to work with tree items
916 // -----------------------------------------------------------------------------
918 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
920 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
922 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
925 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
926 wxTreeItemIcon which
) const
928 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
930 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
933 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
935 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
937 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
940 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
942 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
944 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
945 return pItem
->Attr().GetTextColour();
948 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
950 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
952 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
953 return pItem
->Attr().GetBackgroundColour();
956 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
958 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
960 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
961 return pItem
->Attr().GetFont();
964 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
966 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
969 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
970 pItem
->SetText(text
);
971 CalculateSize(pItem
, dc
);
975 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
977 wxTreeItemIcon which
)
979 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
981 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
982 pItem
->SetImage(image
, which
);
985 CalculateSize(pItem
, dc
);
989 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
991 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
996 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
999 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1001 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1003 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1004 pItem
->SetHasPlus(has
);
1008 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1010 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1012 // avoid redrawing the tree if no real change
1013 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1014 if ( pItem
->IsBold() != bold
)
1016 pItem
->SetBold(bold
);
1021 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1024 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1030 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1031 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1034 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1035 pItem
->Attr().SetTextColour(fg
);
1036 pItem
->Attr().SetBackgroundColour(bg
);
1040 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1041 const wxColour
& col
)
1043 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1045 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1046 pItem
->Attr().SetTextColour(col
);
1050 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1051 const wxColour
& col
)
1053 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1055 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1056 pItem
->Attr().SetBackgroundColour(col
);
1060 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1062 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1064 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1065 pItem
->Attr().SetFont(font
);
1069 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1071 wxScrolledWindow::SetFont(font
);
1073 m_normalFont
= font
;
1074 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1075 m_normalFont
.GetFamily(),
1076 m_normalFont
.GetStyle(),
1078 m_normalFont
.GetUnderlined(),
1079 m_normalFont
.GetFaceName(),
1080 m_normalFont
.GetEncoding());
1086 // -----------------------------------------------------------------------------
1087 // item status inquiries
1088 // -----------------------------------------------------------------------------
1090 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1092 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1094 // An item is only visible if it's not a descendant of a collapsed item
1095 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1096 wxGenericTreeItem
* parent
= pItem
->GetParent();
1099 if (!parent
->IsExpanded())
1101 parent
= parent
->GetParent();
1105 GetViewStart(& startX
, & startY
);
1107 wxSize clientSize
= GetClientSize();
1110 if (!GetBoundingRect(item
, rect
))
1112 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1114 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1116 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1122 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1124 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1126 // consider that the item does have children if it has the "+" button: it
1127 // might not have them (if it had never been expanded yet) but then it
1128 // could have them as well and it's better to err on this side rather than
1129 // disabling some operations which are restricted to the items with
1130 // children for an item which does have them
1131 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1134 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1136 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1138 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1141 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1143 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1145 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1148 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1150 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1152 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1155 // -----------------------------------------------------------------------------
1157 // -----------------------------------------------------------------------------
1159 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1161 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1163 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1166 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1167 wxTreeItemIdValue
& cookie
) const
1169 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1172 return GetNextChild(item
, cookie
);
1175 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1176 wxTreeItemIdValue
& cookie
) const
1178 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1180 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1182 // it's ok to cast cookie to size_t, we never have indices big enough to
1183 // overflow "void *"
1184 size_t *pIndex
= (size_t *)&cookie
;
1185 if ( *pIndex
< children
.Count() )
1187 return children
.Item((*pIndex
)++);
1191 // there are no more of them
1192 return wxTreeItemId();
1196 #if WXWIN_COMPATIBILITY_2_4
1198 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1201 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1204 return GetNextChild(item
, cookie
);
1207 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1210 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1212 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1213 if ( (size_t)cookie
< children
.Count() )
1215 return children
.Item((size_t)cookie
++);
1219 // there are no more of them
1220 return wxTreeItemId();
1224 #endif // WXWIN_COMPATIBILITY_2_4
1226 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1228 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1230 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1231 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1234 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1236 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1238 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1239 wxGenericTreeItem
*parent
= i
->GetParent();
1240 if ( parent
== NULL
)
1242 // root item doesn't have any siblings
1243 return wxTreeItemId();
1246 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1247 int index
= siblings
.Index(i
);
1248 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1250 size_t n
= (size_t)(index
+ 1);
1251 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1254 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1256 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1258 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1259 wxGenericTreeItem
*parent
= i
->GetParent();
1260 if ( parent
== NULL
)
1262 // root item doesn't have any siblings
1263 return wxTreeItemId();
1266 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1267 int index
= siblings
.Index(i
);
1268 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1270 return index
== 0 ? wxTreeItemId()
1271 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1274 // Only for internal use right now, but should probably be public
1275 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1277 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1279 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1281 // First see if there are any children.
1282 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1283 if (children
.GetCount() > 0)
1285 return children
.Item(0);
1289 // Try a sibling of this or ancestor instead
1290 wxTreeItemId p
= item
;
1291 wxTreeItemId toFind
;
1294 toFind
= GetNextSibling(p
);
1295 p
= GetItemParent(p
);
1296 } while (p
.IsOk() && !toFind
.IsOk());
1301 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1303 wxTreeItemId id
= GetRootItem();
1312 } while (id
.IsOk());
1314 return wxTreeItemId();
1317 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1319 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1321 wxTreeItemId id
= item
;
1324 while (id
= GetNext(id
), id
.IsOk())
1330 return wxTreeItemId();
1333 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1335 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1337 wxFAIL_MSG(wxT("not implemented"));
1339 return wxTreeItemId();
1342 // called by wxTextTreeCtrl when it marks itself for deletion
1343 void wxGenericTreeCtrl::ResetTextControl()
1348 // find the first item starting with the given prefix after the given item
1349 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1350 const wxString
& prefixOrig
) const
1352 // match is case insensitive as this is more convenient to the user: having
1353 // to press Shift-letter to go to the item starting with a capital letter
1354 // would be too bothersome
1355 wxString prefix
= prefixOrig
.Lower();
1357 // determine the starting point: we shouldn't take the current item (this
1358 // allows to switch between two items starting with the same letter just by
1359 // pressing it) but we shouldn't jump to the next one if the user is
1360 // continuing to type as otherwise he might easily skip the item he wanted
1361 wxTreeItemId id
= idParent
;
1362 if ( prefix
.length() == 1 )
1367 // look for the item starting with the given prefix after it
1368 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1373 // if we haven't found anything...
1376 // ... wrap to the beginning
1378 if ( HasFlag(wxTR_HIDE_ROOT
) )
1380 // can't select virtual root
1384 // and try all the items (stop when we get to the one we started from)
1385 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1394 // -----------------------------------------------------------------------------
1396 // -----------------------------------------------------------------------------
1398 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1400 const wxString
& text
,
1401 int image
, int selImage
,
1402 wxTreeItemData
*data
)
1404 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1407 // should we give a warning here?
1408 return AddRoot(text
, image
, selImage
, data
);
1411 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1413 wxGenericTreeItem
*item
=
1414 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1418 data
->m_pItem
= item
;
1421 parent
->Insert( item
, previous
);
1426 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1427 int image
, int selImage
,
1428 wxTreeItemData
*data
)
1430 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1432 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1434 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1435 image
, selImage
, data
);
1438 data
->m_pItem
= m_anchor
;
1441 if (HasFlag(wxTR_HIDE_ROOT
))
1443 // if root is hidden, make sure we can navigate
1445 m_anchor
->SetHasPlus();
1447 CalculatePositions();
1450 if (!HasFlag(wxTR_MULTIPLE
))
1452 m_current
= m_key_current
= m_anchor
;
1453 m_current
->SetHilight( true );
1459 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1460 const wxString
& text
,
1461 int image
, int selImage
,
1462 wxTreeItemData
*data
)
1464 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1467 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1468 const wxTreeItemId
& idPrevious
,
1469 const wxString
& text
,
1470 int image
, int selImage
,
1471 wxTreeItemData
*data
)
1473 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1476 // should we give a warning here?
1477 return AddRoot(text
, image
, selImage
, data
);
1481 if (idPrevious
.IsOk())
1483 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1484 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1485 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1488 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1491 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1493 const wxString
& text
,
1494 int image
, int selImage
,
1495 wxTreeItemData
*data
)
1497 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1500 // should we give a warning here?
1501 return AddRoot(text
, image
, selImage
, data
);
1504 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1507 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1508 const wxString
& text
,
1509 int image
, int selImage
,
1510 wxTreeItemData
*data
)
1512 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1515 // should we give a warning here?
1516 return AddRoot(text
, image
, selImage
, data
);
1519 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1520 image
, selImage
, data
);
1523 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1525 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1526 event
.m_item
= item
;
1527 event
.SetEventObject( this );
1528 ProcessEvent( event
);
1531 // Don't leave edit or selection on a child which is about to disappear
1532 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1534 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1535 m_textCtrl
->StopEditing();
1537 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1538 m_key_current
= NULL
;
1540 if (IsDescendantOf(item
, m_select_me
)) {
1543 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1544 m_current
->SetHilight( false );
1550 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1552 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1554 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1555 ChildrenClosing(item
);
1556 item
->DeleteChildren(this);
1559 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1561 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1563 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1565 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1567 // can't delete the item being edited, cancel editing it first
1568 m_textCtrl
->StopEditing();
1571 wxGenericTreeItem
*parent
= item
->GetParent();
1573 // don't keep stale pointers around!
1574 if ( IsDescendantOf(item
, m_key_current
) )
1576 // Don't silently change the selection:
1577 // do it properly in idle time, so event
1578 // handlers get called.
1580 // m_key_current = parent;
1581 m_key_current
= NULL
;
1584 // m_select_me records whether we need to select
1585 // a different item, in idle time.
1586 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1588 m_select_me
= parent
;
1591 if ( IsDescendantOf(item
, m_current
) )
1593 // Don't silently change the selection:
1594 // do it properly in idle time, so event
1595 // handlers get called.
1597 // m_current = parent;
1599 m_select_me
= parent
;
1602 // remove the item from the tree
1605 parent
->GetChildren().Remove( item
); // remove by value
1607 else // deleting the root
1609 // nothing will be left in the tree
1613 // and delete all of its children and the item itself now
1614 item
->DeleteChildren(this);
1615 SendDeleteEvent(item
);
1617 if (item
== m_select_me
)
1623 void wxGenericTreeCtrl::DeleteAllItems()
1631 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1633 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1635 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1636 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1637 _T("can't expand hidden root") );
1639 if ( !item
->HasPlus() )
1642 if ( item
->IsExpanded() )
1645 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1646 event
.m_item
= item
;
1647 event
.SetEventObject( this );
1649 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1651 // cancelled by program
1656 CalculatePositions();
1658 RefreshSubtree(item
);
1660 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1661 ProcessEvent( event
);
1664 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1666 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1669 if ( !IsExpanded(item
) )
1673 wxTreeItemIdValue cookie
;
1674 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1675 while ( child
.IsOk() )
1679 child
= GetNextChild(item
, cookie
);
1683 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1685 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1686 _T("can't collapse hidden root") );
1688 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1690 if ( !item
->IsExpanded() )
1693 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1694 event
.m_item
= item
;
1695 event
.SetEventObject( this );
1696 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1698 // cancelled by program
1702 ChildrenClosing(item
);
1705 #if 0 // TODO why should items be collapsed recursively?
1706 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1707 size_t count
= children
.Count();
1708 for ( size_t n
= 0; n
< count
; n
++ )
1710 Collapse(children
[n
]);
1714 CalculatePositions();
1716 RefreshSubtree(item
);
1718 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1719 ProcessEvent( event
);
1722 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1725 DeleteChildren(item
);
1728 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1730 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1732 if (item
->IsExpanded())
1738 void wxGenericTreeCtrl::Unselect()
1742 m_current
->SetHilight( false );
1743 RefreshLine( m_current
);
1750 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1752 if (item
->IsSelected())
1754 item
->SetHilight(false);
1758 if (item
->HasChildren())
1760 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1761 size_t count
= children
.Count();
1762 for ( size_t n
= 0; n
< count
; ++n
)
1764 UnselectAllChildren(children
[n
]);
1769 void wxGenericTreeCtrl::UnselectAll()
1771 wxTreeItemId rootItem
= GetRootItem();
1773 // the tree might not have the root item at all
1776 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1780 // Recursive function !
1781 // To stop we must have crt_item<last_item
1783 // Tag all next children, when no more children,
1784 // Move to parent (not to tag)
1785 // Keep going... if we found last_item, we stop.
1786 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1788 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1790 if (parent
== NULL
) // This is root item
1791 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1793 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1794 int index
= children
.Index(crt_item
);
1795 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1797 size_t count
= children
.Count();
1798 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1800 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1803 return TagNextChildren(parent
, last_item
, select
);
1806 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1808 crt_item
->SetHilight(select
);
1809 RefreshLine(crt_item
);
1811 if (crt_item
==last_item
)
1814 if (crt_item
->HasChildren())
1816 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1817 size_t count
= children
.Count();
1818 for ( size_t n
= 0; n
< count
; ++n
)
1820 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1828 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1832 // item2 is not necessary after item1
1833 // choice first' and 'last' between item1 and item2
1834 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1835 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1837 bool select
= m_current
->IsSelected();
1839 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1842 TagNextChildren(first
,last
,select
);
1845 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1846 bool unselect_others
,
1847 bool extended_select
)
1849 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1853 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1854 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1856 //wxCHECK_RET( ( (!unselect_others) && is_single),
1857 // wxT("this is a single selection tree") );
1859 // to keep going anyhow !!!
1862 if (item
->IsSelected())
1863 return; // nothing to do
1864 unselect_others
= true;
1865 extended_select
= false;
1867 else if ( unselect_others
&& item
->IsSelected() )
1869 // selection change if there is more than one item currently selected
1870 wxArrayTreeItemIds selected_items
;
1871 if ( GetSelections(selected_items
) == 1 )
1875 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1876 event
.m_item
= item
;
1877 event
.m_itemOld
= m_current
;
1878 event
.SetEventObject( this );
1879 // TODO : Here we don't send any selection mode yet !
1881 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1884 wxTreeItemId parent
= GetItemParent( itemId
);
1885 while (parent
.IsOk())
1887 if (!IsExpanded(parent
))
1890 parent
= GetItemParent( parent
);
1893 EnsureVisible( itemId
);
1896 if (unselect_others
)
1898 if (is_single
) Unselect(); // to speed up thing
1903 if (extended_select
)
1907 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1910 // don't change the mark (m_current)
1911 SelectItemRange(m_current
, item
);
1915 bool select
= true; // the default
1917 // Check if we need to toggle hilight (ctrl mode)
1918 if (!unselect_others
)
1919 select
=!item
->IsSelected();
1921 m_current
= m_key_current
= item
;
1922 m_current
->SetHilight(select
);
1923 RefreshLine( m_current
);
1926 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1927 GetEventHandler()->ProcessEvent( event
);
1930 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1934 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
1938 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1939 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1941 item
->SetHilight(false);
1946 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1947 wxArrayTreeItemIds
&array
) const
1949 if ( item
->IsSelected() )
1950 array
.Add(wxTreeItemId(item
));
1952 if ( item
->HasChildren() )
1954 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1955 size_t count
= children
.GetCount();
1956 for ( size_t n
= 0; n
< count
; ++n
)
1957 FillArray(children
[n
], array
);
1961 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1964 wxTreeItemId idRoot
= GetRootItem();
1965 if ( idRoot
.IsOk() )
1967 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1969 //else: the tree is empty, so no selections
1971 return array
.Count();
1974 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1976 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1978 if (!item
.IsOk()) return;
1980 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1982 // first expand all parent branches
1983 wxGenericTreeItem
*parent
= gitem
->GetParent();
1985 if ( HasFlag(wxTR_HIDE_ROOT
) )
1987 while ( parent
&& parent
!= m_anchor
)
1990 parent
= parent
->GetParent();
1998 parent
= parent
->GetParent();
2002 //if (parent) CalculatePositions();
2007 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
2009 if (!item
.IsOk()) return;
2011 // We have to call this here because the label in
2012 // question might just have been added and no screen
2013 // update taken place.
2015 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2020 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2022 // now scroll to the item
2023 int item_y
= gitem
->GetY();
2027 GetViewStart( &start_x
, &start_y
);
2028 start_y
*= PIXELS_PER_UNIT
;
2032 GetClientSize( &client_w
, &client_h
);
2034 if (item_y
< start_y
+3)
2039 m_anchor
->GetSize( x
, y
, this );
2040 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2041 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2042 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2043 // Item should appear at top
2044 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2046 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2051 m_anchor
->GetSize( x
, y
, this );
2052 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2053 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2054 item_y
+= PIXELS_PER_UNIT
+2;
2055 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2056 // Item should appear at bottom
2057 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
);
2061 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2062 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2064 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2065 wxGenericTreeItem
**item2
)
2067 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2069 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2072 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
2073 const wxTreeItemId
& item2
)
2075 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2078 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2080 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2082 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2084 wxCHECK_RET( !s_treeBeingSorted
,
2085 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2087 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2088 if ( children
.Count() > 1 )
2092 s_treeBeingSorted
= this;
2093 children
.Sort(tree_ctrl_compare_func
);
2094 s_treeBeingSorted
= NULL
;
2096 //else: don't make the tree dirty as nothing changed
2099 wxImageList
*wxGenericTreeCtrl::GetImageList() const
2101 return m_imageListNormal
;
2104 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
2106 return m_imageListButtons
;
2109 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
2111 return m_imageListState
;
2114 void wxGenericTreeCtrl::CalculateLineHeight()
2116 wxClientDC
dc(this);
2117 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2119 if ( m_imageListNormal
)
2121 // Calculate a m_lineHeight value from the normal Image sizes.
2122 // May be toggle off. Then wxGenericTreeCtrl will spread when
2123 // necessary (which might look ugly).
2124 int n
= m_imageListNormal
->GetImageCount();
2125 for (int i
= 0; i
< n
; i
++)
2127 int width
= 0, height
= 0;
2128 m_imageListNormal
->GetSize(i
, width
, height
);
2129 if (height
> m_lineHeight
) m_lineHeight
= height
;
2133 if (m_imageListButtons
)
2135 // Calculate a m_lineHeight value from the Button image sizes.
2136 // May be toggle off. Then wxGenericTreeCtrl will spread when
2137 // necessary (which might look ugly).
2138 int n
= m_imageListButtons
->GetImageCount();
2139 for (int i
= 0; i
< n
; i
++)
2141 int width
= 0, height
= 0;
2142 m_imageListButtons
->GetSize(i
, width
, height
);
2143 if (height
> m_lineHeight
) m_lineHeight
= height
;
2147 if (m_lineHeight
< 30)
2148 m_lineHeight
+= 2; // at least 2 pixels
2150 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2153 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2155 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2156 m_imageListNormal
= imageList
;
2157 m_ownsImageListNormal
= false;
2159 // Don't do any drawing if we're setting the list to NULL,
2160 // since we may be in the process of deleting the tree control.
2162 CalculateLineHeight();
2165 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2167 if (m_ownsImageListState
) delete m_imageListState
;
2168 m_imageListState
= imageList
;
2169 m_ownsImageListState
= false;
2172 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2174 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2175 m_imageListButtons
= imageList
;
2176 m_ownsImageListButtons
= false;
2178 CalculateLineHeight();
2181 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2183 SetImageList(imageList
);
2184 m_ownsImageListNormal
= true;
2187 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2189 SetStateImageList(imageList
);
2190 m_ownsImageListState
= true;
2193 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2195 SetButtonsImageList(imageList
);
2196 m_ownsImageListButtons
= true;
2199 // -----------------------------------------------------------------------------
2201 // -----------------------------------------------------------------------------
2203 void wxGenericTreeCtrl::AdjustMyScrollbars()
2208 m_anchor
->GetSize( x
, y
, this );
2209 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2210 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2211 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2212 int y_pos
= GetScrollPos( wxVERTICAL
);
2213 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2217 SetScrollbars( 0, 0, 0, 0 );
2221 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2223 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2224 return item
->GetHeight();
2226 return m_lineHeight
;
2229 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2231 // TODO implement "state" icon on items
2233 wxTreeItemAttr
*attr
= item
->GetAttributes();
2234 if ( attr
&& attr
->HasFont() )
2235 dc
.SetFont(attr
->GetFont());
2236 else if (item
->IsBold())
2237 dc
.SetFont(m_boldFont
);
2239 long text_w
= 0, text_h
= 0;
2240 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2242 int image_h
= 0, image_w
= 0;
2243 int image
= item
->GetCurrentImage();
2244 if ( image
!= NO_IMAGE
)
2246 if ( m_imageListNormal
)
2248 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2257 int total_h
= GetLineHeight(item
);
2258 bool drawItemBackground
= false;
2260 if ( item
->IsSelected() )
2262 // under mac selections are only a rectangle in case they don't have the focus
2266 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2267 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2271 dc
.SetBrush( *m_hilightBrush
) ;
2274 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2276 drawItemBackground
= true;
2281 if ( attr
&& attr
->HasBackgroundColour() )
2283 drawItemBackground
= true;
2284 colBg
= attr
->GetBackgroundColour();
2288 colBg
= m_backgroundColour
;
2290 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2293 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2295 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2299 DoGetPosition(&x
, &y
);
2301 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2305 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2307 // If it's selected, and there's an image, then we should
2308 // take care to leave the area under the image painted in the
2309 // background colour.
2310 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2311 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2313 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2314 // don't allow backgrounds to be customized. Not drawing the background,
2315 // except for custom item backgrounds, works for both kinds of theme.
2316 else if (drawItemBackground
)
2318 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2319 item
->GetWidth()+2, total_h
-offset
);
2323 if ( image
!= NO_IMAGE
)
2325 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2326 m_imageListNormal
->Draw( image
, dc
,
2328 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2329 wxIMAGELIST_DRAW_TRANSPARENT
);
2330 dc
.DestroyClippingRegion();
2333 dc
.SetBackgroundMode(wxTRANSPARENT
);
2334 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2335 dc
.DrawText( item
->GetText(),
2336 (wxCoord
)(image_w
+ item
->GetX()),
2337 (wxCoord
)(item
->GetY() + extraH
));
2339 // restore normal font
2340 dc
.SetFont( m_normalFont
);
2343 // Now y stands for the top of the item, whereas it used to stand for middle !
2344 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2346 int x
= level
*m_indent
;
2347 if (!HasFlag(wxTR_HIDE_ROOT
))
2351 else if (level
== 0)
2353 // always expand hidden root
2355 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2356 int count
= children
.Count();
2362 PaintLevel(children
[n
], dc
, 1, y
);
2363 } while (++n
< count
);
2365 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2367 // draw line down to last child
2368 origY
+= GetLineHeight(children
[0])>>1;
2369 oldY
+= GetLineHeight(children
[n
-1])>>1;
2370 dc
.DrawLine(3, origY
, 3, oldY
);
2376 item
->SetX(x
+m_spacing
);
2379 int h
= GetLineHeight(item
);
2381 int y_mid
= y_top
+ (h
>>1);
2384 int exposed_x
= dc
.LogicalToDeviceX(0);
2385 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2387 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2391 // don't draw rect outline if we already have the
2392 // background color under Mac
2393 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2394 #endif // !__WXMAC__
2398 if ( item
->IsSelected() )
2400 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2404 wxTreeItemAttr
*attr
= item
->GetAttributes();
2405 if (attr
&& attr
->HasTextColour())
2406 colText
= attr
->GetTextColour();
2408 colText
= GetForegroundColour();
2412 dc
.SetTextForeground(colText
);
2416 PaintItem(item
, dc
);
2418 if (HasFlag(wxTR_ROW_LINES
))
2420 // if the background colour is white, choose a
2421 // contrasting color for the lines
2422 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2423 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2424 dc
.DrawLine(0, y_top
, 10000, y_top
);
2425 dc
.DrawLine(0, y
, 10000, y
);
2428 // restore DC objects
2429 dc
.SetBrush(*wxWHITE_BRUSH
);
2430 dc
.SetPen(m_dottedPen
);
2431 dc
.SetTextForeground(*wxBLACK
);
2433 if ( !HasFlag(wxTR_NO_LINES
) )
2435 // draw the horizontal line here
2437 if (x
> (signed)m_indent
)
2438 x_start
-= m_indent
;
2439 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2441 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2444 // should the item show a button?
2445 if ( item
->HasPlus() && HasButtons() )
2447 if ( m_imageListButtons
)
2449 // draw the image button here
2452 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2453 : wxTreeItemIcon_Normal
;
2454 if ( item
->IsSelected() )
2455 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2457 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2458 int xx
= x
- image_w
/2;
2459 int yy
= y_mid
- image_h
/2;
2461 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2462 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2463 wxIMAGELIST_DRAW_TRANSPARENT
);
2465 else // no custom buttons
2467 static const int wImage
= 9;
2468 static const int hImage
= 9;
2471 if (item
->IsExpanded())
2472 flag
|= wxCONTROL_EXPANDED
;
2473 if (item
== m_underMouse
)
2474 flag
|= wxCONTROL_CURRENT
;
2476 wxRendererNative::Get().DrawTreeItemButton
2480 wxRect(x
- wImage
/2,
2489 if (item
->IsExpanded())
2491 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2492 int count
= children
.Count();
2499 PaintLevel(children
[n
], dc
, level
, y
);
2500 } while (++n
< count
);
2502 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2504 // draw line down to last child
2505 oldY
+= GetLineHeight(children
[n
-1])>>1;
2506 if (HasButtons()) y_mid
+= 5;
2508 // Only draw the portion of the line that is visible, in case it is huge
2509 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2510 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2511 yOrigin
= abs(yOrigin
);
2512 GetClientSize(&width
, &height
);
2514 // Move end points to the begining/end of the view?
2515 if (y_mid
< yOrigin
)
2517 if (oldY
> yOrigin
+ height
)
2518 oldY
= yOrigin
+ height
;
2520 // after the adjustments if y_mid is larger than oldY then the line
2521 // isn't visible at all so don't draw anything
2523 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2529 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2533 if ( item
->HasPlus() )
2535 // it's a folder, indicate it by a border
2540 // draw a line under the drop target because the item will be
2542 DrawLine(item
, !m_dropEffectAboveItem
);
2545 SetCursor(wxCURSOR_BULLSEYE
);
2550 SetCursor(wxCURSOR_NO_ENTRY
);
2554 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2556 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2558 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2560 wxClientDC
dc(this);
2562 dc
.SetLogicalFunction(wxINVERT
);
2563 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2565 int w
= i
->GetWidth() + 2;
2566 int h
= GetLineHeight(i
) + 2;
2568 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2571 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2573 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2575 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2577 wxClientDC
dc(this);
2579 dc
.SetLogicalFunction(wxINVERT
);
2585 y
+= GetLineHeight(i
) - 1;
2588 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2591 // -----------------------------------------------------------------------------
2592 // wxWidgets callbacks
2593 // -----------------------------------------------------------------------------
2595 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2603 dc
.SetFont( m_normalFont
);
2604 dc
.SetPen( m_dottedPen
);
2606 // this is now done dynamically
2607 //if(GetImageList() == NULL)
2608 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2611 PaintLevel( m_anchor
, dc
, 0, y
);
2614 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2623 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2632 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2634 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2635 te
.m_evtKey
= event
;
2636 te
.SetEventObject( this );
2637 if ( GetEventHandler()->ProcessEvent( te
) )
2639 // intercepted by the user code
2643 if ( (m_current
== 0) || (m_key_current
== 0) )
2649 // how should the selection work for this event?
2650 bool is_multiple
, extended_select
, unselect_others
;
2651 EventFlagsToSelType(GetWindowStyleFlag(),
2654 is_multiple
, extended_select
, unselect_others
);
2658 // * : Expand all/Collapse all
2659 // ' ' | return : activate
2660 // up : go up (not last children!)
2662 // left : go to parent
2663 // right : open if parent and go next
2664 // home : go to root
2665 // end : go to last item without opening parents
2666 // alnum : start or continue searching for the item with this prefix
2667 int keyCode
= event
.GetKeyCode();
2672 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2680 if ( !IsExpanded(m_current
) )
2683 ExpandAll(m_current
);
2686 //else: fall through to Collapse() it
2690 if (IsExpanded(m_current
))
2692 Collapse(m_current
);
2698 // Use the item's bounding rectangle to determine position for the event
2700 GetBoundingRect(m_current
, ItemRect
, true);
2702 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_MENU
, GetId() );
2703 event
.m_item
= m_current
;
2704 // Use the left edge, vertical middle
2705 event
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
2706 ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2707 event
.SetEventObject( this );
2708 GetEventHandler()->ProcessEvent( event
);
2713 if ( !event
.HasModifiers() )
2715 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2716 event
.m_item
= m_current
;
2717 event
.SetEventObject( this );
2718 GetEventHandler()->ProcessEvent( event
);
2721 // in any case, also generate the normal key event for this key,
2722 // even if we generated the ACTIVATED event above: this is what
2723 // wxMSW does and it makes sense because you might not want to
2724 // process ACTIVATED event at all and handle Space and Return
2725 // directly (and differently) which would be impossible otherwise
2729 // up goes to the previous sibling or to the last
2730 // of its children if it's expanded
2733 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2736 prev
= GetItemParent( m_key_current
);
2737 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2739 break; // don't go to root if it is hidden
2743 wxTreeItemIdValue cookie
;
2744 wxTreeItemId current
= m_key_current
;
2745 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2746 if (current
== GetFirstChild( prev
, cookie
))
2748 // otherwise we return to where we came from
2749 DoSelectItem( prev
, unselect_others
, extended_select
);
2750 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2757 while ( IsExpanded(prev
) && HasChildren(prev
) )
2759 wxTreeItemId child
= GetLastChild(prev
);
2766 DoSelectItem( prev
, unselect_others
, extended_select
);
2767 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2772 // left arrow goes to the parent
2775 wxTreeItemId prev
= GetItemParent( m_current
);
2776 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2778 // don't go to root if it is hidden
2779 prev
= GetPrevSibling( m_current
);
2783 DoSelectItem( prev
, unselect_others
, extended_select
);
2789 // this works the same as the down arrow except that we
2790 // also expand the item if it wasn't expanded yet
2796 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2798 wxTreeItemIdValue cookie
;
2799 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2800 DoSelectItem( child
, unselect_others
, extended_select
);
2801 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2805 wxTreeItemId next
= GetNextSibling( m_key_current
);
2808 wxTreeItemId current
= m_key_current
;
2809 while (current
.IsOk() && !next
)
2811 current
= GetItemParent( current
);
2812 if (current
) next
= GetNextSibling( current
);
2817 DoSelectItem( next
, unselect_others
, extended_select
);
2818 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2824 // <End> selects the last visible tree item
2827 wxTreeItemId last
= GetRootItem();
2829 while ( last
.IsOk() && IsExpanded(last
) )
2831 wxTreeItemId lastChild
= GetLastChild(last
);
2833 // it may happen if the item was expanded but then all of
2834 // its children have been deleted - so IsExpanded() returned
2835 // true, but GetLastChild() returned invalid item
2844 DoSelectItem( last
, unselect_others
, extended_select
);
2849 // <Home> selects the root item
2852 wxTreeItemId prev
= GetRootItem();
2856 if ( HasFlag(wxTR_HIDE_ROOT
) )
2858 wxTreeItemIdValue cookie
;
2859 prev
= GetFirstChild(prev
, cookie
);
2864 DoSelectItem( prev
, unselect_others
, extended_select
);
2869 // do not use wxIsalnum() here
2870 if ( !event
.HasModifiers() &&
2871 ((keyCode
>= '0' && keyCode
<= '9') ||
2872 (keyCode
>= 'a' && keyCode
<= 'z') ||
2873 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2875 // find the next item starting with the given prefix
2876 wxChar ch
= (wxChar
)keyCode
;
2878 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2889 // also start the timer to reset the current prefix if the user
2890 // doesn't press any more alnum keys soon -- we wouldn't want
2891 // to use this prefix for a new item search
2894 m_findTimer
= new wxTreeFindTimer(this);
2897 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2906 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2908 // JACS: removed wxYieldIfNeeded() because it can cause the window
2909 // to be deleted from under us if a close window event is pending
2914 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2915 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2916 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2917 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2918 if (flags
) return wxTreeItemId();
2920 if (m_anchor
== NULL
)
2922 flags
= wxTREE_HITTEST_NOWHERE
;
2923 return wxTreeItemId();
2926 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2930 flags
= wxTREE_HITTEST_NOWHERE
;
2931 return wxTreeItemId();
2936 // get the bounding rectangle of the item (or of its label only)
2937 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2939 bool WXUNUSED(textOnly
)) const
2941 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2943 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2946 GetViewStart(& startX
, & startY
);
2948 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2949 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2950 rect
.width
= i
->GetWidth();
2951 //rect.height = i->GetHeight();
2952 rect
.height
= GetLineHeight(i
);
2957 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2959 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2961 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2963 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2964 te
.m_item
= itemEdit
;
2965 te
.SetEventObject( this );
2966 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2972 // We have to call this here because the label in
2973 // question might just have been added and no screen
2974 // update taken place.
2976 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2982 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2984 m_textCtrl
->SetFocus();
2987 // returns a pointer to the text edit control if the item is being
2988 // edited, NULL otherwise (it's assumed that no more than one item may
2989 // be edited simultaneously)
2990 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2995 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2996 const wxString
& value
)
2998 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
3000 le
.SetEventObject( this );
3002 le
.m_editCancelled
= false;
3004 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3007 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3009 // let owner know that the edit was cancelled
3010 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
3012 le
.SetEventObject( this );
3013 le
.m_label
= wxEmptyString
;
3014 le
.m_editCancelled
= true;
3016 GetEventHandler()->ProcessEvent( le
);
3019 void wxGenericTreeCtrl::OnRenameTimer()
3024 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3026 if ( !m_anchor
) return;
3028 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3030 // Is the mouse over a tree item button?
3032 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3033 wxGenericTreeItem
*underMouse
= thisItem
;
3035 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3036 #endif // wxUSE_TOOLTIPS
3039 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3040 (!event
.LeftIsDown()) &&
3042 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3050 if (underMouse
!= m_underMouse
)
3054 // unhighlight old item
3055 wxGenericTreeItem
*tmp
= m_underMouse
;
3056 m_underMouse
= NULL
;
3060 m_underMouse
= underMouse
;
3062 RefreshLine( m_underMouse
);
3066 // Determines what item we are hovering over and need a tooltip for
3067 wxTreeItemId hoverItem
= thisItem
;
3069 // We do not want a tooltip if we are dragging, or if the rename timer is running
3070 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3072 // Ask the tree control what tooltip (if any) should be shown
3073 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, GetId());
3074 hevent
.m_item
= hoverItem
;
3075 hevent
.SetEventObject(this);
3077 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3079 SetToolTip(hevent
.m_label
);
3084 // we process left mouse up event (enables in-place edit), right down
3085 // (pass to the user code), left dbl click (activate item) and
3086 // dragging/moving events for items drag-and-drop
3087 if ( !(event
.LeftDown() ||
3089 event
.RightDown() ||
3090 event
.LeftDClick() ||
3092 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3101 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3103 if ( event
.Dragging() && !m_isDragging
)
3105 if (m_dragCount
== 0)
3110 if (m_dragCount
!= 3)
3112 // wait until user drags a bit further...
3116 wxEventType command
= event
.RightIsDown()
3117 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3118 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3120 wxTreeEvent
nevent( command
, GetId() );
3121 nevent
.m_item
= m_current
;
3122 nevent
.SetEventObject(this);
3123 nevent
.SetPoint(pt
);
3125 // by default the dragging is not supported, the user code must
3126 // explicitly allow the event for it to take place
3129 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3131 // we're going to drag this item
3132 m_isDragging
= true;
3134 // remember the old cursor because we will change it while
3136 m_oldCursor
= m_cursor
;
3138 // in a single selection control, hide the selection temporarily
3139 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3141 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3143 if ( m_oldSelection
)
3145 m_oldSelection
->SetHilight(false);
3146 RefreshLine(m_oldSelection
);
3153 else if ( event
.Dragging() )
3155 if ( item
!= m_dropTarget
)
3157 // unhighlight the previous drop target
3158 DrawDropEffect(m_dropTarget
);
3160 m_dropTarget
= item
;
3162 // highlight the current drop target if any
3163 DrawDropEffect(m_dropTarget
);
3165 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3172 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3174 // erase the highlighting
3175 DrawDropEffect(m_dropTarget
);
3177 if ( m_oldSelection
)
3179 m_oldSelection
->SetHilight(true);
3180 RefreshLine(m_oldSelection
);
3181 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3184 // generate the drag end event
3185 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3187 event
.m_item
= item
;
3188 event
.m_pointDrag
= pt
;
3189 event
.SetEventObject(this);
3191 (void)GetEventHandler()->ProcessEvent(event
);
3193 m_isDragging
= false;
3194 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3198 SetCursor(m_oldCursor
);
3200 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3208 // If we got to this point, we are not dragging or moving the mouse.
3209 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3210 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3211 // We skip even if we didn't hit an item because we still should
3212 // restore focus to the tree control even if we didn't exactly hit an item.
3213 if ( event
.LeftDown() )
3218 // here we process only the messages which happen on tree items
3222 if (item
== NULL
) return; /* we hit the blank area */
3224 if ( event
.RightDown() )
3226 // If the item is already selected, do not update the selection.
3227 // Multi-selections should not be cleared if a selected item is clicked.
3228 if (!IsSelected(item
))
3230 DoSelectItem(item
, true, false);
3233 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3234 nevent
.m_item
= item
;
3235 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3236 nevent
.SetEventObject(this);
3237 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3239 // Consistent with MSW (for now), send the ITEM_MENU *after*
3240 // the RIGHT_CLICK event. TODO: This behavior may change.
3241 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, GetId());
3242 nevent2
.m_item
= item
;
3243 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3244 nevent2
.SetEventObject(this);
3245 GetEventHandler()->ProcessEvent(nevent2
);
3247 else if ( event
.LeftUp() )
3249 // this facilitates multiple-item drag-and-drop
3251 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3253 wxArrayTreeItemIds selections
;
3254 size_t count
= GetSelections(selections
);
3260 DoSelectItem(item
, true, false);
3266 if ( (item
== m_current
) &&
3267 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3268 HasFlag(wxTR_EDIT_LABELS
) )
3270 if ( m_renameTimer
)
3272 if ( m_renameTimer
->IsRunning() )
3273 m_renameTimer
->Stop();
3277 m_renameTimer
= new wxTreeRenameTimer( this );
3280 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3283 m_lastOnSame
= false;
3286 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3288 if ( event
.LeftDown() )
3290 m_lastOnSame
= item
== m_current
;
3293 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3295 // only toggle the item for a single click, double click on
3296 // the button doesn't do anything (it toggles the item twice)
3297 if ( event
.LeftDown() )
3302 // don't select the item if the button was clicked
3307 // clear the previously selected items, if the
3308 // user clicked outside of the present selection.
3309 // otherwise, perform the deselection on mouse-up.
3310 // this allows multiple drag and drop to work.
3311 // but if Cmd is down, toggle selection of the clicked item
3312 if (!IsSelected(item
) || event
.CmdDown())
3314 // how should the selection work for this event?
3315 bool is_multiple
, extended_select
, unselect_others
;
3316 EventFlagsToSelType(GetWindowStyleFlag(),
3319 is_multiple
, extended_select
, unselect_others
);
3321 DoSelectItem(item
, unselect_others
, extended_select
);
3325 // For some reason, Windows isn't recognizing a left double-click,
3326 // so we need to simulate it here. Allow 200 milliseconds for now.
3327 if ( event
.LeftDClick() )
3329 // double clicking should not start editing the item label
3330 if ( m_renameTimer
)
3331 m_renameTimer
->Stop();
3333 m_lastOnSame
= false;
3335 // send activate event first
3336 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3337 nevent
.m_item
= item
;
3338 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3339 nevent
.SetEventObject( this );
3340 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3342 // if the user code didn't process the activate event,
3343 // handle it ourselves by toggling the item when it is
3345 if ( item
->HasPlus() )
3355 void wxGenericTreeCtrl::OnInternalIdle()
3357 wxWindow::OnInternalIdle();
3359 // Check if we need to select the root item
3360 // because nothing else has been selected.
3361 // Delaying it means that we can invoke event handlers
3362 // as required, when a first item is selected.
3363 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3366 SelectItem(m_select_me
);
3367 else if (GetRootItem().IsOk())
3368 SelectItem(GetRootItem());
3371 /* after all changes have been done to the tree control,
3372 * we actually redraw the tree when everything is over */
3374 if (!m_dirty
) return;
3375 if (m_freezeCount
) return;
3379 CalculatePositions();
3381 AdjustMyScrollbars();
3384 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3389 wxTreeItemAttr
*attr
= item
->GetAttributes();
3390 if ( attr
&& attr
->HasFont() )
3391 dc
.SetFont(attr
->GetFont());
3392 else if ( item
->IsBold() )
3393 dc
.SetFont(m_boldFont
);
3395 dc
.SetFont(m_normalFont
);
3397 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3400 // restore normal font
3401 dc
.SetFont( m_normalFont
);
3405 int image
= item
->GetCurrentImage();
3406 if ( image
!= NO_IMAGE
)
3408 if ( m_imageListNormal
)
3410 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3415 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3418 total_h
+= 2; // at least 2 pixels
3420 total_h
+= total_h
/10; // otherwise 10% extra spacing
3422 item
->SetHeight(total_h
);
3423 if (total_h
>m_lineHeight
)
3424 m_lineHeight
=total_h
;
3426 item
->SetWidth(image_w
+text_w
+2);
3429 // -----------------------------------------------------------------------------
3430 // for developper : y is now the top of the level
3431 // not the middle of it !
3432 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3434 int x
= level
*m_indent
;
3435 if (!HasFlag(wxTR_HIDE_ROOT
))
3439 else if (level
== 0)
3441 // a hidden root is not evaluated, but its
3442 // children are always calculated
3446 CalculateSize( item
, dc
);
3449 item
->SetX( x
+m_spacing
);
3451 y
+= GetLineHeight(item
);
3453 if ( !item
->IsExpanded() )
3455 // we don't need to calculate collapsed branches
3460 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3461 size_t n
, count
= children
.Count();
3463 for (n
= 0; n
< count
; ++n
)
3464 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3467 void wxGenericTreeCtrl::CalculatePositions()
3469 if ( !m_anchor
) return;
3471 wxClientDC
dc(this);
3474 dc
.SetFont( m_normalFont
);
3476 dc
.SetPen( m_dottedPen
);
3477 //if(GetImageList() == NULL)
3478 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3481 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3484 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3486 if (m_dirty
) return;
3487 if (m_freezeCount
) return;
3489 wxSize client
= GetClientSize();
3492 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3493 rect
.width
= client
.x
;
3494 rect
.height
= client
.y
;
3496 Refresh(true, &rect
);
3498 AdjustMyScrollbars();
3501 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3503 if (m_dirty
) return;
3504 if (m_freezeCount
) return;
3507 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3508 rect
.width
= GetClientSize().x
;
3509 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3511 Refresh(true, &rect
);
3514 void wxGenericTreeCtrl::RefreshSelected()
3516 if (m_freezeCount
) return;
3518 // TODO: this is awfully inefficient, we should keep the list of all
3519 // selected items internally, should be much faster
3521 RefreshSelectedUnder(m_anchor
);
3524 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3526 if (m_freezeCount
) return;
3528 if ( item
->IsSelected() )
3531 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3532 size_t count
= children
.GetCount();
3533 for ( size_t n
= 0; n
< count
; n
++ )
3535 RefreshSelectedUnder(children
[n
]);
3539 void wxGenericTreeCtrl::Freeze()
3544 void wxGenericTreeCtrl::Thaw()
3546 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen tree control?") );
3548 if ( !--m_freezeCount
)
3554 // ----------------------------------------------------------------------------
3555 // changing colours: we need to refresh the tree control
3556 // ----------------------------------------------------------------------------
3558 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3560 if ( !wxWindow::SetBackgroundColour(colour
) )
3563 if (m_freezeCount
) return true;
3570 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3572 if ( !wxWindow::SetForegroundColour(colour
) )
3575 if (m_freezeCount
) return true;
3582 // Process the tooltip event, to speed up event processing.
3583 // Doesn't actually get a tooltip.
3584 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3590 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
3592 // something is better than nothing...
3593 // 100x80 is what the MSW version will get from the default
3594 // wxControl::DoGetBestSize
3595 return wxSize(100,80);
3599 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3600 // be removed, as well as the #else case below.
3601 #define _USE_VISATTR 0
3604 #include "wx/listbox.h"
3610 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3612 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3616 // Use the same color scheme as wxListBox
3617 return wxListBox::GetClassDefaultAttributes(variant
);
3619 wxVisualAttributes attr
;
3620 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3621 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3622 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3627 #if WXWIN_COMPATIBILITY_2_4
3629 int wxGenericTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
3631 return GetItemImage(item
, wxTreeItemIcon_Selected
);
3634 void wxGenericTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
3636 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
3639 #endif // WXWIN_COMPATIBILITY_2_4
3641 #if WXWIN_COMPATIBILITY_2_2
3643 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
3645 return GetItemParent( item
);
3648 #endif // WXWIN_COMPATIBILITY_2_2
3650 #endif // wxUSE_TREECTRL