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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treebase.h"
30 #include "wx/generic/treectlg.h"
32 #include "wx/textctrl.h"
33 #include "wx/imaglist.h"
34 #include "wx/settings.h"
35 #include "wx/dcclient.h"
37 #include "wx/renderer.h"
40 #include "wx/mac/private.h"
43 // -----------------------------------------------------------------------------
45 // -----------------------------------------------------------------------------
47 class WXDLLEXPORT wxGenericTreeItem
;
49 WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 static const int NO_IMAGE
= -1;
57 static const int PIXELS_PER_UNIT
= 10;
59 // -----------------------------------------------------------------------------
61 // -----------------------------------------------------------------------------
63 // timer used for enabling in-place edit
64 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
67 // start editing the current item after half a second (if the mouse hasn't
68 // been clicked/moved)
71 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
73 virtual void Notify();
76 wxGenericTreeCtrl
*m_owner
;
78 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
81 // control used for in-place edit
82 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
85 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
90 m_owner
->OnRenameCancelled(m_itemEdited
);
92 const wxGenericTreeItem
* item() const { return m_itemEdited
; }
95 void OnChar( wxKeyEvent
&event
);
96 void OnKeyUp( wxKeyEvent
&event
);
97 void OnKillFocus( wxFocusEvent
&event
);
103 wxGenericTreeCtrl
*m_owner
;
104 wxGenericTreeItem
*m_itemEdited
;
105 wxString m_startValue
;
107 bool m_aboutToFinish
;
109 DECLARE_EVENT_TABLE()
110 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
113 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
114 // for a sufficiently long time
115 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
118 // reset the current prefix after half a second of inactivity
119 enum { DELAY
= 500 };
121 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
123 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
126 wxGenericTreeCtrl
*m_owner
;
128 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
132 class WXDLLEXPORT wxGenericTreeItem
136 wxGenericTreeItem() { m_data
= NULL
; }
137 wxGenericTreeItem( wxGenericTreeItem
*parent
,
138 const wxString
& text
,
141 wxTreeItemData
*data
);
143 ~wxGenericTreeItem();
146 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
148 const wxString
& GetText() const { return m_text
; }
149 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
150 { return m_images
[which
]; }
151 wxTreeItemData
*GetData() const { return m_data
; }
153 // returns the current image for the item (depending on its
154 // selected/expanded/whatever state)
155 int GetCurrentImage() const;
157 void SetText( const wxString
&text
);
158 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
159 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
161 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
163 void SetBold(bool bold
) { m_isBold
= bold
; }
165 int GetX() const { return m_x
; }
166 int GetY() const { return m_y
; }
168 void SetX(int x
) { m_x
= x
; }
169 void SetY(int y
) { m_y
= y
; }
171 int GetHeight() const { return m_height
; }
172 int GetWidth() const { return m_width
; }
174 void SetHeight(int h
) { m_height
= h
; }
175 void SetWidth(int w
) { m_width
= w
; }
177 wxGenericTreeItem
*GetParent() const { return m_parent
; }
180 // deletes all children notifying the treectrl about it if !NULL
182 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
184 // get count of all children (and grand children if 'recursively')
185 size_t GetChildrenCount(bool recursively
= true) const;
187 void Insert(wxGenericTreeItem
*child
, size_t index
)
188 { m_children
.Insert(child
, index
); }
190 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
192 // return the item at given position (or NULL if no item), onButton is
193 // true if the point belongs to the item's button, otherwise it lies
194 // on the item's label
195 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
196 const wxGenericTreeCtrl
*,
200 void Expand() { m_isCollapsed
= false; }
201 void Collapse() { m_isCollapsed
= true; }
203 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
206 bool HasChildren() const { return !m_children
.IsEmpty(); }
207 bool IsSelected() const { return m_hasHilight
!= 0; }
208 bool IsExpanded() const { return !m_isCollapsed
; }
209 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
210 bool IsBold() const { return m_isBold
!= 0; }
213 // get them - may be NULL
214 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
215 // get them ensuring that the pointer is not NULL
216 wxTreeItemAttr
& Attr()
220 m_attr
= new wxTreeItemAttr
;
226 void SetAttributes(wxTreeItemAttr
*attr
)
228 if ( m_ownsAttr
) delete m_attr
;
232 // set them and delete when done
233 void AssignAttributes(wxTreeItemAttr
*attr
)
240 // since there can be very many of these, we save size by chosing
241 // the smallest representation for the elements and by ordering
242 // the members to avoid padding.
243 wxString m_text
; // label to be rendered for item
245 wxTreeItemData
*m_data
; // user-provided data
247 wxArrayGenericTreeItems m_children
; // list of children
248 wxGenericTreeItem
*m_parent
; // parent of this item
250 wxTreeItemAttr
*m_attr
; // attributes???
252 // tree ctrl images for the normal, selected, expanded and
253 // expanded+selected states
254 int m_images
[wxTreeItemIcon_Max
];
256 wxCoord m_x
; // (virtual) offset from top
257 wxCoord m_y
; // (virtual) offset from left
258 int m_width
; // width of this item
259 int m_height
; // height of this item
261 // use bitfields to save size
262 unsigned int m_isCollapsed
:1;
263 unsigned int m_hasHilight
:1; // same as focused
264 unsigned int m_hasPlus
:1; // used for item which doesn't have
265 // children but has a [+] button
266 unsigned int m_isBold
:1; // render the label in bold font
267 unsigned int m_ownsAttr
:1; // delete attribute when done
269 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
272 // =============================================================================
274 // =============================================================================
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
280 // translate the key or mouse event flags to the type of selection we're
282 static void EventFlagsToSelType(long style
,
286 bool &extended_select
,
287 bool &unselect_others
)
289 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
290 extended_select
= shiftDown
&& is_multiple
;
291 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
294 // check if the given item is under another one
295 static bool IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
299 if ( item
== parent
)
301 // item is a descendant of parent
305 item
= item
->GetParent();
311 // -----------------------------------------------------------------------------
312 // wxTreeRenameTimer (internal)
313 // -----------------------------------------------------------------------------
315 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
320 void wxTreeRenameTimer::Notify()
322 m_owner
->OnRenameTimer();
325 //-----------------------------------------------------------------------------
326 // wxTreeTextCtrl (internal)
327 //-----------------------------------------------------------------------------
329 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
330 EVT_CHAR (wxTreeTextCtrl::OnChar
)
331 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
332 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
335 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
336 wxGenericTreeItem
*item
)
337 : m_itemEdited(item
), m_startValue(item
->GetText())
341 m_aboutToFinish
= false;
343 int w
= m_itemEdited
->GetWidth(),
344 h
= m_itemEdited
->GetHeight();
347 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
352 int image
= item
->GetCurrentImage();
353 if ( image
!= NO_IMAGE
)
355 if ( m_owner
->m_imageListNormal
)
357 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
362 wxFAIL_MSG(_T("you must create an image list to use images!"));
366 // FIXME: what are all these hardcoded 4, 8 and 11s really?
370 wxSize bs
= DoGetBestSize() ;
371 // edit control height
374 int diff
= h
- ( bs
.y
- 8 ) ;
380 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
381 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
384 bool wxTreeTextCtrl::AcceptChanges()
386 const wxString value
= GetValue();
388 if ( value
== m_startValue
)
390 // nothing changed, always accept
391 // when an item remains unchanged, the owner
392 // needs to be notified that the user decided
393 // not to change the tree item label, and that
394 // the edit has been cancelled
396 m_owner
->OnRenameCancelled(m_itemEdited
);
400 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
402 // vetoed by the user
406 // accepted, do rename the item
407 m_owner
->SetItemText(m_itemEdited
, value
);
412 void wxTreeTextCtrl::Finish()
416 m_owner
->ResetTextControl();
418 wxPendingDelete
.Append(this);
422 m_owner
->SetFocusIgnoringChildren();
426 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
428 switch ( event
.m_keyCode
)
431 m_aboutToFinish
= true;
432 // Notify the owner about the changes
434 // Even if vetoed, close the control (consistent with MSW)
447 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
451 // auto-grow the textctrl:
452 wxSize parentSize
= m_owner
->GetSize();
453 wxPoint myPos
= GetPosition();
454 wxSize mySize
= GetSize();
456 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
457 if (myPos
.x
+ sx
> parentSize
.x
)
458 sx
= parentSize
.x
- myPos
.x
;
461 SetSize(sx
, wxDefaultCoord
);
467 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
469 if ( !m_finished
&& !m_aboutToFinish
)
471 // We must finish regardless of success, otherwise we'll get
475 if ( !AcceptChanges() )
476 m_owner
->OnRenameCancelled( m_itemEdited
);
479 // We must let the native text control handle focus, too, otherwise
480 // it could have problems with the cursor (e.g., in wxGTK).
484 // -----------------------------------------------------------------------------
486 // -----------------------------------------------------------------------------
488 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
489 const wxString
& text
,
490 int image
, int selImage
,
491 wxTreeItemData
*data
)
494 m_images
[wxTreeItemIcon_Normal
] = image
;
495 m_images
[wxTreeItemIcon_Selected
] = selImage
;
496 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
497 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
502 m_isCollapsed
= true;
503 m_hasHilight
= false;
509 m_attr
= (wxTreeItemAttr
*)NULL
;
512 // We don't know the height here yet.
517 wxGenericTreeItem::~wxGenericTreeItem()
521 if (m_ownsAttr
) delete m_attr
;
523 wxASSERT_MSG( m_children
.IsEmpty(),
524 wxT("please call DeleteChildren() before deleting the item") );
527 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
529 size_t count
= m_children
.Count();
530 for ( size_t n
= 0; n
< count
; n
++ )
532 wxGenericTreeItem
*child
= m_children
[n
];
534 tree
->SendDeleteEvent(child
);
536 child
->DeleteChildren(tree
);
537 if (child
== tree
->m_select_me
)
538 tree
->m_select_me
= NULL
;
545 void wxGenericTreeItem::SetText( const wxString
&text
)
550 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
552 size_t count
= m_children
.Count();
556 size_t total
= count
;
557 for (size_t n
= 0; n
< count
; ++n
)
559 total
+= m_children
[n
]->GetChildrenCount();
565 void wxGenericTreeItem::GetSize( int &x
, int &y
,
566 const wxGenericTreeCtrl
*theButton
)
568 int bottomY
=m_y
+theButton
->GetLineHeight(this);
569 if ( y
< bottomY
) y
= bottomY
;
570 int width
= m_x
+ m_width
;
571 if ( x
< width
) x
= width
;
575 size_t count
= m_children
.Count();
576 for ( size_t n
= 0; n
< count
; ++n
)
578 m_children
[n
]->GetSize( x
, y
, theButton
);
583 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
584 const wxGenericTreeCtrl
*theCtrl
,
588 // for a hidden root node, don't evaluate it, but do evaluate children
589 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
592 int h
= theCtrl
->GetLineHeight(this);
593 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
595 int y_mid
= m_y
+ h
/2;
596 if (point
.y
< y_mid
)
597 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
599 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
601 int xCross
= m_x
- theCtrl
->GetSpacing();
603 // according to the drawing code the triangels are drawn
604 // at -4 , -4 from the position up to +10/+10 max
605 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
606 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
607 HasPlus() && theCtrl
->HasButtons() )
609 // 5 is the size of the plus sign
610 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
611 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
612 HasPlus() && theCtrl
->HasButtons() )
615 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
619 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
624 // assuming every image (normal and selected) has the same size!
625 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
626 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
629 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
630 flags
|= wxTREE_HITTEST_ONITEMICON
;
632 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
638 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
639 if (point
.x
> m_x
+m_width
)
640 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
645 // if children are expanded, fall through to evaluate them
646 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
650 size_t count
= m_children
.Count();
651 for ( size_t n
= 0; n
< count
; n
++ )
653 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
661 return (wxGenericTreeItem
*) NULL
;
664 int wxGenericTreeItem::GetCurrentImage() const
666 int image
= NO_IMAGE
;
671 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
674 if ( image
== NO_IMAGE
)
676 // we usually fall back to the normal item, but try just the
677 // expanded one (and not selected) first in this case
678 image
= GetImage(wxTreeItemIcon_Expanded
);
684 image
= GetImage(wxTreeItemIcon_Selected
);
687 // maybe it doesn't have the specific image we want,
688 // try the default one instead
689 if ( image
== NO_IMAGE
) image
= GetImage();
694 // -----------------------------------------------------------------------------
695 // wxGenericTreeCtrl implementation
696 // -----------------------------------------------------------------------------
698 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
700 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
701 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
702 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
703 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
704 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
705 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
706 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
709 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
711 * wxTreeCtrl has to be a real class or we have problems with
712 * the run-time information.
715 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
718 // -----------------------------------------------------------------------------
719 // construction/destruction
720 // -----------------------------------------------------------------------------
722 void wxGenericTreeCtrl::Init()
724 m_current
= m_key_current
= m_anchor
= m_select_me
= (wxGenericTreeItem
*) NULL
;
732 m_hilightBrush
= new wxBrush
734 wxSystemSettings::GetColour
736 wxSYS_COLOUR_HIGHLIGHT
741 m_hilightUnfocusedBrush
= new wxBrush
743 wxSystemSettings::GetColour
745 wxSYS_COLOUR_BTNSHADOW
750 m_imageListNormal
= m_imageListButtons
=
751 m_imageListState
= (wxImageList
*) NULL
;
752 m_ownsImageListNormal
= m_ownsImageListButtons
=
753 m_ownsImageListState
= false;
756 m_isDragging
= false;
757 m_dropTarget
= m_oldSelection
= NULL
;
761 m_renameTimer
= NULL
;
766 m_dropEffectAboveItem
= false;
768 m_lastOnSame
= false;
770 #ifdef __WXMAC_CARBON__
771 m_normalFont
.MacCreateThemeFont( kThemeViewsFont
) ;
773 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
775 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
776 m_normalFont
.GetFamily(),
777 m_normalFont
.GetStyle(),
779 m_normalFont
.GetUnderlined(),
780 m_normalFont
.GetFaceName(),
781 m_normalFont
.GetEncoding());
784 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
789 const wxValidator
& wxVALIDATOR_PARAM(validator
),
790 const wxString
& name
)
794 wxGetOsVersion( &major
, &minor
);
796 style
&= ~wxTR_LINES_AT_ROOT
;
797 style
|= wxTR_NO_LINES
;
799 style
|= wxTR_ROW_LINES
;
802 wxScrolledWindow::Create( parent
, id
, pos
, size
,
803 style
|wxHSCROLL
|wxVSCROLL
, name
);
805 // If the tree display has no buttons, but does have
806 // connecting lines, we can use a narrower layout.
807 // It may not be a good idea to force this...
808 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
815 SetValidator( validator
);
818 wxVisualAttributes attr
= GetDefaultAttributes();
819 SetOwnForegroundColour( attr
.colFg
);
820 SetOwnBackgroundColour( attr
.colBg
);
822 SetOwnFont(attr
.font
);
824 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
825 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
832 wxGenericTreeCtrl::~wxGenericTreeCtrl()
834 delete m_hilightBrush
;
835 delete m_hilightUnfocusedBrush
;
839 delete m_renameTimer
;
842 if (m_ownsImageListNormal
)
843 delete m_imageListNormal
;
844 if (m_ownsImageListState
)
845 delete m_imageListState
;
846 if (m_ownsImageListButtons
)
847 delete m_imageListButtons
;
850 // -----------------------------------------------------------------------------
852 // -----------------------------------------------------------------------------
854 size_t wxGenericTreeCtrl::GetCount() const
862 size_t count
= m_anchor
->GetChildrenCount();
863 if ( !HasFlag(wxTR_HIDE_ROOT
) )
865 // take the root itself into account
872 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
874 m_indent
= (unsigned short) indent
;
878 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
880 m_spacing
= (unsigned short) spacing
;
885 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
886 bool recursively
) const
888 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
890 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
893 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
895 // Do not try to expand the root node if it hasn't been created yet
896 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
898 // if we will hide the root, make sure children are visible
899 m_anchor
->SetHasPlus();
901 CalculatePositions();
904 // right now, just sets the styles. Eventually, we may
905 // want to update the inherited styles, but right now
906 // none of the parents has updatable styles
907 m_windowStyle
= styles
;
911 // -----------------------------------------------------------------------------
912 // functions to work with tree items
913 // -----------------------------------------------------------------------------
915 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
917 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
919 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
922 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
923 wxTreeItemIcon which
) const
925 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
927 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
930 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
932 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
934 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
937 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
939 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
941 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
942 return pItem
->Attr().GetTextColour();
945 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
947 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
949 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
950 return pItem
->Attr().GetBackgroundColour();
953 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
955 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
957 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
958 return pItem
->Attr().GetFont();
961 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
963 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
966 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
967 pItem
->SetText(text
);
968 CalculateSize(pItem
, dc
);
972 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
974 wxTreeItemIcon which
)
976 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
978 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
979 pItem
->SetImage(image
, which
);
982 CalculateSize(pItem
, dc
);
986 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
988 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
993 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
996 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
998 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1000 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1001 pItem
->SetHasPlus(has
);
1005 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1007 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1009 // avoid redrawing the tree if no real change
1010 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1011 if ( pItem
->IsBold() != bold
)
1013 pItem
->SetBold(bold
);
1018 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1021 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1027 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1028 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1031 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1032 pItem
->Attr().SetTextColour(fg
);
1033 pItem
->Attr().SetBackgroundColour(bg
);
1037 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1038 const wxColour
& col
)
1040 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1042 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1043 pItem
->Attr().SetTextColour(col
);
1047 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1048 const wxColour
& col
)
1050 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1052 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1053 pItem
->Attr().SetBackgroundColour(col
);
1057 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1059 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1061 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1062 pItem
->Attr().SetFont(font
);
1066 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1068 wxScrolledWindow::SetFont(font
);
1070 m_normalFont
= font
;
1071 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1072 m_normalFont
.GetFamily(),
1073 m_normalFont
.GetStyle(),
1075 m_normalFont
.GetUnderlined(),
1076 m_normalFont
.GetFaceName(),
1077 m_normalFont
.GetEncoding());
1083 // -----------------------------------------------------------------------------
1084 // item status inquiries
1085 // -----------------------------------------------------------------------------
1087 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1089 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1091 // An item is only visible if it's not a descendant of a collapsed item
1092 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1093 wxGenericTreeItem
* parent
= pItem
->GetParent();
1096 if (!parent
->IsExpanded())
1098 parent
= parent
->GetParent();
1102 GetViewStart(& startX
, & startY
);
1104 wxSize clientSize
= GetClientSize();
1107 if (!GetBoundingRect(item
, rect
))
1109 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1111 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1113 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1119 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1121 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1123 // consider that the item does have children if it has the "+" button: it
1124 // might not have them (if it had never been expanded yet) but then it
1125 // could have them as well and it's better to err on this side rather than
1126 // disabling some operations which are restricted to the items with
1127 // children for an item which does have them
1128 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1131 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1133 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1135 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1138 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1140 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1142 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1145 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1147 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1149 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1152 // -----------------------------------------------------------------------------
1154 // -----------------------------------------------------------------------------
1156 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1158 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1160 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1163 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1164 wxTreeItemIdValue
& cookie
) const
1166 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1169 return GetNextChild(item
, cookie
);
1172 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1173 wxTreeItemIdValue
& cookie
) const
1175 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1177 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1179 // it's ok to cast cookie to size_t, we never have indices big enough to
1180 // overflow "void *"
1181 size_t *pIndex
= (size_t *)&cookie
;
1182 if ( *pIndex
< children
.Count() )
1184 return children
.Item((*pIndex
)++);
1188 // there are no more of them
1189 return wxTreeItemId();
1193 #if WXWIN_COMPATIBILITY_2_4
1195 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1198 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1201 return GetNextChild(item
, cookie
);
1204 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1207 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1209 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1210 if ( (size_t)cookie
< children
.Count() )
1212 return children
.Item((size_t)cookie
++);
1216 // there are no more of them
1217 return wxTreeItemId();
1221 #endif // WXWIN_COMPATIBILITY_2_4
1223 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1225 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1227 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1228 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1231 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1233 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1235 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1236 wxGenericTreeItem
*parent
= i
->GetParent();
1237 if ( parent
== NULL
)
1239 // root item doesn't have any siblings
1240 return wxTreeItemId();
1243 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1244 int index
= siblings
.Index(i
);
1245 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1247 size_t n
= (size_t)(index
+ 1);
1248 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1251 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1253 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1255 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1256 wxGenericTreeItem
*parent
= i
->GetParent();
1257 if ( parent
== NULL
)
1259 // root item doesn't have any siblings
1260 return wxTreeItemId();
1263 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1264 int index
= siblings
.Index(i
);
1265 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1267 return index
== 0 ? wxTreeItemId()
1268 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1271 // Only for internal use right now, but should probably be public
1272 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1274 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1276 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1278 // First see if there are any children.
1279 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1280 if (children
.GetCount() > 0)
1282 return children
.Item(0);
1286 // Try a sibling of this or ancestor instead
1287 wxTreeItemId p
= item
;
1288 wxTreeItemId toFind
;
1291 toFind
= GetNextSibling(p
);
1292 p
= GetItemParent(p
);
1293 } while (p
.IsOk() && !toFind
.IsOk());
1298 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1300 wxTreeItemId id
= GetRootItem();
1309 } while (id
.IsOk());
1311 return wxTreeItemId();
1314 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1316 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1318 wxTreeItemId id
= item
;
1321 while (id
= GetNext(id
), id
.IsOk())
1327 return wxTreeItemId();
1330 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1332 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1334 wxFAIL_MSG(wxT("not implemented"));
1336 return wxTreeItemId();
1339 // called by wxTextTreeCtrl when it marks itself for deletion
1340 void wxGenericTreeCtrl::ResetTextControl()
1345 // find the first item starting with the given prefix after the given item
1346 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1347 const wxString
& prefixOrig
) const
1349 // match is case insensitive as this is more convenient to the user: having
1350 // to press Shift-letter to go to the item starting with a capital letter
1351 // would be too bothersome
1352 wxString prefix
= prefixOrig
.Lower();
1354 // determine the starting point: we shouldn't take the current item (this
1355 // allows to switch between two items starting with the same letter just by
1356 // pressing it) but we shouldn't jump to the next one if the user is
1357 // continuing to type as otherwise he might easily skip the item he wanted
1358 wxTreeItemId id
= idParent
;
1359 if ( prefix
.length() == 1 )
1364 // look for the item starting with the given prefix after it
1365 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1370 // if we haven't found anything...
1373 // ... wrap to the beginning
1375 if ( HasFlag(wxTR_HIDE_ROOT
) )
1377 // can't select virtual root
1381 // and try all the items (stop when we get to the one we started from)
1382 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1391 // -----------------------------------------------------------------------------
1393 // -----------------------------------------------------------------------------
1395 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1397 const wxString
& text
,
1398 int image
, int selImage
,
1399 wxTreeItemData
*data
)
1401 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1404 // should we give a warning here?
1405 return AddRoot(text
, image
, selImage
, data
);
1408 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1410 wxGenericTreeItem
*item
=
1411 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1415 data
->m_pItem
= item
;
1418 parent
->Insert( item
, previous
);
1423 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1424 int image
, int selImage
,
1425 wxTreeItemData
*data
)
1427 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1429 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1431 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1432 image
, selImage
, data
);
1435 data
->m_pItem
= m_anchor
;
1438 if (HasFlag(wxTR_HIDE_ROOT
))
1440 // if root is hidden, make sure we can navigate
1442 m_anchor
->SetHasPlus();
1444 CalculatePositions();
1447 if (!HasFlag(wxTR_MULTIPLE
))
1449 m_current
= m_key_current
= m_anchor
;
1450 m_current
->SetHilight( true );
1456 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1457 const wxString
& text
,
1458 int image
, int selImage
,
1459 wxTreeItemData
*data
)
1461 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1464 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1465 const wxTreeItemId
& idPrevious
,
1466 const wxString
& text
,
1467 int image
, int selImage
,
1468 wxTreeItemData
*data
)
1470 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1473 // should we give a warning here?
1474 return AddRoot(text
, image
, selImage
, data
);
1478 if (idPrevious
.IsOk())
1480 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1481 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1482 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1485 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1488 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1490 const wxString
& text
,
1491 int image
, int selImage
,
1492 wxTreeItemData
*data
)
1494 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1497 // should we give a warning here?
1498 return AddRoot(text
, image
, selImage
, data
);
1501 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1504 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1505 const wxString
& text
,
1506 int image
, int selImage
,
1507 wxTreeItemData
*data
)
1509 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1512 // should we give a warning here?
1513 return AddRoot(text
, image
, selImage
, data
);
1516 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1517 image
, selImage
, data
);
1520 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1522 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1523 event
.m_item
= item
;
1524 event
.SetEventObject( this );
1525 ProcessEvent( event
);
1528 // Don't leave edit or selection on a child which is about to disappear
1529 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1531 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1532 m_textCtrl
->StopEditing();
1534 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1535 m_key_current
= NULL
;
1537 if (IsDescendantOf(item
, m_select_me
)) {
1540 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1541 m_current
->SetHilight( false );
1547 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1549 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1551 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1552 ChildrenClosing(item
);
1553 item
->DeleteChildren(this);
1556 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1558 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1560 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1562 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1564 // can't delete the item being edited, cancel editing it first
1565 m_textCtrl
->StopEditing();
1568 wxGenericTreeItem
*parent
= item
->GetParent();
1570 // don't keep stale pointers around!
1571 if ( IsDescendantOf(item
, m_key_current
) )
1573 // Don't silently change the selection:
1574 // do it properly in idle time, so event
1575 // handlers get called.
1577 // m_key_current = parent;
1578 m_key_current
= NULL
;
1581 // m_select_me records whether we need to select
1582 // a different item, in idle time.
1583 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1585 m_select_me
= parent
;
1588 if ( IsDescendantOf(item
, m_current
) )
1590 // Don't silently change the selection:
1591 // do it properly in idle time, so event
1592 // handlers get called.
1594 // m_current = parent;
1596 m_select_me
= parent
;
1599 // remove the item from the tree
1602 parent
->GetChildren().Remove( item
); // remove by value
1604 else // deleting the root
1606 // nothing will be left in the tree
1610 // and delete all of its children and the item itself now
1611 item
->DeleteChildren(this);
1612 SendDeleteEvent(item
);
1614 if (item
== m_select_me
)
1620 void wxGenericTreeCtrl::DeleteAllItems()
1628 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1630 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1632 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1633 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1634 _T("can't expand hidden root") );
1636 if ( !item
->HasPlus() )
1639 if ( item
->IsExpanded() )
1642 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1643 event
.m_item
= item
;
1644 event
.SetEventObject( this );
1646 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1648 // cancelled by program
1653 CalculatePositions();
1655 RefreshSubtree(item
);
1657 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1658 ProcessEvent( event
);
1661 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1663 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1666 if ( !IsExpanded(item
) )
1670 wxTreeItemIdValue cookie
;
1671 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1672 while ( child
.IsOk() )
1676 child
= GetNextChild(item
, cookie
);
1680 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1682 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1683 _T("can't collapse hidden root") );
1685 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1687 if ( !item
->IsExpanded() )
1690 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1691 event
.m_item
= item
;
1692 event
.SetEventObject( this );
1693 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1695 // cancelled by program
1699 ChildrenClosing(item
);
1702 #if 0 // TODO why should items be collapsed recursively?
1703 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1704 size_t count
= children
.Count();
1705 for ( size_t n
= 0; n
< count
; n
++ )
1707 Collapse(children
[n
]);
1711 CalculatePositions();
1713 RefreshSubtree(item
);
1715 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1716 ProcessEvent( event
);
1719 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1722 DeleteChildren(item
);
1725 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1727 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1729 if (item
->IsExpanded())
1735 void wxGenericTreeCtrl::Unselect()
1739 m_current
->SetHilight( false );
1740 RefreshLine( m_current
);
1747 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1749 if (item
->IsSelected())
1751 item
->SetHilight(false);
1755 if (item
->HasChildren())
1757 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1758 size_t count
= children
.Count();
1759 for ( size_t n
= 0; n
< count
; ++n
)
1761 UnselectAllChildren(children
[n
]);
1766 void wxGenericTreeCtrl::UnselectAll()
1768 wxTreeItemId rootItem
= GetRootItem();
1770 // the tree might not have the root item at all
1773 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1777 // Recursive function !
1778 // To stop we must have crt_item<last_item
1780 // Tag all next children, when no more children,
1781 // Move to parent (not to tag)
1782 // Keep going... if we found last_item, we stop.
1783 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1785 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1787 if (parent
== NULL
) // This is root item
1788 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1790 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1791 int index
= children
.Index(crt_item
);
1792 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1794 size_t count
= children
.Count();
1795 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1797 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1800 return TagNextChildren(parent
, last_item
, select
);
1803 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1805 crt_item
->SetHilight(select
);
1806 RefreshLine(crt_item
);
1808 if (crt_item
==last_item
)
1811 if (crt_item
->HasChildren())
1813 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1814 size_t count
= children
.Count();
1815 for ( size_t n
= 0; n
< count
; ++n
)
1817 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1825 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1829 // item2 is not necessary after item1
1830 // choice first' and 'last' between item1 and item2
1831 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1832 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1834 bool select
= m_current
->IsSelected();
1836 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1839 TagNextChildren(first
,last
,select
);
1842 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1843 bool unselect_others
,
1844 bool extended_select
)
1846 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1850 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1851 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1853 //wxCHECK_RET( ( (!unselect_others) && is_single),
1854 // wxT("this is a single selection tree") );
1856 // to keep going anyhow !!!
1859 if (item
->IsSelected())
1860 return; // nothing to do
1861 unselect_others
= true;
1862 extended_select
= false;
1864 else if ( unselect_others
&& item
->IsSelected() )
1866 // selection change if there is more than one item currently selected
1867 wxArrayTreeItemIds selected_items
;
1868 if ( GetSelections(selected_items
) == 1 )
1872 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1873 event
.m_item
= item
;
1874 event
.m_itemOld
= m_current
;
1875 event
.SetEventObject( this );
1876 // TODO : Here we don't send any selection mode yet !
1878 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1881 wxTreeItemId parent
= GetItemParent( itemId
);
1882 while (parent
.IsOk())
1884 if (!IsExpanded(parent
))
1887 parent
= GetItemParent( parent
);
1890 EnsureVisible( itemId
);
1893 if (unselect_others
)
1895 if (is_single
) Unselect(); // to speed up thing
1900 if (extended_select
)
1904 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1907 // don't change the mark (m_current)
1908 SelectItemRange(m_current
, item
);
1912 bool select
= true; // the default
1914 // Check if we need to toggle hilight (ctrl mode)
1915 if (!unselect_others
)
1916 select
=!item
->IsSelected();
1918 m_current
= m_key_current
= item
;
1919 m_current
->SetHilight(select
);
1920 RefreshLine( m_current
);
1923 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1924 GetEventHandler()->ProcessEvent( event
);
1927 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1931 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
1935 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1936 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1938 item
->SetHilight(false);
1943 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1944 wxArrayTreeItemIds
&array
) const
1946 if ( item
->IsSelected() )
1947 array
.Add(wxTreeItemId(item
));
1949 if ( item
->HasChildren() )
1951 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1952 size_t count
= children
.GetCount();
1953 for ( size_t n
= 0; n
< count
; ++n
)
1954 FillArray(children
[n
], array
);
1958 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1961 wxTreeItemId idRoot
= GetRootItem();
1962 if ( idRoot
.IsOk() )
1964 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1966 //else: the tree is empty, so no selections
1968 return array
.Count();
1971 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1973 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1975 if (!item
.IsOk()) return;
1977 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1979 // first expand all parent branches
1980 wxGenericTreeItem
*parent
= gitem
->GetParent();
1982 if ( HasFlag(wxTR_HIDE_ROOT
) )
1984 while ( parent
&& parent
!= m_anchor
)
1987 parent
= parent
->GetParent();
1995 parent
= parent
->GetParent();
1999 //if (parent) CalculatePositions();
2004 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
2006 if (!item
.IsOk()) return;
2008 // We have to call this here because the label in
2009 // question might just have been added and no screen
2010 // update taken place.
2012 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2017 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2019 // now scroll to the item
2020 int item_y
= gitem
->GetY();
2024 GetViewStart( &start_x
, &start_y
);
2025 start_y
*= PIXELS_PER_UNIT
;
2029 GetClientSize( &client_w
, &client_h
);
2031 if (item_y
< start_y
+3)
2036 m_anchor
->GetSize( x
, y
, this );
2037 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2038 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2039 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2040 // Item should appear at top
2041 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2043 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2048 m_anchor
->GetSize( x
, y
, this );
2049 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2050 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2051 item_y
+= PIXELS_PER_UNIT
+2;
2052 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2053 // Item should appear at bottom
2054 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
);
2058 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2059 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2061 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2062 wxGenericTreeItem
**item2
)
2064 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2066 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2069 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
2070 const wxTreeItemId
& item2
)
2072 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2075 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2077 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2079 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2081 wxCHECK_RET( !s_treeBeingSorted
,
2082 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2084 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2085 if ( children
.Count() > 1 )
2089 s_treeBeingSorted
= this;
2090 children
.Sort(tree_ctrl_compare_func
);
2091 s_treeBeingSorted
= NULL
;
2093 //else: don't make the tree dirty as nothing changed
2096 wxImageList
*wxGenericTreeCtrl::GetImageList() const
2098 return m_imageListNormal
;
2101 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
2103 return m_imageListButtons
;
2106 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
2108 return m_imageListState
;
2111 void wxGenericTreeCtrl::CalculateLineHeight()
2113 wxClientDC
dc(this);
2114 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2116 if ( m_imageListNormal
)
2118 // Calculate a m_lineHeight value from the normal Image sizes.
2119 // May be toggle off. Then wxGenericTreeCtrl will spread when
2120 // necessary (which might look ugly).
2121 int n
= m_imageListNormal
->GetImageCount();
2122 for (int i
= 0; i
< n
; i
++)
2124 int width
= 0, height
= 0;
2125 m_imageListNormal
->GetSize(i
, width
, height
);
2126 if (height
> m_lineHeight
) m_lineHeight
= height
;
2130 if (m_imageListButtons
)
2132 // Calculate a m_lineHeight value from the Button image sizes.
2133 // May be toggle off. Then wxGenericTreeCtrl will spread when
2134 // necessary (which might look ugly).
2135 int n
= m_imageListButtons
->GetImageCount();
2136 for (int i
= 0; i
< n
; i
++)
2138 int width
= 0, height
= 0;
2139 m_imageListButtons
->GetSize(i
, width
, height
);
2140 if (height
> m_lineHeight
) m_lineHeight
= height
;
2144 if (m_lineHeight
< 30)
2145 m_lineHeight
+= 2; // at least 2 pixels
2147 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2150 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2152 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2153 m_imageListNormal
= imageList
;
2154 m_ownsImageListNormal
= false;
2156 // Don't do any drawing if we're setting the list to NULL,
2157 // since we may be in the process of deleting the tree control.
2159 CalculateLineHeight();
2162 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2164 if (m_ownsImageListState
) delete m_imageListState
;
2165 m_imageListState
= imageList
;
2166 m_ownsImageListState
= false;
2169 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2171 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2172 m_imageListButtons
= imageList
;
2173 m_ownsImageListButtons
= false;
2175 CalculateLineHeight();
2178 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2180 SetImageList(imageList
);
2181 m_ownsImageListNormal
= true;
2184 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2186 SetStateImageList(imageList
);
2187 m_ownsImageListState
= true;
2190 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2192 SetButtonsImageList(imageList
);
2193 m_ownsImageListButtons
= true;
2196 // -----------------------------------------------------------------------------
2198 // -----------------------------------------------------------------------------
2200 void wxGenericTreeCtrl::AdjustMyScrollbars()
2205 m_anchor
->GetSize( x
, y
, this );
2206 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2207 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2208 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2209 int y_pos
= GetScrollPos( wxVERTICAL
);
2210 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2214 SetScrollbars( 0, 0, 0, 0 );
2218 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2220 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2221 return item
->GetHeight();
2223 return m_lineHeight
;
2226 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2228 // TODO implement "state" icon on items
2230 wxTreeItemAttr
*attr
= item
->GetAttributes();
2231 if ( attr
&& attr
->HasFont() )
2232 dc
.SetFont(attr
->GetFont());
2233 else if (item
->IsBold())
2234 dc
.SetFont(m_boldFont
);
2236 long text_w
= 0, text_h
= 0;
2237 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2239 int image_h
= 0, image_w
= 0;
2240 int image
= item
->GetCurrentImage();
2241 if ( image
!= NO_IMAGE
)
2243 if ( m_imageListNormal
)
2245 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2254 int total_h
= GetLineHeight(item
);
2255 bool drawItemBackground
= false;
2257 if ( item
->IsSelected() )
2259 // under mac selections are only a rectangle in case they don't have the focus
2263 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2264 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2268 dc
.SetBrush( *m_hilightBrush
) ;
2271 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2273 drawItemBackground
= true;
2278 if ( attr
&& attr
->HasBackgroundColour() )
2280 drawItemBackground
= true;
2281 colBg
= attr
->GetBackgroundColour();
2285 colBg
= m_backgroundColour
;
2287 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2290 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2292 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2296 DoGetPosition(&x
, &y
);
2298 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2302 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2304 // If it's selected, and there's an image, then we should
2305 // take care to leave the area under the image painted in the
2306 // background colour.
2307 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2308 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2310 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2311 // don't allow backgrounds to be customized. Not drawing the background,
2312 // except for custom item backgrounds, works for both kinds of theme.
2313 else if (drawItemBackground
)
2315 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2316 item
->GetWidth()+2, total_h
-offset
);
2320 if ( image
!= NO_IMAGE
)
2322 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2323 m_imageListNormal
->Draw( image
, dc
,
2325 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2326 wxIMAGELIST_DRAW_TRANSPARENT
);
2327 dc
.DestroyClippingRegion();
2330 dc
.SetBackgroundMode(wxTRANSPARENT
);
2331 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2332 dc
.DrawText( item
->GetText(),
2333 (wxCoord
)(image_w
+ item
->GetX()),
2334 (wxCoord
)(item
->GetY() + extraH
));
2336 // restore normal font
2337 dc
.SetFont( m_normalFont
);
2340 // Now y stands for the top of the item, whereas it used to stand for middle !
2341 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2343 int x
= level
*m_indent
;
2344 if (!HasFlag(wxTR_HIDE_ROOT
))
2348 else if (level
== 0)
2350 // always expand hidden root
2352 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2353 int count
= children
.Count();
2359 PaintLevel(children
[n
], dc
, 1, y
);
2360 } while (++n
< count
);
2362 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2364 // draw line down to last child
2365 origY
+= GetLineHeight(children
[0])>>1;
2366 oldY
+= GetLineHeight(children
[n
-1])>>1;
2367 dc
.DrawLine(3, origY
, 3, oldY
);
2373 item
->SetX(x
+m_spacing
);
2376 int h
= GetLineHeight(item
);
2378 int y_mid
= y_top
+ (h
>>1);
2381 int exposed_x
= dc
.LogicalToDeviceX(0);
2382 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2384 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2388 // don't draw rect outline if we already have the
2389 // background color under Mac
2390 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2391 #endif // !__WXMAC__
2395 if ( item
->IsSelected() )
2397 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2401 wxTreeItemAttr
*attr
= item
->GetAttributes();
2402 if (attr
&& attr
->HasTextColour())
2403 colText
= attr
->GetTextColour();
2405 colText
= GetForegroundColour();
2409 dc
.SetTextForeground(colText
);
2413 PaintItem(item
, dc
);
2415 if (HasFlag(wxTR_ROW_LINES
))
2417 // if the background colour is white, choose a
2418 // contrasting color for the lines
2419 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2420 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2421 dc
.DrawLine(0, y_top
, 10000, y_top
);
2422 dc
.DrawLine(0, y
, 10000, y
);
2425 // restore DC objects
2426 dc
.SetBrush(*wxWHITE_BRUSH
);
2427 dc
.SetPen(m_dottedPen
);
2428 dc
.SetTextForeground(*wxBLACK
);
2430 if ( !HasFlag(wxTR_NO_LINES
) )
2432 // draw the horizontal line here
2434 if (x
> (signed)m_indent
)
2435 x_start
-= m_indent
;
2436 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2438 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2441 // should the item show a button?
2442 if ( item
->HasPlus() && HasButtons() )
2444 if ( m_imageListButtons
)
2446 // draw the image button here
2449 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2450 : wxTreeItemIcon_Normal
;
2451 if ( item
->IsSelected() )
2452 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2454 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2455 int xx
= x
- image_w
/2;
2456 int yy
= y_mid
- image_h
/2;
2458 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2459 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2460 wxIMAGELIST_DRAW_TRANSPARENT
);
2462 else // no custom buttons
2464 static const int wImage
= 9;
2465 static const int hImage
= 9;
2468 if (item
->IsExpanded())
2469 flag
|= wxCONTROL_EXPANDED
;
2470 if (item
== m_underMouse
)
2471 flag
|= wxCONTROL_CURRENT
;
2473 wxRendererNative::Get().DrawTreeItemButton
2477 wxRect(x
- wImage
/2,
2486 if (item
->IsExpanded())
2488 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2489 int count
= children
.Count();
2496 PaintLevel(children
[n
], dc
, level
, y
);
2497 } while (++n
< count
);
2499 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2501 // draw line down to last child
2502 oldY
+= GetLineHeight(children
[n
-1])>>1;
2503 if (HasButtons()) y_mid
+= 5;
2505 // Only draw the portion of the line that is visible, in case it is huge
2506 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2507 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2508 yOrigin
= abs(yOrigin
);
2509 GetClientSize(&width
, &height
);
2511 // Move end points to the begining/end of the view?
2512 if (y_mid
< yOrigin
)
2514 if (oldY
> yOrigin
+ height
)
2515 oldY
= yOrigin
+ height
;
2517 // after the adjustments if y_mid is larger than oldY then the line
2518 // isn't visible at all so don't draw anything
2520 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2526 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2530 if ( item
->HasPlus() )
2532 // it's a folder, indicate it by a border
2537 // draw a line under the drop target because the item will be
2539 DrawLine(item
, !m_dropEffectAboveItem
);
2542 SetCursor(wxCURSOR_BULLSEYE
);
2547 SetCursor(wxCURSOR_NO_ENTRY
);
2551 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2553 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2555 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2557 wxClientDC
dc(this);
2559 dc
.SetLogicalFunction(wxINVERT
);
2560 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2562 int w
= i
->GetWidth() + 2;
2563 int h
= GetLineHeight(i
) + 2;
2565 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2568 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2570 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2572 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2574 wxClientDC
dc(this);
2576 dc
.SetLogicalFunction(wxINVERT
);
2582 y
+= GetLineHeight(i
) - 1;
2585 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2588 // -----------------------------------------------------------------------------
2589 // wxWidgets callbacks
2590 // -----------------------------------------------------------------------------
2592 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2600 dc
.SetFont( m_normalFont
);
2601 dc
.SetPen( m_dottedPen
);
2603 // this is now done dynamically
2604 //if(GetImageList() == NULL)
2605 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2608 PaintLevel( m_anchor
, dc
, 0, y
);
2611 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2620 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2629 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2631 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2632 te
.m_evtKey
= event
;
2633 te
.SetEventObject( this );
2634 if ( GetEventHandler()->ProcessEvent( te
) )
2636 // intercepted by the user code
2640 if ( (m_current
== 0) || (m_key_current
== 0) )
2646 // how should the selection work for this event?
2647 bool is_multiple
, extended_select
, unselect_others
;
2648 EventFlagsToSelType(GetWindowStyleFlag(),
2651 is_multiple
, extended_select
, unselect_others
);
2655 // * : Expand all/Collapse all
2656 // ' ' | return : activate
2657 // up : go up (not last children!)
2659 // left : go to parent
2660 // right : open if parent and go next
2661 // home : go to root
2662 // end : go to last item without opening parents
2663 // alnum : start or continue searching for the item with this prefix
2664 int keyCode
= event
.GetKeyCode();
2669 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2677 if ( !IsExpanded(m_current
) )
2680 ExpandAll(m_current
);
2683 //else: fall through to Collapse() it
2687 if (IsExpanded(m_current
))
2689 Collapse(m_current
);
2695 // Use the item's bounding rectangle to determine position for the event
2697 GetBoundingRect(m_current
, ItemRect
, true);
2699 wxTreeEvent
eventMenu( wxEVT_COMMAND_TREE_ITEM_MENU
, GetId() );
2700 eventMenu
.m_item
= m_current
;
2701 // Use the left edge, vertical middle
2702 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
2703 ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2704 eventMenu
.SetEventObject( this );
2705 GetEventHandler()->ProcessEvent( eventMenu
);
2710 if ( !event
.HasModifiers() )
2712 wxTreeEvent
eventAct( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2713 eventAct
.m_item
= m_current
;
2714 eventAct
.SetEventObject( this );
2715 GetEventHandler()->ProcessEvent( eventAct
);
2718 // in any case, also generate the normal key event for this key,
2719 // even if we generated the ACTIVATED event above: this is what
2720 // wxMSW does and it makes sense because you might not want to
2721 // process ACTIVATED event at all and handle Space and Return
2722 // directly (and differently) which would be impossible otherwise
2726 // up goes to the previous sibling or to the last
2727 // of its children if it's expanded
2730 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2733 prev
= GetItemParent( m_key_current
);
2734 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2736 break; // don't go to root if it is hidden
2740 wxTreeItemIdValue cookie
;
2741 wxTreeItemId current
= m_key_current
;
2742 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2743 if (current
== GetFirstChild( prev
, cookie
))
2745 // otherwise we return to where we came from
2746 DoSelectItem( prev
, unselect_others
, extended_select
);
2747 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2754 while ( IsExpanded(prev
) && HasChildren(prev
) )
2756 wxTreeItemId child
= GetLastChild(prev
);
2763 DoSelectItem( prev
, unselect_others
, extended_select
);
2764 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2769 // left arrow goes to the parent
2772 wxTreeItemId prev
= GetItemParent( m_current
);
2773 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2775 // don't go to root if it is hidden
2776 prev
= GetPrevSibling( m_current
);
2780 DoSelectItem( prev
, unselect_others
, extended_select
);
2786 // this works the same as the down arrow except that we
2787 // also expand the item if it wasn't expanded yet
2793 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2795 wxTreeItemIdValue cookie
;
2796 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2797 DoSelectItem( child
, unselect_others
, extended_select
);
2798 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2802 wxTreeItemId next
= GetNextSibling( m_key_current
);
2805 wxTreeItemId current
= m_key_current
;
2806 while (current
.IsOk() && !next
)
2808 current
= GetItemParent( current
);
2809 if (current
) next
= GetNextSibling( current
);
2814 DoSelectItem( next
, unselect_others
, extended_select
);
2815 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2821 // <End> selects the last visible tree item
2824 wxTreeItemId last
= GetRootItem();
2826 while ( last
.IsOk() && IsExpanded(last
) )
2828 wxTreeItemId lastChild
= GetLastChild(last
);
2830 // it may happen if the item was expanded but then all of
2831 // its children have been deleted - so IsExpanded() returned
2832 // true, but GetLastChild() returned invalid item
2841 DoSelectItem( last
, unselect_others
, extended_select
);
2846 // <Home> selects the root item
2849 wxTreeItemId prev
= GetRootItem();
2853 if ( HasFlag(wxTR_HIDE_ROOT
) )
2855 wxTreeItemIdValue cookie
;
2856 prev
= GetFirstChild(prev
, cookie
);
2861 DoSelectItem( prev
, unselect_others
, extended_select
);
2866 // do not use wxIsalnum() here
2867 if ( !event
.HasModifiers() &&
2868 ((keyCode
>= '0' && keyCode
<= '9') ||
2869 (keyCode
>= 'a' && keyCode
<= 'z') ||
2870 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2872 // find the next item starting with the given prefix
2873 wxChar ch
= (wxChar
)keyCode
;
2875 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2886 // also start the timer to reset the current prefix if the user
2887 // doesn't press any more alnum keys soon -- we wouldn't want
2888 // to use this prefix for a new item search
2891 m_findTimer
= new wxTreeFindTimer(this);
2894 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2903 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2905 // JACS: removed wxYieldIfNeeded() because it can cause the window
2906 // to be deleted from under us if a close window event is pending
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 WXUNUSED(textOnly
)) const
2938 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2940 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2943 GetViewStart(& startX
, & startY
);
2945 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2946 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2947 rect
.width
= i
->GetWidth();
2948 //rect.height = i->GetHeight();
2949 rect
.height
= GetLineHeight(i
);
2954 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2956 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2958 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2960 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2961 te
.m_item
= itemEdit
;
2962 te
.SetEventObject( this );
2963 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2969 // We have to call this here because the label in
2970 // question might just have been added and no screen
2971 // update taken place.
2973 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2979 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2981 m_textCtrl
->SetFocus();
2984 // returns a pointer to the text edit control if the item is being
2985 // edited, NULL otherwise (it's assumed that no more than one item may
2986 // be edited simultaneously)
2987 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2992 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2993 const wxString
& value
)
2995 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2997 le
.SetEventObject( this );
2999 le
.m_editCancelled
= false;
3001 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3004 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3006 // let owner know that the edit was cancelled
3007 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
3009 le
.SetEventObject( this );
3010 le
.m_label
= wxEmptyString
;
3011 le
.m_editCancelled
= true;
3013 GetEventHandler()->ProcessEvent( le
);
3016 void wxGenericTreeCtrl::OnRenameTimer()
3021 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3023 if ( !m_anchor
) return;
3025 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3027 // Is the mouse over a tree item button?
3029 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3030 wxGenericTreeItem
*underMouse
= thisItem
;
3032 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3033 #endif // wxUSE_TOOLTIPS
3036 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3037 (!event
.LeftIsDown()) &&
3039 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3047 if (underMouse
!= m_underMouse
)
3051 // unhighlight old item
3052 wxGenericTreeItem
*tmp
= m_underMouse
;
3053 m_underMouse
= NULL
;
3057 m_underMouse
= underMouse
;
3059 RefreshLine( m_underMouse
);
3063 // Determines what item we are hovering over and need a tooltip for
3064 wxTreeItemId hoverItem
= thisItem
;
3066 // We do not want a tooltip if we are dragging, or if the rename timer is running
3067 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3069 // Ask the tree control what tooltip (if any) should be shown
3070 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, GetId());
3071 hevent
.m_item
= hoverItem
;
3072 hevent
.SetEventObject(this);
3074 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3076 SetToolTip(hevent
.m_label
);
3081 // we process left mouse up event (enables in-place edit), right down
3082 // (pass to the user code), left dbl click (activate item) and
3083 // dragging/moving events for items drag-and-drop
3084 if ( !(event
.LeftDown() ||
3086 event
.RightDown() ||
3087 event
.LeftDClick() ||
3089 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3098 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3100 if ( event
.Dragging() && !m_isDragging
)
3102 if (m_dragCount
== 0)
3107 if (m_dragCount
!= 3)
3109 // wait until user drags a bit further...
3113 wxEventType command
= event
.RightIsDown()
3114 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3115 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3117 wxTreeEvent
nevent( command
, GetId() );
3118 nevent
.m_item
= m_current
;
3119 nevent
.SetEventObject(this);
3120 nevent
.SetPoint(pt
);
3122 // by default the dragging is not supported, the user code must
3123 // explicitly allow the event for it to take place
3126 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3128 // we're going to drag this item
3129 m_isDragging
= true;
3131 // remember the old cursor because we will change it while
3133 m_oldCursor
= m_cursor
;
3135 // in a single selection control, hide the selection temporarily
3136 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3138 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3140 if ( m_oldSelection
)
3142 m_oldSelection
->SetHilight(false);
3143 RefreshLine(m_oldSelection
);
3150 else if ( event
.Dragging() )
3152 if ( item
!= m_dropTarget
)
3154 // unhighlight the previous drop target
3155 DrawDropEffect(m_dropTarget
);
3157 m_dropTarget
= item
;
3159 // highlight the current drop target if any
3160 DrawDropEffect(m_dropTarget
);
3162 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3169 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3171 // erase the highlighting
3172 DrawDropEffect(m_dropTarget
);
3174 if ( m_oldSelection
)
3176 m_oldSelection
->SetHilight(true);
3177 RefreshLine(m_oldSelection
);
3178 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3181 // generate the drag end event
3182 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3184 eventEndDrag
.m_item
= item
;
3185 eventEndDrag
.m_pointDrag
= pt
;
3186 eventEndDrag
.SetEventObject(this);
3188 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3190 m_isDragging
= false;
3191 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3195 SetCursor(m_oldCursor
);
3197 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3205 // If we got to this point, we are not dragging or moving the mouse.
3206 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3207 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3208 // We skip even if we didn't hit an item because we still should
3209 // restore focus to the tree control even if we didn't exactly hit an item.
3210 if ( event
.LeftDown() )
3215 // here we process only the messages which happen on tree items
3219 if (item
== NULL
) return; /* we hit the blank area */
3221 if ( event
.RightDown() )
3223 // If the item is already selected, do not update the selection.
3224 // Multi-selections should not be cleared if a selected item is clicked.
3225 if (!IsSelected(item
))
3227 DoSelectItem(item
, true, false);
3230 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3231 nevent
.m_item
= item
;
3232 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3233 nevent
.SetEventObject(this);
3234 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3236 // Consistent with MSW (for now), send the ITEM_MENU *after*
3237 // the RIGHT_CLICK event. TODO: This behavior may change.
3238 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, GetId());
3239 nevent2
.m_item
= item
;
3240 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3241 nevent2
.SetEventObject(this);
3242 GetEventHandler()->ProcessEvent(nevent2
);
3244 else if ( event
.LeftUp() )
3246 // this facilitates multiple-item drag-and-drop
3248 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3250 wxArrayTreeItemIds selections
;
3251 size_t count
= GetSelections(selections
);
3257 DoSelectItem(item
, true, false);
3263 if ( (item
== m_current
) &&
3264 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3265 HasFlag(wxTR_EDIT_LABELS
) )
3267 if ( m_renameTimer
)
3269 if ( m_renameTimer
->IsRunning() )
3270 m_renameTimer
->Stop();
3274 m_renameTimer
= new wxTreeRenameTimer( this );
3277 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3280 m_lastOnSame
= false;
3283 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3285 if ( event
.LeftDown() )
3287 m_lastOnSame
= item
== m_current
;
3290 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3292 // only toggle the item for a single click, double click on
3293 // the button doesn't do anything (it toggles the item twice)
3294 if ( event
.LeftDown() )
3299 // don't select the item if the button was clicked
3304 // clear the previously selected items, if the
3305 // user clicked outside of the present selection.
3306 // otherwise, perform the deselection on mouse-up.
3307 // this allows multiple drag and drop to work.
3308 // but if Cmd is down, toggle selection of the clicked item
3309 if (!IsSelected(item
) || event
.CmdDown())
3311 // how should the selection work for this event?
3312 bool is_multiple
, extended_select
, unselect_others
;
3313 EventFlagsToSelType(GetWindowStyleFlag(),
3316 is_multiple
, extended_select
, unselect_others
);
3318 DoSelectItem(item
, unselect_others
, extended_select
);
3322 // For some reason, Windows isn't recognizing a left double-click,
3323 // so we need to simulate it here. Allow 200 milliseconds for now.
3324 if ( event
.LeftDClick() )
3326 // double clicking should not start editing the item label
3327 if ( m_renameTimer
)
3328 m_renameTimer
->Stop();
3330 m_lastOnSame
= false;
3332 // send activate event first
3333 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3334 nevent
.m_item
= item
;
3335 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3336 nevent
.SetEventObject( this );
3337 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3339 // if the user code didn't process the activate event,
3340 // handle it ourselves by toggling the item when it is
3342 if ( item
->HasPlus() )
3352 void wxGenericTreeCtrl::OnInternalIdle()
3354 wxWindow::OnInternalIdle();
3356 // Check if we need to select the root item
3357 // because nothing else has been selected.
3358 // Delaying it means that we can invoke event handlers
3359 // as required, when a first item is selected.
3360 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3363 SelectItem(m_select_me
);
3364 else if (GetRootItem().IsOk())
3365 SelectItem(GetRootItem());
3368 /* after all changes have been done to the tree control,
3369 * we actually redraw the tree when everything is over */
3371 if (!m_dirty
) return;
3372 if (m_freezeCount
) return;
3376 CalculatePositions();
3378 AdjustMyScrollbars();
3381 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3386 wxTreeItemAttr
*attr
= item
->GetAttributes();
3387 if ( attr
&& attr
->HasFont() )
3388 dc
.SetFont(attr
->GetFont());
3389 else if ( item
->IsBold() )
3390 dc
.SetFont(m_boldFont
);
3392 dc
.SetFont(m_normalFont
);
3394 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3397 // restore normal font
3398 dc
.SetFont( m_normalFont
);
3402 int image
= item
->GetCurrentImage();
3403 if ( image
!= NO_IMAGE
)
3405 if ( m_imageListNormal
)
3407 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3412 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3415 total_h
+= 2; // at least 2 pixels
3417 total_h
+= total_h
/10; // otherwise 10% extra spacing
3419 item
->SetHeight(total_h
);
3420 if (total_h
>m_lineHeight
)
3421 m_lineHeight
=total_h
;
3423 item
->SetWidth(image_w
+text_w
+2);
3426 // -----------------------------------------------------------------------------
3427 // for developper : y is now the top of the level
3428 // not the middle of it !
3429 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3431 int x
= level
*m_indent
;
3432 if (!HasFlag(wxTR_HIDE_ROOT
))
3436 else if (level
== 0)
3438 // a hidden root is not evaluated, but its
3439 // children are always calculated
3443 CalculateSize( item
, dc
);
3446 item
->SetX( x
+m_spacing
);
3448 y
+= GetLineHeight(item
);
3450 if ( !item
->IsExpanded() )
3452 // we don't need to calculate collapsed branches
3457 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3458 size_t n
, count
= children
.Count();
3460 for (n
= 0; n
< count
; ++n
)
3461 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3464 void wxGenericTreeCtrl::CalculatePositions()
3466 if ( !m_anchor
) return;
3468 wxClientDC
dc(this);
3471 dc
.SetFont( m_normalFont
);
3473 dc
.SetPen( m_dottedPen
);
3474 //if(GetImageList() == NULL)
3475 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3478 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3481 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3483 if (m_dirty
) return;
3484 if (m_freezeCount
) return;
3486 wxSize client
= GetClientSize();
3489 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3490 rect
.width
= client
.x
;
3491 rect
.height
= client
.y
;
3493 Refresh(true, &rect
);
3495 AdjustMyScrollbars();
3498 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3500 if (m_dirty
) return;
3501 if (m_freezeCount
) return;
3504 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3505 rect
.width
= GetClientSize().x
;
3506 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3508 Refresh(true, &rect
);
3511 void wxGenericTreeCtrl::RefreshSelected()
3513 if (m_freezeCount
) return;
3515 // TODO: this is awfully inefficient, we should keep the list of all
3516 // selected items internally, should be much faster
3518 RefreshSelectedUnder(m_anchor
);
3521 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3523 if (m_freezeCount
) return;
3525 if ( item
->IsSelected() )
3528 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3529 size_t count
= children
.GetCount();
3530 for ( size_t n
= 0; n
< count
; n
++ )
3532 RefreshSelectedUnder(children
[n
]);
3536 void wxGenericTreeCtrl::Freeze()
3541 void wxGenericTreeCtrl::Thaw()
3543 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen tree control?") );
3545 if ( !--m_freezeCount
)
3551 // ----------------------------------------------------------------------------
3552 // changing colours: we need to refresh the tree control
3553 // ----------------------------------------------------------------------------
3555 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3557 if ( !wxWindow::SetBackgroundColour(colour
) )
3560 if (m_freezeCount
) return true;
3567 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3569 if ( !wxWindow::SetForegroundColour(colour
) )
3572 if (m_freezeCount
) return true;
3579 // Process the tooltip event, to speed up event processing.
3580 // Doesn't actually get a tooltip.
3581 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3587 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
3589 // something is better than nothing...
3590 // 100x80 is what the MSW version will get from the default
3591 // wxControl::DoGetBestSize
3592 return wxSize(100,80);
3596 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3597 // be removed, as well as the #else case below.
3598 #define _USE_VISATTR 0
3601 #include "wx/listbox.h"
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 #if WXWIN_COMPATIBILITY_2_2
3640 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
3642 return GetItemParent( item
);
3645 #endif // WXWIN_COMPATIBILITY_2_2
3647 #endif // wxUSE_TREECTRL