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, Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
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 // -----------------------------------------------------------------------------
43 // -----------------------------------------------------------------------------
45 class WXDLLEXPORT wxGenericTreeItem
;
47 WX_DEFINE_EXPORTED_ARRAY(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 static const int NO_IMAGE
= -1;
55 static const int PIXELS_PER_UNIT
= 10;
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
62 static const char *aqua_arrow_right
[] = {
63 /* columns rows colors chars-per-pixel */
84 static const char *aqua_arrow_down
[] = {
85 /* columns rows colors chars-per-pixel */
105 // -----------------------------------------------------------------------------
107 // -----------------------------------------------------------------------------
109 // timer used for enabling in-place edit
110 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
113 // start editing the current item after half a second (if the mouse hasn't
114 // been clicked/moved)
115 enum { DELAY
= 500 };
117 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
119 virtual void Notify();
122 wxGenericTreeCtrl
*m_owner
;
125 // control used for in-place edit
126 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
129 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
132 void OnChar( wxKeyEvent
&event
);
133 void OnKeyUp( wxKeyEvent
&event
);
134 void OnKillFocus( wxFocusEvent
&event
);
136 bool AcceptChanges();
140 wxGenericTreeCtrl
*m_owner
;
141 wxGenericTreeItem
*m_itemEdited
;
142 wxString m_startValue
;
145 DECLARE_EVENT_TABLE()
148 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
149 // for a sufficiently long time
150 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
153 // reset the current prefix after half a second of inactivity
154 enum { DELAY
= 500 };
156 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
158 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
161 wxGenericTreeCtrl
*m_owner
;
165 class WXDLLEXPORT wxGenericTreeItem
169 wxGenericTreeItem() { m_data
= NULL
; }
170 wxGenericTreeItem( wxGenericTreeItem
*parent
,
171 const wxString
& text
,
174 wxTreeItemData
*data
);
176 ~wxGenericTreeItem();
179 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
181 const wxString
& GetText() const { return m_text
; }
182 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
183 { return m_images
[which
]; }
184 wxTreeItemData
*GetData() const { return m_data
; }
186 // returns the current image for the item (depending on its
187 // selected/expanded/whatever state)
188 int GetCurrentImage() const;
190 void SetText( const wxString
&text
);
191 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
192 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
194 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
196 void SetBold(bool bold
) { m_isBold
= bold
; }
198 int GetX() const { return m_x
; }
199 int GetY() const { return m_y
; }
201 void SetX(int x
) { m_x
= x
; }
202 void SetY(int y
) { m_y
= y
; }
204 int GetHeight() const { return m_height
; }
205 int GetWidth() const { return m_width
; }
207 void SetHeight(int h
) { m_height
= h
; }
208 void SetWidth(int w
) { m_width
= w
; }
210 wxGenericTreeItem
*GetParent() const { return m_parent
; }
213 // deletes all children notifying the treectrl about it if !NULL
215 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
217 // get count of all children (and grand children if 'recursively')
218 size_t GetChildrenCount(bool recursively
= TRUE
) const;
220 void Insert(wxGenericTreeItem
*child
, size_t index
)
221 { m_children
.Insert(child
, index
); }
223 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
225 // return the item at given position (or NULL if no item), onButton is
226 // TRUE if the point belongs to the item's button, otherwise it lies
227 // on the button's label
228 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
229 const wxGenericTreeCtrl
*,
233 void Expand() { m_isCollapsed
= FALSE
; }
234 void Collapse() { m_isCollapsed
= TRUE
; }
236 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
239 bool HasChildren() const { return !m_children
.IsEmpty(); }
240 bool IsSelected() const { return m_hasHilight
!= 0; }
241 bool IsExpanded() const { return !m_isCollapsed
; }
242 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
243 bool IsBold() const { return m_isBold
!= 0; }
246 // get them - may be NULL
247 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
248 // get them ensuring that the pointer is not NULL
249 wxTreeItemAttr
& Attr()
253 m_attr
= new wxTreeItemAttr
;
259 void SetAttributes(wxTreeItemAttr
*attr
)
261 if ( m_ownsAttr
) delete m_attr
;
265 // set them and delete when done
266 void AssignAttributes(wxTreeItemAttr
*attr
)
273 // since there can be very many of these, we save size by chosing
274 // the smallest representation for the elements and by ordering
275 // the members to avoid padding.
276 wxString m_text
; // label to be rendered for item
278 wxTreeItemData
*m_data
; // user-provided data
280 wxArrayGenericTreeItems m_children
; // list of children
281 wxGenericTreeItem
*m_parent
; // parent of this item
283 wxTreeItemAttr
*m_attr
; // attributes???
285 // tree ctrl images for the normal, selected, expanded and
286 // expanded+selected states
287 short m_images
[wxTreeItemIcon_Max
];
289 wxCoord m_x
; // (virtual) offset from top
290 short m_y
; // (virtual) offset from left
291 short m_width
; // width of this item
292 unsigned char m_height
; // height of this item
294 // use bitfields to save size
295 int m_isCollapsed
:1;
296 int m_hasHilight
:1; // same as focused
297 int m_hasPlus
:1; // used for item which doesn't have
298 // children but has a [+] button
299 int m_isBold
:1; // render the label in bold font
300 int m_ownsAttr
:1; // delete attribute when done
303 // =============================================================================
305 // =============================================================================
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 // translate the key or mouse event flags to the type of selection we're
313 static void EventFlagsToSelType(long style
,
317 bool &extended_select
,
318 bool &unselect_others
)
320 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
321 extended_select
= shiftDown
&& is_multiple
;
322 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
325 // check if the given item is under another one
326 static bool IsDescendantOf(wxGenericTreeItem
*parent
, wxGenericTreeItem
*item
)
330 if ( item
== parent
)
332 // item is a descendant of parent
336 item
= item
->GetParent();
342 // -----------------------------------------------------------------------------
343 // wxTreeRenameTimer (internal)
344 // -----------------------------------------------------------------------------
346 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
351 void wxTreeRenameTimer::Notify()
353 m_owner
->OnRenameTimer();
356 //-----------------------------------------------------------------------------
357 // wxTreeTextCtrl (internal)
358 //-----------------------------------------------------------------------------
360 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
361 EVT_CHAR (wxTreeTextCtrl::OnChar
)
362 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
363 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
366 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
367 wxGenericTreeItem
*item
)
368 : m_itemEdited(item
), m_startValue(item
->GetText())
373 int w
= m_itemEdited
->GetWidth(),
374 h
= m_itemEdited
->GetHeight();
377 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
382 int image
= item
->GetCurrentImage();
383 if ( image
!= NO_IMAGE
)
385 if ( m_owner
->m_imageListNormal
)
387 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
392 wxFAIL_MSG(_T("you must create an image list to use images!"));
396 // FIXME: what are all these hardcoded 4, 8 and 11s really?
400 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
401 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
404 bool wxTreeTextCtrl::AcceptChanges()
406 const wxString value
= GetValue();
408 if ( value
== m_startValue
)
410 // nothing changed, always accept
414 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
416 // vetoed by the user
420 // accepted, do rename the item
421 m_owner
->SetItemText(m_itemEdited
, value
);
426 void wxTreeTextCtrl::Finish()
430 wxPendingDelete
.Append(this);
434 m_owner
->SetFocus(); // This doesn't work. TODO.
438 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
440 switch ( event
.m_keyCode
)
443 if ( !AcceptChanges() )
445 // vetoed by the user, don't disappear
459 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
463 // auto-grow the textctrl:
464 wxSize parentSize
= m_owner
->GetSize();
465 wxPoint myPos
= GetPosition();
466 wxSize mySize
= GetSize();
468 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
469 if (myPos
.x
+ sx
> parentSize
.x
)
470 sx
= parentSize
.x
- myPos
.x
;
479 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
487 if ( AcceptChanges() )
493 // -----------------------------------------------------------------------------
495 // -----------------------------------------------------------------------------
497 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
498 const wxString
& text
,
499 int image
, int selImage
,
500 wxTreeItemData
*data
)
503 m_images
[wxTreeItemIcon_Normal
] = image
;
504 m_images
[wxTreeItemIcon_Selected
] = selImage
;
505 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
506 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
511 m_isCollapsed
= TRUE
;
512 m_hasHilight
= FALSE
;
518 m_attr
= (wxTreeItemAttr
*)NULL
;
521 // We don't know the height here yet.
526 wxGenericTreeItem::~wxGenericTreeItem()
530 if (m_ownsAttr
) delete m_attr
;
532 wxASSERT_MSG( m_children
.IsEmpty(),
533 wxT("please call DeleteChildren() before deleting the item") );
536 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
538 size_t count
= m_children
.Count();
539 for ( size_t n
= 0; n
< count
; n
++ )
541 wxGenericTreeItem
*child
= m_children
[n
];
543 tree
->SendDeleteEvent(child
);
545 child
->DeleteChildren(tree
);
552 void wxGenericTreeItem::SetText( const wxString
&text
)
557 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
559 size_t count
= m_children
.Count();
563 size_t total
= count
;
564 for (size_t n
= 0; n
< count
; ++n
)
566 total
+= m_children
[n
]->GetChildrenCount();
572 void wxGenericTreeItem::GetSize( int &x
, int &y
,
573 const wxGenericTreeCtrl
*theButton
)
575 int bottomY
=m_y
+theButton
->GetLineHeight(this);
576 if ( y
< bottomY
) y
= bottomY
;
577 int width
= m_x
+ m_width
;
578 if ( x
< width
) x
= width
;
582 size_t count
= m_children
.Count();
583 for ( size_t n
= 0; n
< count
; ++n
)
585 m_children
[n
]->GetSize( x
, y
, theButton
);
590 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
591 const wxGenericTreeCtrl
*theCtrl
,
595 // for a hidden root node, don't evaluate it, but do evaluate children
596 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
599 int h
= theCtrl
->GetLineHeight(this);
600 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
602 int y_mid
= m_y
+ h
/2;
603 if (point
.y
< y_mid
)
604 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
606 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
608 // 5 is the size of the plus sign
609 int xCross
= m_x
- theCtrl
->GetSpacing();
610 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
611 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
612 HasPlus() && theCtrl
->HasButtons() )
614 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
618 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
623 // assuming every image (normal and selected) has the same size!
624 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
625 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
628 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
629 flags
|= wxTREE_HITTEST_ONITEMICON
;
631 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
637 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
638 if (point
.x
> m_x
+m_width
)
639 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
644 // if children are expanded, fall through to evaluate them
645 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
649 size_t count
= m_children
.Count();
650 for ( size_t n
= 0; n
< count
; n
++ )
652 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
660 return (wxGenericTreeItem
*) NULL
;
663 int wxGenericTreeItem::GetCurrentImage() const
665 int image
= NO_IMAGE
;
670 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
673 if ( image
== NO_IMAGE
)
675 // we usually fall back to the normal item, but try just the
676 // expanded one (and not selected) first in this case
677 image
= GetImage(wxTreeItemIcon_Expanded
);
683 image
= GetImage(wxTreeItemIcon_Selected
);
686 // maybe it doesn't have the specific image we want,
687 // try the default one instead
688 if ( image
== NO_IMAGE
) image
= GetImage();
693 // -----------------------------------------------------------------------------
694 // wxGenericTreeCtrl implementation
695 // -----------------------------------------------------------------------------
697 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
699 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
700 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
701 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
702 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
703 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
704 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
705 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
708 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
710 * wxTreeCtrl has to be a real class or we have problems with
711 * the run-time information.
714 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
717 // -----------------------------------------------------------------------------
718 // construction/destruction
719 // -----------------------------------------------------------------------------
721 void wxGenericTreeCtrl::Init()
723 m_current
= m_key_current
= m_anchor
= (wxGenericTreeItem
*) NULL
;
731 m_hilightBrush
= new wxBrush
733 wxSystemSettings::GetColour
735 wxSYS_COLOUR_HIGHLIGHT
740 m_hilightUnfocusedBrush
= new wxBrush
742 wxSystemSettings::GetColour
744 wxSYS_COLOUR_BTNSHADOW
749 m_imageListNormal
= m_imageListButtons
=
750 m_imageListState
= (wxImageList
*) NULL
;
751 m_ownsImageListNormal
= m_ownsImageListButtons
=
752 m_ownsImageListState
= FALSE
;
755 m_isDragging
= FALSE
;
756 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
758 m_renameTimer
= NULL
;
761 m_lastOnSame
= FALSE
;
763 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
764 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
765 m_normalFont
.GetFamily(),
766 m_normalFont
.GetStyle(),
768 m_normalFont
.GetUnderlined());
771 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
776 const wxValidator
&validator
,
777 const wxString
& name
)
781 wxGetOsVersion( &major
, &minor
);
783 if (style
& wxTR_HAS_BUTTONS
) style
|= wxTR_MAC_BUTTONS
;
784 if (style
& wxTR_HAS_BUTTONS
) style
&= ~wxTR_HAS_BUTTONS
;
785 style
&= ~wxTR_LINES_AT_ROOT
;
786 style
|= wxTR_NO_LINES
;
788 style
|= wxTR_ROW_LINES
;
790 style
|= wxTR_AQUA_BUTTONS
;
793 if (style
& wxTR_AQUA_BUTTONS
)
795 m_arrowRight
= new wxBitmap( aqua_arrow_right
);
796 m_arrowDown
= new wxBitmap( aqua_arrow_down
);
804 wxScrolledWindow::Create( parent
, id
, pos
, size
,
805 style
|wxHSCROLL
|wxVSCROLL
, name
);
807 // If the tree display has no buttons, but does have
808 // connecting lines, we can use a narrower layout.
809 // It may not be a good idea to force this...
810 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
817 SetValidator( validator
);
820 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
821 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
823 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
824 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
829 wxGenericTreeCtrl::~wxGenericTreeCtrl()
831 delete m_hilightBrush
;
832 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
856 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
859 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
861 m_indent
= (unsigned short) indent
;
865 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
867 m_spacing
= (unsigned short) spacing
;
871 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
873 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
875 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
878 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
880 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
882 // if we will hide the root, make sure children are visible
883 m_anchor
->SetHasPlus();
885 CalculatePositions();
888 // right now, just sets the styles. Eventually, we may
889 // want to update the inherited styles, but right now
890 // none of the parents has updatable styles
891 m_windowStyle
= styles
;
895 // -----------------------------------------------------------------------------
896 // functions to work with tree items
897 // -----------------------------------------------------------------------------
899 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
901 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
903 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
906 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
907 wxTreeItemIcon which
) const
909 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
911 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
914 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
916 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
918 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
921 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
923 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
926 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
927 pItem
->SetText(text
);
928 CalculateSize(pItem
, dc
);
932 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
934 wxTreeItemIcon which
)
936 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
938 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
939 pItem
->SetImage(image
, which
);
942 CalculateSize(pItem
, dc
);
946 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
948 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
950 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
953 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
955 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
957 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
958 pItem
->SetHasPlus(has
);
962 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
964 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
966 // avoid redrawing the tree if no real change
967 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
968 if ( pItem
->IsBold() != bold
)
970 pItem
->SetBold(bold
);
975 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
978 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
980 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
981 pItem
->Attr().SetTextColour(col
);
985 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
988 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
990 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
991 pItem
->Attr().SetBackgroundColour(col
);
995 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
997 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
999 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1000 pItem
->Attr().SetFont(font
);
1004 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1006 wxScrolledWindow::SetFont(font
);
1008 m_normalFont
= font
;
1009 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
1010 m_normalFont
.GetFamily(),
1011 m_normalFont
.GetStyle(),
1013 m_normalFont
.GetUnderlined());
1019 // -----------------------------------------------------------------------------
1020 // item status inquiries
1021 // -----------------------------------------------------------------------------
1023 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1025 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1027 // An item is only visible if it's not a descendant of a collapsed item
1028 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1029 wxGenericTreeItem
* parent
= pItem
->GetParent();
1032 if (!parent
->IsExpanded())
1034 parent
= parent
->GetParent();
1038 GetViewStart(& startX
, & startY
);
1040 wxSize clientSize
= GetClientSize();
1043 if (!GetBoundingRect(item
, rect
))
1045 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1047 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1049 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1055 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1057 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1059 // consider that the item does have children if it has the "+" button: it
1060 // might not have them (if it had never been expanded yet) but then it
1061 // could have them as well and it's better to err on this side rather than
1062 // disabling some operations which are restricted to the items with
1063 // children for an item which does have them
1064 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1067 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1069 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1071 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1074 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1076 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1078 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1081 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1083 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1085 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1088 // -----------------------------------------------------------------------------
1090 // -----------------------------------------------------------------------------
1092 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1094 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1096 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1099 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
1101 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1104 return GetNextChild(item
, cookie
);
1107 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
1109 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1111 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1112 if ( (size_t)cookie
< children
.Count() )
1114 return children
.Item((size_t)cookie
++);
1118 // there are no more of them
1119 return wxTreeItemId();
1123 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1125 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1127 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1128 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1131 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1133 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1135 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1136 wxGenericTreeItem
*parent
= i
->GetParent();
1137 if ( parent
== NULL
)
1139 // root item doesn't have any siblings
1140 return wxTreeItemId();
1143 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1144 int index
= siblings
.Index(i
);
1145 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1147 size_t n
= (size_t)(index
+ 1);
1148 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1151 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1153 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1155 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1156 wxGenericTreeItem
*parent
= i
->GetParent();
1157 if ( parent
== NULL
)
1159 // root item doesn't have any siblings
1160 return wxTreeItemId();
1163 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1164 int index
= siblings
.Index(i
);
1165 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1167 return index
== 0 ? wxTreeItemId()
1168 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1171 // Only for internal use right now, but should probably be public
1172 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1174 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1176 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1178 // First see if there are any children.
1179 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1180 if (children
.GetCount() > 0)
1182 return children
.Item(0);
1186 // Try a sibling of this or ancestor instead
1187 wxTreeItemId p
= item
;
1188 wxTreeItemId toFind
;
1191 toFind
= GetNextSibling(p
);
1193 } while (p
.IsOk() && !toFind
.IsOk());
1198 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1200 wxTreeItemId id
= GetRootItem();
1209 } while (id
.IsOk());
1211 return wxTreeItemId();
1214 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1216 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1218 wxTreeItemId id
= item
;
1221 while (id
= GetNext(id
), id
.IsOk())
1227 return wxTreeItemId();
1230 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1232 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1234 wxFAIL_MSG(wxT("not implemented"));
1236 return wxTreeItemId();
1239 // find the first item starting with the given prefix after the given item
1240 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1241 const wxString
& prefixOrig
) const
1243 // match is case insensitive as this is more convenient to the user: having
1244 // to press Shift-letter to go to the item starting with a capital letter
1245 // would be too bothersome
1246 wxString prefix
= prefixOrig
.Lower();
1248 // determine the starting point: we shouldn't take the current item (this
1249 // allows to switch between two items starting with the same letter just by
1250 // pressing it) but we shouldn't jump to the next one if the user is
1251 // continuing to type as otherwise he might easily skip the item he wanted
1252 wxTreeItemId id
= idParent
;
1253 if ( prefix
.length() == 1 )
1258 // look for the item starting with the given prefix after it
1259 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1264 // if we haven't found anything...
1267 // ... wrap to the beginning
1269 if ( HasFlag(wxTR_HIDE_ROOT
) )
1271 // can't select virtual root
1275 // and try all the items (stop when we get to the one we started from)
1276 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1285 // -----------------------------------------------------------------------------
1287 // -----------------------------------------------------------------------------
1289 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1291 const wxString
& text
,
1292 int image
, int selImage
,
1293 wxTreeItemData
*data
)
1295 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1298 // should we give a warning here?
1299 return AddRoot(text
, image
, selImage
, data
);
1302 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1304 wxGenericTreeItem
*item
=
1305 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1309 data
->m_pItem
= (long) item
;
1312 parent
->Insert( item
, previous
);
1317 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1318 int image
, int selImage
,
1319 wxTreeItemData
*data
)
1321 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1323 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1325 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1326 image
, selImage
, data
);
1329 data
->m_pItem
= (long) m_anchor
;
1332 if (HasFlag(wxTR_HIDE_ROOT
))
1334 // if root is hidden, make sure we can navigate
1336 m_anchor
->SetHasPlus();
1338 CalculatePositions();
1341 if (!HasFlag(wxTR_MULTIPLE
))
1343 m_current
= m_key_current
= m_anchor
;
1344 m_current
->SetHilight( TRUE
);
1350 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1351 const wxString
& text
,
1352 int image
, int selImage
,
1353 wxTreeItemData
*data
)
1355 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1358 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1359 const wxTreeItemId
& idPrevious
,
1360 const wxString
& text
,
1361 int image
, int selImage
,
1362 wxTreeItemData
*data
)
1364 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1367 // should we give a warning here?
1368 return AddRoot(text
, image
, selImage
, data
);
1372 if (idPrevious
.IsOk())
1374 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1375 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1376 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1379 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1382 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1384 const wxString
& text
,
1385 int image
, int selImage
,
1386 wxTreeItemData
*data
)
1388 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1391 // should we give a warning here?
1392 return AddRoot(text
, image
, selImage
, data
);
1395 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1398 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1399 const wxString
& text
,
1400 int image
, int selImage
,
1401 wxTreeItemData
*data
)
1403 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1406 // should we give a warning here?
1407 return AddRoot(text
, image
, selImage
, data
);
1410 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1411 image
, selImage
, data
);
1414 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1416 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1417 event
.m_item
= (long) item
;
1418 event
.SetEventObject( this );
1419 ProcessEvent( event
);
1422 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1424 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1426 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1427 item
->DeleteChildren(this);
1430 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1432 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1434 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1436 wxGenericTreeItem
*parent
= item
->GetParent();
1438 // don't keep stale pointers around!
1439 if ( IsDescendantOf(item
, m_key_current
) )
1441 m_key_current
= parent
;
1444 if ( IsDescendantOf(item
, m_current
) )
1449 // remove the item from the tree
1452 parent
->GetChildren().Remove( item
); // remove by value
1454 else // deleting the root
1456 // nothing will be left in the tree
1460 // and delete all of its children and the item itself now
1461 item
->DeleteChildren(this);
1462 SendDeleteEvent(item
);
1466 void wxGenericTreeCtrl::DeleteAllItems()
1474 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1476 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1478 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1479 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1480 _T("can't expand hidden root") );
1482 if ( !item
->HasPlus() )
1485 if ( item
->IsExpanded() )
1488 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1489 event
.m_item
= (long) item
;
1490 event
.SetEventObject( this );
1492 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1494 // cancelled by program
1499 CalculatePositions();
1501 RefreshSubtree(item
);
1503 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1504 ProcessEvent( event
);
1507 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1509 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1512 if ( !IsExpanded(item
) )
1517 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1518 while ( child
.IsOk() )
1522 child
= GetNextChild(item
, cookie
);
1526 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1528 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1529 _T("can't collapse hidden root") );
1531 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1533 if ( !item
->IsExpanded() )
1536 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1537 event
.m_item
= (long) item
;
1538 event
.SetEventObject( this );
1539 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1541 // cancelled by program
1547 #if 0 // TODO why should items be collapsed recursively?
1548 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1549 size_t count
= children
.Count();
1550 for ( size_t n
= 0; n
< count
; n
++ )
1552 Collapse(children
[n
]);
1556 CalculatePositions();
1558 RefreshSubtree(item
);
1560 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1561 ProcessEvent( event
);
1564 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1567 DeleteChildren(item
);
1570 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1572 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1574 if (item
->IsExpanded())
1580 void wxGenericTreeCtrl::Unselect()
1584 m_current
->SetHilight( FALSE
);
1585 RefreshLine( m_current
);
1591 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1593 if (item
->IsSelected())
1595 item
->SetHilight(FALSE
);
1599 if (item
->HasChildren())
1601 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1602 size_t count
= children
.Count();
1603 for ( size_t n
= 0; n
< count
; ++n
)
1605 UnselectAllChildren(children
[n
]);
1610 void wxGenericTreeCtrl::UnselectAll()
1612 wxTreeItemId rootItem
= GetRootItem();
1614 // the tree might not have the root item at all
1617 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1621 // Recursive function !
1622 // To stop we must have crt_item<last_item
1624 // Tag all next children, when no more children,
1625 // Move to parent (not to tag)
1626 // Keep going... if we found last_item, we stop.
1627 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1629 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1631 if (parent
== NULL
) // This is root item
1632 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1634 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1635 int index
= children
.Index(crt_item
);
1636 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1638 size_t count
= children
.Count();
1639 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1641 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1644 return TagNextChildren(parent
, last_item
, select
);
1647 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1649 crt_item
->SetHilight(select
);
1650 RefreshLine(crt_item
);
1652 if (crt_item
==last_item
)
1655 if (crt_item
->HasChildren())
1657 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1658 size_t count
= children
.Count();
1659 for ( size_t n
= 0; n
< count
; ++n
)
1661 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1669 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1671 // item2 is not necessary after item1
1672 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1674 // choice first' and 'last' between item1 and item2
1675 if (item1
->GetY()<item2
->GetY())
1686 bool select
= m_current
->IsSelected();
1688 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1691 TagNextChildren(first
,last
,select
);
1694 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1695 bool unselect_others
,
1696 bool extended_select
)
1698 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1700 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1701 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1703 //wxCHECK_RET( ( (!unselect_others) && is_single),
1704 // wxT("this is a single selection tree") );
1706 // to keep going anyhow !!!
1709 if (item
->IsSelected())
1710 return; // nothing to do
1711 unselect_others
= TRUE
;
1712 extended_select
= FALSE
;
1714 else if ( unselect_others
&& item
->IsSelected() )
1716 // selection change if there is more than one item currently selected
1717 wxArrayTreeItemIds selected_items
;
1718 if ( GetSelections(selected_items
) == 1 )
1722 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1723 event
.m_item
= (long) item
;
1724 event
.m_itemOld
= (long) m_current
;
1725 event
.SetEventObject( this );
1726 // TODO : Here we don't send any selection mode yet !
1728 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1731 wxTreeItemId parent
= GetParent( itemId
);
1732 while (parent
.IsOk())
1734 if (!IsExpanded(parent
))
1737 parent
= GetParent( parent
);
1740 EnsureVisible( itemId
);
1743 if (unselect_others
)
1745 if (is_single
) Unselect(); // to speed up thing
1750 if (extended_select
)
1754 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1757 // don't change the mark (m_current)
1758 SelectItemRange(m_current
, item
);
1762 bool select
=TRUE
; // the default
1764 // Check if we need to toggle hilight (ctrl mode)
1765 if (!unselect_others
)
1766 select
=!item
->IsSelected();
1768 m_current
= m_key_current
= item
;
1769 m_current
->SetHilight(select
);
1770 RefreshLine( m_current
);
1773 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1774 GetEventHandler()->ProcessEvent( event
);
1777 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1778 wxArrayTreeItemIds
&array
) const
1780 if ( item
->IsSelected() )
1781 array
.Add(wxTreeItemId(item
));
1783 if ( item
->HasChildren() )
1785 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1786 size_t count
= children
.GetCount();
1787 for ( size_t n
= 0; n
< count
; ++n
)
1788 FillArray(children
[n
], array
);
1792 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1795 wxTreeItemId idRoot
= GetRootItem();
1796 if ( idRoot
.IsOk() )
1798 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1800 //else: the tree is empty, so no selections
1802 return array
.Count();
1805 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1807 if (!item
.IsOk()) return;
1809 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1811 // first expand all parent branches
1812 wxGenericTreeItem
*parent
= gitem
->GetParent();
1814 if ( HasFlag(wxTR_HIDE_ROOT
) )
1816 while ( parent
!= m_anchor
)
1819 parent
= parent
->GetParent();
1827 parent
= parent
->GetParent();
1831 //if (parent) CalculatePositions();
1836 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1838 if (!item
.IsOk()) return;
1840 // We have to call this here because the label in
1841 // question might just have been added and no screen
1842 // update taken place.
1843 if (m_dirty
) wxYieldIfNeeded();
1845 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1847 // now scroll to the item
1848 int item_y
= gitem
->GetY();
1852 GetViewStart( &start_x
, &start_y
);
1853 start_y
*= PIXELS_PER_UNIT
;
1857 GetClientSize( &client_w
, &client_h
);
1859 if (item_y
< start_y
+3)
1864 m_anchor
->GetSize( x
, y
, this );
1865 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1866 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1867 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1868 // Item should appear at top
1869 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1871 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1876 m_anchor
->GetSize( x
, y
, this );
1877 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1878 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1879 item_y
+= PIXELS_PER_UNIT
+2;
1880 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1881 // Item should appear at bottom
1882 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
);
1886 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1887 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1889 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1890 wxGenericTreeItem
**item2
)
1892 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1894 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1897 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1898 const wxTreeItemId
& item2
)
1900 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1903 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1905 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1907 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1909 wxCHECK_RET( !s_treeBeingSorted
,
1910 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1912 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1913 if ( children
.Count() > 1 )
1917 s_treeBeingSorted
= this;
1918 children
.Sort(tree_ctrl_compare_func
);
1919 s_treeBeingSorted
= NULL
;
1921 //else: don't make the tree dirty as nothing changed
1924 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1926 return m_imageListNormal
;
1929 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1931 return m_imageListButtons
;
1934 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1936 return m_imageListState
;
1939 void wxGenericTreeCtrl::CalculateLineHeight()
1941 wxClientDC
dc(this);
1942 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1944 if ( m_imageListNormal
)
1946 // Calculate a m_lineHeight value from the normal Image sizes.
1947 // May be toggle off. Then wxGenericTreeCtrl will spread when
1948 // necessary (which might look ugly).
1949 int n
= m_imageListNormal
->GetImageCount();
1950 for (int i
= 0; i
< n
; i
++)
1952 int width
= 0, height
= 0;
1953 m_imageListNormal
->GetSize(i
, width
, height
);
1954 if (height
> m_lineHeight
) m_lineHeight
= height
;
1958 if (m_imageListButtons
)
1960 // Calculate a m_lineHeight value from the Button image sizes.
1961 // May be toggle off. Then wxGenericTreeCtrl will spread when
1962 // necessary (which might look ugly).
1963 int n
= m_imageListButtons
->GetImageCount();
1964 for (int i
= 0; i
< n
; i
++)
1966 int width
= 0, height
= 0;
1967 m_imageListButtons
->GetSize(i
, width
, height
);
1968 if (height
> m_lineHeight
) m_lineHeight
= height
;
1972 if (m_lineHeight
< 30)
1973 m_lineHeight
+= 2; // at least 2 pixels
1975 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1978 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1980 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1981 m_imageListNormal
= imageList
;
1982 m_ownsImageListNormal
= FALSE
;
1984 // Don't do any drawing if we're setting the list to NULL,
1985 // since we may be in the process of deleting the tree control.
1987 CalculateLineHeight();
1990 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1992 if (m_ownsImageListState
) delete m_imageListState
;
1993 m_imageListState
= imageList
;
1994 m_ownsImageListState
= FALSE
;
1997 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1999 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2000 m_imageListButtons
= imageList
;
2001 m_ownsImageListButtons
= FALSE
;
2003 CalculateLineHeight();
2006 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2008 SetImageList(imageList
);
2009 m_ownsImageListNormal
= TRUE
;
2012 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2014 SetStateImageList(imageList
);
2015 m_ownsImageListState
= TRUE
;
2018 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2020 SetButtonsImageList(imageList
);
2021 m_ownsImageListButtons
= TRUE
;
2024 // -----------------------------------------------------------------------------
2026 // -----------------------------------------------------------------------------
2028 void wxGenericTreeCtrl::AdjustMyScrollbars()
2033 m_anchor
->GetSize( x
, y
, this );
2034 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2035 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2036 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2037 int y_pos
= GetScrollPos( wxVERTICAL
);
2038 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2042 SetScrollbars( 0, 0, 0, 0 );
2046 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2048 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2049 return item
->GetHeight();
2051 return m_lineHeight
;
2054 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2056 // TODO implement "state" icon on items
2058 wxTreeItemAttr
*attr
= item
->GetAttributes();
2059 if ( attr
&& attr
->HasFont() )
2060 dc
.SetFont(attr
->GetFont());
2061 else if (item
->IsBold())
2062 dc
.SetFont(m_boldFont
);
2064 long text_w
= 0, text_h
= 0;
2065 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2067 int image_h
= 0, image_w
= 0;
2068 int image
= item
->GetCurrentImage();
2069 if ( image
!= NO_IMAGE
)
2071 if ( m_imageListNormal
)
2073 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2082 int total_h
= GetLineHeight(item
);
2084 if ( item
->IsSelected() )
2086 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2091 if ( attr
&& attr
->HasBackgroundColour() )
2092 colBg
= attr
->GetBackgroundColour();
2094 colBg
= m_backgroundColour
;
2095 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2098 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2100 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2104 DoGetPosition(&x
, &y
);
2106 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2110 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2112 // If it's selected, and there's an image, then we should
2113 // take care to leave the area under the image painted in the
2114 // background colour.
2115 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2116 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2120 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2121 item
->GetWidth()+2, total_h
-offset
);
2125 if ( image
!= NO_IMAGE
)
2127 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2128 m_imageListNormal
->Draw( image
, dc
,
2130 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2131 wxIMAGELIST_DRAW_TRANSPARENT
);
2132 dc
.DestroyClippingRegion();
2135 dc
.SetBackgroundMode(wxTRANSPARENT
);
2136 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2137 dc
.DrawText( item
->GetText(),
2138 (wxCoord
)(image_w
+ item
->GetX()),
2139 (wxCoord
)(item
->GetY() + extraH
));
2141 // restore normal font
2142 dc
.SetFont( m_normalFont
);
2145 // Now y stands for the top of the item, whereas it used to stand for middle !
2146 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2148 int x
= level
*m_indent
;
2149 if (!HasFlag(wxTR_HIDE_ROOT
))
2153 else if (level
== 0)
2155 // always expand hidden root
2157 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2158 int count
= children
.Count();
2164 PaintLevel(children
[n
], dc
, 1, y
);
2165 } while (++n
< count
);
2167 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2169 // draw line down to last child
2170 origY
+= GetLineHeight(children
[0])>>1;
2171 oldY
+= GetLineHeight(children
[n
-1])>>1;
2172 dc
.DrawLine(3, origY
, 3, oldY
);
2178 item
->SetX(x
+m_spacing
);
2181 int h
= GetLineHeight(item
);
2183 int y_mid
= y_top
+ (h
>>1);
2186 int exposed_x
= dc
.LogicalToDeviceX(0);
2187 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2189 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2193 // don't draw rect outline if we already have the
2194 // background color under Mac
2195 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2196 #endif // !__WXMAC__
2200 if ( item
->IsSelected() )
2202 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2206 wxTreeItemAttr
*attr
= item
->GetAttributes();
2207 if (attr
&& attr
->HasTextColour())
2208 colText
= attr
->GetTextColour();
2210 colText
= GetForegroundColour();
2214 dc
.SetTextForeground(colText
);
2218 PaintItem(item
, dc
);
2220 if (HasFlag(wxTR_ROW_LINES
))
2222 // if the background colour is white, choose a
2223 // contrasting color for the lines
2224 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2225 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2226 dc
.DrawLine(0, y_top
, 10000, y_top
);
2227 dc
.DrawLine(0, y
, 10000, y
);
2230 // restore DC objects
2231 dc
.SetBrush(*wxWHITE_BRUSH
);
2232 dc
.SetPen(m_dottedPen
);
2233 dc
.SetTextForeground(*wxBLACK
);
2235 if (item
->HasPlus() && HasButtons()) // should the item show a button?
2237 if (!HasFlag(wxTR_NO_LINES
))
2239 if (x
> (signed)m_indent
)
2240 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
2241 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2242 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
2243 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
2246 if (m_imageListButtons
!= NULL
)
2248 // draw the image button here
2249 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
2250 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
2251 if (item
->IsSelected())
2252 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2253 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2254 int xx
= x
- (image_w
>>1);
2255 int yy
= y_mid
- (image_h
>>1);
2256 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
2257 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2258 wxIMAGELIST_DRAW_TRANSPARENT
);
2259 dc
.DestroyClippingRegion();
2261 else if (HasFlag(wxTR_TWIST_BUTTONS
))
2263 // draw the twisty button here
2265 if (HasFlag(wxTR_AQUA_BUTTONS
))
2267 if (item
->IsExpanded())
2268 dc
.DrawBitmap( *m_arrowDown
, x
-5, y_mid
-6, TRUE
);
2270 dc
.DrawBitmap( *m_arrowRight
, x
-5, y_mid
-6, TRUE
);
2274 dc
.SetBrush(*m_hilightBrush
);
2275 dc
.SetPen(*wxBLACK_PEN
);
2278 if (item
->IsExpanded())
2281 button
[0].y
= y_mid
-2;
2283 button
[1].y
= y_mid
-2;
2285 button
[2].y
= y_mid
+3;
2289 button
[0].y
= y_mid
-5;
2291 button
[1].y
= y_mid
+5;
2293 button
[2].y
= y_mid
;
2296 dc
.DrawPolygon(3, button
);
2297 dc
.SetPen(m_dottedPen
);
2300 else // if (HasFlag(wxTR_HAS_BUTTONS))
2302 // draw the plus sign here
2303 dc
.SetPen(*wxGREY_PEN
);
2304 dc
.SetBrush(*wxWHITE_BRUSH
);
2305 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2306 dc
.SetPen(*wxBLACK_PEN
);
2307 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2308 if (!item
->IsExpanded())
2309 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2310 dc
.SetPen(m_dottedPen
);
2313 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2315 // draw the horizontal line here
2317 if (x
> (signed)m_indent
)
2318 x_start
-= m_indent
;
2319 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2321 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2325 if (item
->IsExpanded())
2327 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2328 int count
= children
.Count();
2335 PaintLevel(children
[n
], dc
, level
, y
);
2336 } while (++n
< count
);
2338 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2340 // draw line down to last child
2341 oldY
+= GetLineHeight(children
[n
-1])>>1;
2342 if (HasButtons()) y_mid
+= 5;
2343 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2349 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2353 if ( item
->HasPlus() )
2355 // it's a folder, indicate it by a border
2360 // draw a line under the drop target because the item will be
2362 DrawLine(item
, TRUE
/* below */);
2365 SetCursor(wxCURSOR_BULLSEYE
);
2370 SetCursor(wxCURSOR_NO_ENTRY
);
2374 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2376 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2378 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2380 wxClientDC
dc(this);
2382 dc
.SetLogicalFunction(wxINVERT
);
2383 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2385 int w
= i
->GetWidth() + 2;
2386 int h
= GetLineHeight(i
) + 2;
2388 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2391 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2393 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2395 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2397 wxClientDC
dc(this);
2399 dc
.SetLogicalFunction(wxINVERT
);
2405 y
+= GetLineHeight(i
) - 1;
2408 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2411 // -----------------------------------------------------------------------------
2412 // wxWindows callbacks
2413 // -----------------------------------------------------------------------------
2415 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2423 dc
.SetFont( m_normalFont
);
2424 dc
.SetPen( m_dottedPen
);
2426 // this is now done dynamically
2427 //if(GetImageList() == NULL)
2428 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2431 PaintLevel( m_anchor
, dc
, 0, y
);
2434 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2443 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2452 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2454 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2455 te
.m_evtKey
= event
;
2456 te
.SetEventObject( this );
2457 if ( GetEventHandler()->ProcessEvent( te
) )
2459 // intercepted by the user code
2463 if ( (m_current
== 0) || (m_key_current
== 0) )
2469 // how should the selection work for this event?
2470 bool is_multiple
, extended_select
, unselect_others
;
2471 EventFlagsToSelType(GetWindowStyleFlag(),
2473 event
.ControlDown(),
2474 is_multiple
, extended_select
, unselect_others
);
2478 // * : Expand all/Collapse all
2479 // ' ' | return : activate
2480 // up : go up (not last children!)
2482 // left : go to parent
2483 // right : open if parent and go next
2484 // home : go to root
2485 // end : go to last item without opening parents
2486 // alnum : start or continue searching for the item with this prefix
2487 int keyCode
= event
.KeyCode();
2492 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2500 if ( !IsExpanded(m_current
) )
2503 ExpandAll(m_current
);
2506 //else: fall through to Collapse() it
2510 if (IsExpanded(m_current
))
2512 Collapse(m_current
);
2519 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2520 event
.m_item
= (long) m_current
;
2521 event
.SetEventObject( this );
2522 GetEventHandler()->ProcessEvent( event
);
2526 // up goes to the previous sibling or to the last
2527 // of its children if it's expanded
2530 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2533 prev
= GetParent( m_key_current
);
2534 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2536 break; // don't go to root if it is hidden
2541 wxTreeItemId current
= m_key_current
;
2542 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2543 if (current
== GetFirstChild( prev
, cookie
))
2545 // otherwise we return to where we came from
2546 SelectItem( prev
, unselect_others
, extended_select
);
2547 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2554 while ( IsExpanded(prev
) && HasChildren(prev
) )
2556 wxTreeItemId child
= GetLastChild(prev
);
2563 SelectItem( prev
, unselect_others
, extended_select
);
2564 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2569 // left arrow goes to the parent
2572 wxTreeItemId prev
= GetParent( m_current
);
2573 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2575 // don't go to root if it is hidden
2576 prev
= GetPrevSibling( m_current
);
2580 SelectItem( prev
, unselect_others
, extended_select
);
2586 // this works the same as the down arrow except that we
2587 // also expand the item if it wasn't expanded yet
2593 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2596 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2597 SelectItem( child
, unselect_others
, extended_select
);
2598 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2602 wxTreeItemId next
= GetNextSibling( m_key_current
);
2605 wxTreeItemId current
= m_key_current
;
2606 while (current
&& !next
)
2608 current
= GetParent( current
);
2609 if (current
) next
= GetNextSibling( current
);
2614 SelectItem( next
, unselect_others
, extended_select
);
2615 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2621 // <End> selects the last visible tree item
2624 wxTreeItemId last
= GetRootItem();
2626 while ( last
.IsOk() && IsExpanded(last
) )
2628 wxTreeItemId lastChild
= GetLastChild(last
);
2630 // it may happen if the item was expanded but then all of
2631 // its children have been deleted - so IsExpanded() returned
2632 // TRUE, but GetLastChild() returned invalid item
2641 SelectItem( last
, unselect_others
, extended_select
);
2646 // <Home> selects the root item
2649 wxTreeItemId prev
= GetRootItem();
2653 if ( HasFlag(wxTR_HIDE_ROOT
) )
2656 prev
= GetFirstChild(prev
, dummy
);
2661 SelectItem( prev
, unselect_others
, extended_select
);
2666 // do not use wxIsalnum() here
2667 if ( !event
.HasModifiers() &&
2668 ((keyCode
>= '0' && keyCode
<= '9') ||
2669 (keyCode
>= 'a' && keyCode
<= 'z') ||
2670 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2672 // find the next item starting with the given prefix
2673 char ch
= (char)keyCode
;
2675 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2686 // also start the timer to reset the current prefix if the user
2687 // doesn't press any more alnum keys soon -- we wouldn't want
2688 // to use this prefix for a new item search
2691 m_findTimer
= new wxTreeFindTimer(this);
2694 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2703 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2705 // JACS: removed wxYieldIfNeeded() because it can cause the window
2706 // to be deleted from under us if a close window event is pending
2711 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2712 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2713 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2714 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2715 if (flags
) return wxTreeItemId();
2717 if (m_anchor
== NULL
)
2719 flags
= wxTREE_HITTEST_NOWHERE
;
2720 return wxTreeItemId();
2723 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2727 flags
= wxTREE_HITTEST_NOWHERE
;
2728 return wxTreeItemId();
2733 // get the bounding rectangle of the item (or of its label only)
2734 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2736 bool WXUNUSED(textOnly
)) const
2738 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2740 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2743 GetViewStart(& startX
, & startY
);
2745 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2746 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2747 rect
.width
= i
->GetWidth();
2748 //rect.height = i->GetHeight();
2749 rect
.height
= GetLineHeight(i
);
2754 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2756 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2758 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2760 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2761 te
.m_item
= (long) itemEdit
;
2762 te
.SetEventObject( this );
2763 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2769 // We have to call this here because the label in
2770 // question might just have been added and no screen
2771 // update taken place.
2775 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, itemEdit
);
2780 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2781 const wxString
& value
)
2783 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2784 le
.m_item
= (long) item
;
2785 le
.SetEventObject( this );
2788 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2791 void wxGenericTreeCtrl::OnRenameTimer()
2796 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2798 if ( !m_anchor
) return;
2800 // we process left mouse up event (enables in-place edit), right down
2801 // (pass to the user code), left dbl click (activate item) and
2802 // dragging/moving events for items drag-and-drop
2803 if ( !(event
.LeftDown() ||
2805 event
.RightDown() ||
2806 event
.LeftDClick() ||
2808 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2815 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2818 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2820 if ( event
.Dragging() && !m_isDragging
)
2822 if (m_dragCount
== 0)
2827 if (m_dragCount
!= 3)
2829 // wait until user drags a bit further...
2833 wxEventType command
= event
.RightIsDown()
2834 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2835 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2837 wxTreeEvent
nevent( command
, GetId() );
2838 nevent
.m_item
= (long) m_current
;
2839 nevent
.SetEventObject(this);
2841 // by default the dragging is not supported, the user code must
2842 // explicitly allow the event for it to take place
2845 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2847 // we're going to drag this item
2848 m_isDragging
= TRUE
;
2850 // remember the old cursor because we will change it while
2852 m_oldCursor
= m_cursor
;
2854 // in a single selection control, hide the selection temporarily
2855 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2857 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2859 if ( m_oldSelection
)
2861 m_oldSelection
->SetHilight(FALSE
);
2862 RefreshLine(m_oldSelection
);
2869 else if ( event
.Moving() )
2871 if ( item
!= m_dropTarget
)
2873 // unhighlight the previous drop target
2874 DrawDropEffect(m_dropTarget
);
2876 m_dropTarget
= item
;
2878 // highlight the current drop target if any
2879 DrawDropEffect(m_dropTarget
);
2884 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2886 // erase the highlighting
2887 DrawDropEffect(m_dropTarget
);
2889 if ( m_oldSelection
)
2891 m_oldSelection
->SetHilight(TRUE
);
2892 RefreshLine(m_oldSelection
);
2893 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2896 // generate the drag end event
2897 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2899 event
.m_item
= (long) item
;
2900 event
.m_pointDrag
= pt
;
2901 event
.SetEventObject(this);
2903 (void)GetEventHandler()->ProcessEvent(event
);
2905 m_isDragging
= FALSE
;
2906 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2910 SetCursor(m_oldCursor
);
2916 // here we process only the messages which happen on tree items
2920 if (item
== NULL
) return; /* we hit the blank area */
2922 if ( event
.RightDown() )
2924 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2925 nevent
.m_item
= (long) item
;
2926 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
2927 nevent
.SetEventObject(this);
2928 GetEventHandler()->ProcessEvent(nevent
);
2930 else if ( event
.LeftUp() )
2934 if ( (item
== m_current
) &&
2935 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2936 HasFlag(wxTR_EDIT_LABELS
) )
2938 if ( m_renameTimer
)
2940 if ( m_renameTimer
->IsRunning() )
2941 m_renameTimer
->Stop();
2945 m_renameTimer
= new wxTreeRenameTimer( this );
2948 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
2951 m_lastOnSame
= FALSE
;
2954 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2956 if ( event
.LeftDown() )
2958 m_lastOnSame
= item
== m_current
;
2961 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2963 // only toggle the item for a single click, double click on
2964 // the button doesn't do anything (it toggles the item twice)
2965 if ( event
.LeftDown() )
2970 // don't select the item if the button was clicked
2974 // how should the selection work for this event?
2975 bool is_multiple
, extended_select
, unselect_others
;
2976 EventFlagsToSelType(GetWindowStyleFlag(),
2978 event
.ControlDown(),
2979 is_multiple
, extended_select
, unselect_others
);
2981 SelectItem(item
, unselect_others
, extended_select
);
2983 // For some reason, Windows isn't recognizing a left double-click,
2984 // so we need to simulate it here. Allow 200 milliseconds for now.
2985 if ( event
.LeftDClick() )
2987 // double clicking should not start editing the item label
2988 if ( m_renameTimer
)
2989 m_renameTimer
->Stop();
2991 m_lastOnSame
= FALSE
;
2993 // send activate event first
2994 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2995 nevent
.m_item
= (long) item
;
2996 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
2997 nevent
.SetEventObject( this );
2998 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3000 // if the user code didn't process the activate event,
3001 // handle it ourselves by toggling the item when it is
3003 if ( item
->HasPlus() )
3013 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
3015 /* after all changes have been done to the tree control,
3016 * we actually redraw the tree when everything is over */
3018 if (!m_dirty
) return;
3022 CalculatePositions();
3024 AdjustMyScrollbars();
3027 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3032 wxTreeItemAttr
*attr
= item
->GetAttributes();
3033 if ( attr
&& attr
->HasFont() )
3034 dc
.SetFont(attr
->GetFont());
3035 else if ( item
->IsBold() )
3036 dc
.SetFont(m_boldFont
);
3038 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3041 // restore normal font
3042 dc
.SetFont( m_normalFont
);
3046 int image
= item
->GetCurrentImage();
3047 if ( image
!= NO_IMAGE
)
3049 if ( m_imageListNormal
)
3051 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3056 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3059 total_h
+= 2; // at least 2 pixels
3061 total_h
+= total_h
/10; // otherwise 10% extra spacing
3063 item
->SetHeight(total_h
);
3064 if (total_h
>m_lineHeight
)
3065 m_lineHeight
=total_h
;
3067 item
->SetWidth(image_w
+text_w
+2);
3070 // -----------------------------------------------------------------------------
3071 // for developper : y is now the top of the level
3072 // not the middle of it !
3073 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3075 int x
= level
*m_indent
;
3076 if (!HasFlag(wxTR_HIDE_ROOT
))
3080 else if (level
== 0)
3082 // a hidden root is not evaluated, but its
3083 // children are always calculated
3087 CalculateSize( item
, dc
);
3090 item
->SetX( x
+m_spacing
);
3092 y
+= GetLineHeight(item
);
3094 if ( !item
->IsExpanded() )
3096 // we don't need to calculate collapsed branches
3101 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3102 size_t n
, count
= children
.Count();
3104 for (n
= 0; n
< count
; ++n
)
3105 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3108 void wxGenericTreeCtrl::CalculatePositions()
3110 if ( !m_anchor
) return;
3112 wxClientDC
dc(this);
3115 dc
.SetFont( m_normalFont
);
3117 dc
.SetPen( m_dottedPen
);
3118 //if(GetImageList() == NULL)
3119 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3122 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3125 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3127 if (m_dirty
) return;
3129 wxSize client
= GetClientSize();
3132 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3133 rect
.width
= client
.x
;
3134 rect
.height
= client
.y
;
3136 Refresh(TRUE
, &rect
);
3138 AdjustMyScrollbars();
3141 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3143 if (m_dirty
) return;
3146 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3147 rect
.width
= GetClientSize().x
;
3148 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3150 Refresh(TRUE
, &rect
);
3153 void wxGenericTreeCtrl::RefreshSelected()
3155 // TODO: this is awfully inefficient, we should keep the list of all
3156 // selected items internally, should be much faster
3158 RefreshSelectedUnder(m_anchor
);
3161 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3163 if ( item
->IsSelected() )
3166 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3167 size_t count
= children
.GetCount();
3168 for ( size_t n
= 0; n
< count
; n
++ )
3170 RefreshSelectedUnder(children
[n
]);
3174 // ----------------------------------------------------------------------------
3175 // changing colours: we need to refresh the tree control
3176 // ----------------------------------------------------------------------------
3178 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3180 if ( !wxWindow::SetBackgroundColour(colour
) )
3188 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3190 if ( !wxWindow::SetForegroundColour(colour
) )
3198 #endif // wxUSE_TREECTRL