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 // -----------------------------------------------------------------------------
326 // wxTreeRenameTimer (internal)
327 // -----------------------------------------------------------------------------
329 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
334 void wxTreeRenameTimer::Notify()
336 m_owner
->OnRenameTimer();
339 //-----------------------------------------------------------------------------
340 // wxTreeTextCtrl (internal)
341 //-----------------------------------------------------------------------------
343 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
344 EVT_CHAR (wxTreeTextCtrl::OnChar
)
345 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
346 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
349 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
350 wxGenericTreeItem
*item
)
351 : m_itemEdited(item
), m_startValue(item
->GetText())
356 int w
= m_itemEdited
->GetWidth(),
357 h
= m_itemEdited
->GetHeight();
360 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
365 int image
= item
->GetCurrentImage();
366 if ( image
!= NO_IMAGE
)
368 if ( m_owner
->m_imageListNormal
)
370 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
375 wxFAIL_MSG(_T("you must create an image list to use images!"));
379 // FIXME: what are all these hardcoded 4, 8 and 11s really?
383 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
384 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
387 bool wxTreeTextCtrl::AcceptChanges()
389 const wxString value
= GetValue();
391 if ( value
== m_startValue
)
393 // nothing changed, always accept
397 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
399 // vetoed by the user
403 // accepted, do rename the item
404 m_owner
->SetItemText(m_itemEdited
, value
);
409 void wxTreeTextCtrl::Finish()
413 wxPendingDelete
.Append(this);
417 m_owner
->SetFocus(); // This doesn't work. TODO.
421 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
423 switch ( event
.m_keyCode
)
426 if ( !AcceptChanges() )
428 // vetoed by the user, don't disappear
442 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
446 // auto-grow the textctrl:
447 wxSize parentSize
= m_owner
->GetSize();
448 wxPoint myPos
= GetPosition();
449 wxSize mySize
= GetSize();
451 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
452 if (myPos
.x
+ sx
> parentSize
.x
)
453 sx
= parentSize
.x
- myPos
.x
;
462 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
470 if ( AcceptChanges() )
476 // -----------------------------------------------------------------------------
478 // -----------------------------------------------------------------------------
480 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
481 const wxString
& text
,
482 int image
, int selImage
,
483 wxTreeItemData
*data
)
486 m_images
[wxTreeItemIcon_Normal
] = image
;
487 m_images
[wxTreeItemIcon_Selected
] = selImage
;
488 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
489 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
494 m_isCollapsed
= TRUE
;
495 m_hasHilight
= FALSE
;
501 m_attr
= (wxTreeItemAttr
*)NULL
;
504 // We don't know the height here yet.
509 wxGenericTreeItem::~wxGenericTreeItem()
513 if (m_ownsAttr
) delete m_attr
;
515 wxASSERT_MSG( m_children
.IsEmpty(),
516 wxT("please call DeleteChildren() before deleting the item") );
519 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
521 size_t count
= m_children
.Count();
522 for ( size_t n
= 0; n
< count
; n
++ )
524 wxGenericTreeItem
*child
= m_children
[n
];
526 tree
->SendDeleteEvent(child
);
528 child
->DeleteChildren(tree
);
535 void wxGenericTreeItem::SetText( const wxString
&text
)
540 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
542 size_t count
= m_children
.Count();
546 size_t total
= count
;
547 for (size_t n
= 0; n
< count
; ++n
)
549 total
+= m_children
[n
]->GetChildrenCount();
555 void wxGenericTreeItem::GetSize( int &x
, int &y
,
556 const wxGenericTreeCtrl
*theButton
)
558 int bottomY
=m_y
+theButton
->GetLineHeight(this);
559 if ( y
< bottomY
) y
= bottomY
;
560 int width
= m_x
+ m_width
;
561 if ( x
< width
) x
= width
;
565 size_t count
= m_children
.Count();
566 for ( size_t n
= 0; n
< count
; ++n
)
568 m_children
[n
]->GetSize( x
, y
, theButton
);
573 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
574 const wxGenericTreeCtrl
*theCtrl
,
578 // for a hidden root node, don't evaluate it, but do evaluate children
579 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
582 int h
= theCtrl
->GetLineHeight(this);
583 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
585 int y_mid
= m_y
+ h
/2;
586 if (point
.y
< y_mid
)
587 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
589 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
591 // 5 is the size of the plus sign
592 int xCross
= m_x
- theCtrl
->GetSpacing();
593 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
594 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
595 HasPlus() && theCtrl
->HasButtons() )
597 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
601 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
606 // assuming every image (normal and selected) has the same size!
607 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
608 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
611 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
612 flags
|= wxTREE_HITTEST_ONITEMICON
;
614 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
620 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
621 if (point
.x
> m_x
+m_width
)
622 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
627 // if children are expanded, fall through to evaluate them
628 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
632 size_t count
= m_children
.Count();
633 for ( size_t n
= 0; n
< count
; n
++ )
635 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
643 return (wxGenericTreeItem
*) NULL
;
646 int wxGenericTreeItem::GetCurrentImage() const
648 int image
= NO_IMAGE
;
653 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
656 if ( image
== NO_IMAGE
)
658 // we usually fall back to the normal item, but try just the
659 // expanded one (and not selected) first in this case
660 image
= GetImage(wxTreeItemIcon_Expanded
);
666 image
= GetImage(wxTreeItemIcon_Selected
);
669 // maybe it doesn't have the specific image we want,
670 // try the default one instead
671 if ( image
== NO_IMAGE
) image
= GetImage();
676 // -----------------------------------------------------------------------------
677 // wxGenericTreeCtrl implementation
678 // -----------------------------------------------------------------------------
680 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
682 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
683 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
684 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
685 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
686 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
687 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
688 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
691 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
693 * wxTreeCtrl has to be a real class or we have problems with
694 * the run-time information.
697 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
700 // -----------------------------------------------------------------------------
701 // construction/destruction
702 // -----------------------------------------------------------------------------
704 void wxGenericTreeCtrl::Init()
706 m_current
= m_key_current
= m_anchor
= (wxGenericTreeItem
*) NULL
;
714 m_hilightBrush
= new wxBrush
716 wxSystemSettings::GetColour
718 wxSYS_COLOUR_HIGHLIGHT
723 m_hilightUnfocusedBrush
= new wxBrush
725 wxSystemSettings::GetColour
727 wxSYS_COLOUR_BTNSHADOW
732 m_imageListNormal
= m_imageListButtons
=
733 m_imageListState
= (wxImageList
*) NULL
;
734 m_ownsImageListNormal
= m_ownsImageListButtons
=
735 m_ownsImageListState
= FALSE
;
738 m_isDragging
= FALSE
;
739 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
741 m_renameTimer
= NULL
;
744 m_lastOnSame
= FALSE
;
746 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
747 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
748 m_normalFont
.GetFamily(),
749 m_normalFont
.GetStyle(),
751 m_normalFont
.GetUnderlined());
754 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
759 const wxValidator
&validator
,
760 const wxString
& name
)
764 wxGetOsVersion( &major
, &minor
);
766 if (style
& wxTR_HAS_BUTTONS
) style
|= wxTR_MAC_BUTTONS
;
767 if (style
& wxTR_HAS_BUTTONS
) style
&= ~wxTR_HAS_BUTTONS
;
768 style
&= ~wxTR_LINES_AT_ROOT
;
769 style
|= wxTR_NO_LINES
;
771 style
|= wxTR_ROW_LINES
;
773 style
|= wxTR_AQUA_BUTTONS
;
776 if (style
& wxTR_AQUA_BUTTONS
)
778 m_arrowRight
= new wxBitmap( aqua_arrow_right
);
779 m_arrowDown
= new wxBitmap( aqua_arrow_down
);
787 wxScrolledWindow::Create( parent
, id
, pos
, size
,
788 style
|wxHSCROLL
|wxVSCROLL
, name
);
790 // If the tree display has no buttons, but does have
791 // connecting lines, we can use a narrower layout.
792 // It may not be a good idea to force this...
793 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
800 SetValidator( validator
);
803 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
804 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
806 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
807 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
812 wxGenericTreeCtrl::~wxGenericTreeCtrl()
814 delete m_hilightBrush
;
815 delete m_hilightUnfocusedBrush
;
822 delete m_renameTimer
;
825 if (m_ownsImageListNormal
)
826 delete m_imageListNormal
;
827 if (m_ownsImageListState
)
828 delete m_imageListState
;
829 if (m_ownsImageListButtons
)
830 delete m_imageListButtons
;
833 // -----------------------------------------------------------------------------
835 // -----------------------------------------------------------------------------
837 size_t wxGenericTreeCtrl::GetCount() const
839 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
842 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
844 m_indent
= (unsigned short) indent
;
848 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
850 m_spacing
= (unsigned short) spacing
;
854 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
856 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
858 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
861 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
863 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
865 // if we will hide the root, make sure children are visible
866 m_anchor
->SetHasPlus();
868 CalculatePositions();
871 // right now, just sets the styles. Eventually, we may
872 // want to update the inherited styles, but right now
873 // none of the parents has updatable styles
874 m_windowStyle
= styles
;
878 // -----------------------------------------------------------------------------
879 // functions to work with tree items
880 // -----------------------------------------------------------------------------
882 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
884 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
886 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
889 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
890 wxTreeItemIcon which
) const
892 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
894 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
897 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
899 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
901 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
904 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
906 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
909 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
910 pItem
->SetText(text
);
911 CalculateSize(pItem
, dc
);
915 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
917 wxTreeItemIcon which
)
919 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
921 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
922 pItem
->SetImage(image
, which
);
925 CalculateSize(pItem
, dc
);
929 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
931 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
933 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
936 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
938 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
940 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
941 pItem
->SetHasPlus(has
);
945 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
947 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
949 // avoid redrawing the tree if no real change
950 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
951 if ( pItem
->IsBold() != bold
)
953 pItem
->SetBold(bold
);
958 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
961 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
963 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
964 pItem
->Attr().SetTextColour(col
);
968 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
971 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
973 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
974 pItem
->Attr().SetBackgroundColour(col
);
978 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
980 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
982 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
983 pItem
->Attr().SetFont(font
);
987 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
989 wxScrolledWindow::SetFont(font
);
991 m_normalFont
= font
;
992 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
993 m_normalFont
.GetFamily(),
994 m_normalFont
.GetStyle(),
996 m_normalFont
.GetUnderlined());
1002 // -----------------------------------------------------------------------------
1003 // item status inquiries
1004 // -----------------------------------------------------------------------------
1006 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1008 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1010 // An item is only visible if it's not a descendant of a collapsed item
1011 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1012 wxGenericTreeItem
* parent
= pItem
->GetParent();
1015 if (!parent
->IsExpanded())
1017 parent
= parent
->GetParent();
1021 GetViewStart(& startX
, & startY
);
1023 wxSize clientSize
= GetClientSize();
1026 if (!GetBoundingRect(item
, rect
))
1028 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1030 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1032 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1038 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1040 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1042 // consider that the item does have children if it has the "+" button: it
1043 // might not have them (if it had never been expanded yet) but then it
1044 // could have them as well and it's better to err on this side rather than
1045 // disabling some operations which are restricted to the items with
1046 // children for an item which does have them
1047 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1050 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1052 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1054 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1057 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1059 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1061 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1064 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1066 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1068 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1071 // -----------------------------------------------------------------------------
1073 // -----------------------------------------------------------------------------
1075 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1077 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1079 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1082 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
1084 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1087 return GetNextChild(item
, cookie
);
1090 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
1092 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1094 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1095 if ( (size_t)cookie
< children
.Count() )
1097 return children
.Item((size_t)cookie
++);
1101 // there are no more of them
1102 return wxTreeItemId();
1106 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1108 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1110 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1111 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1114 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1116 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1118 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1119 wxGenericTreeItem
*parent
= i
->GetParent();
1120 if ( parent
== NULL
)
1122 // root item doesn't have any siblings
1123 return wxTreeItemId();
1126 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1127 int index
= siblings
.Index(i
);
1128 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1130 size_t n
= (size_t)(index
+ 1);
1131 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1134 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1136 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1138 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1139 wxGenericTreeItem
*parent
= i
->GetParent();
1140 if ( parent
== NULL
)
1142 // root item doesn't have any siblings
1143 return wxTreeItemId();
1146 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1147 int index
= siblings
.Index(i
);
1148 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1150 return index
== 0 ? wxTreeItemId()
1151 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1154 // Only for internal use right now, but should probably be public
1155 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1157 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1159 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1161 // First see if there are any children.
1162 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1163 if (children
.GetCount() > 0)
1165 return children
.Item(0);
1169 // Try a sibling of this or ancestor instead
1170 wxTreeItemId p
= item
;
1171 wxTreeItemId toFind
;
1174 toFind
= GetNextSibling(p
);
1176 } while (p
.IsOk() && !toFind
.IsOk());
1181 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1183 wxTreeItemId id
= GetRootItem();
1192 } while (id
.IsOk());
1194 return wxTreeItemId();
1197 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1199 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1201 wxTreeItemId id
= item
;
1204 while (id
= GetNext(id
), id
.IsOk())
1210 return wxTreeItemId();
1213 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1215 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1217 wxFAIL_MSG(wxT("not implemented"));
1219 return wxTreeItemId();
1222 // find the first item starting with the given prefix after the given item
1223 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1224 const wxString
& prefixOrig
) const
1226 // match is case insensitive as this is more convenient to the user: having
1227 // to press Shift-letter to go to the item starting with a capital letter
1228 // would be too bothersome
1229 wxString prefix
= prefixOrig
.Lower();
1231 // determine the starting point: we shouldn't take the current item (this
1232 // allows to switch between two items starting with the same letter just by
1233 // pressing it) but we shouldn't jump to the next one if the user is
1234 // continuing to type as otherwise he might easily skip the item he wanted
1235 wxTreeItemId id
= idParent
;
1236 if ( prefix
.length() == 1 )
1241 // look for the item starting with the given prefix after it
1242 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1247 // if we haven't found anything...
1250 // ... wrap to the beginning
1252 if ( HasFlag(wxTR_HIDE_ROOT
) )
1254 // can't select virtual root
1258 // and try all the items (stop when we get to the one we started from)
1259 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1268 // -----------------------------------------------------------------------------
1270 // -----------------------------------------------------------------------------
1272 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1274 const wxString
& text
,
1275 int image
, int selImage
,
1276 wxTreeItemData
*data
)
1278 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1281 // should we give a warning here?
1282 return AddRoot(text
, image
, selImage
, data
);
1285 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1287 wxGenericTreeItem
*item
=
1288 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1292 data
->m_pItem
= (long) item
;
1295 parent
->Insert( item
, previous
);
1300 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1301 int image
, int selImage
,
1302 wxTreeItemData
*data
)
1304 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1306 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1308 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1309 image
, selImage
, data
);
1312 data
->m_pItem
= (long) m_anchor
;
1315 if (HasFlag(wxTR_HIDE_ROOT
))
1317 // if root is hidden, make sure we can navigate
1319 m_anchor
->SetHasPlus();
1321 CalculatePositions();
1324 if (!HasFlag(wxTR_MULTIPLE
))
1326 m_current
= m_key_current
= m_anchor
;
1327 m_current
->SetHilight( TRUE
);
1333 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1334 const wxString
& text
,
1335 int image
, int selImage
,
1336 wxTreeItemData
*data
)
1338 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1341 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1342 const wxTreeItemId
& idPrevious
,
1343 const wxString
& text
,
1344 int image
, int selImage
,
1345 wxTreeItemData
*data
)
1347 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1350 // should we give a warning here?
1351 return AddRoot(text
, image
, selImage
, data
);
1355 if (idPrevious
.IsOk())
1357 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1358 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1359 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1362 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1365 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1367 const wxString
& text
,
1368 int image
, int selImage
,
1369 wxTreeItemData
*data
)
1371 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1374 // should we give a warning here?
1375 return AddRoot(text
, image
, selImage
, data
);
1378 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1381 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1382 const wxString
& text
,
1383 int image
, int selImage
,
1384 wxTreeItemData
*data
)
1386 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1389 // should we give a warning here?
1390 return AddRoot(text
, image
, selImage
, data
);
1393 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1394 image
, selImage
, data
);
1397 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1399 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1400 event
.m_item
= (long) item
;
1401 event
.SetEventObject( this );
1402 ProcessEvent( event
);
1405 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1407 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1409 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1410 item
->DeleteChildren(this);
1413 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1415 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1417 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1419 // don't stay with invalid m_key_current or we will crash in
1420 // the next call to OnChar()
1421 bool changeKeyCurrent
= FALSE
;
1422 wxGenericTreeItem
*itemKey
= m_key_current
;
1425 if ( itemKey
== item
)
1427 // m_key_current is a descendant of the item being deleted
1428 changeKeyCurrent
= TRUE
;
1431 itemKey
= itemKey
->GetParent();
1434 wxGenericTreeItem
*parent
= item
->GetParent();
1437 parent
->GetChildren().Remove( item
); // remove by value
1440 if ( changeKeyCurrent
)
1442 // may be NULL or not
1443 m_key_current
= parent
;
1446 item
->DeleteChildren(this);
1447 SendDeleteEvent(item
);
1451 void wxGenericTreeCtrl::DeleteAllItems()
1457 m_anchor
->DeleteChildren(this);
1464 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1466 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1468 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1469 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1470 _T("can't expand hidden root") );
1472 if ( !item
->HasPlus() )
1475 if ( item
->IsExpanded() )
1478 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1479 event
.m_item
= (long) item
;
1480 event
.SetEventObject( this );
1482 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1484 // cancelled by program
1489 CalculatePositions();
1491 RefreshSubtree(item
);
1493 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1494 ProcessEvent( event
);
1497 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1500 if ( IsExpanded(item
) )
1503 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1504 while ( child
.IsOk() )
1508 child
= GetNextChild(item
, cookie
);
1513 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1515 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1516 _T("can't collapse hidden root") );
1518 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1520 if ( !item
->IsExpanded() )
1523 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1524 event
.m_item
= (long) item
;
1525 event
.SetEventObject( this );
1526 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1528 // cancelled by program
1534 #if 0 // TODO why should items be collapsed recursively?
1535 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1536 size_t count
= children
.Count();
1537 for ( size_t n
= 0; n
< count
; n
++ )
1539 Collapse(children
[n
]);
1543 CalculatePositions();
1545 RefreshSubtree(item
);
1547 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1548 ProcessEvent( event
);
1551 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1554 DeleteChildren(item
);
1557 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1559 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1561 if (item
->IsExpanded())
1567 void wxGenericTreeCtrl::Unselect()
1571 m_current
->SetHilight( FALSE
);
1572 RefreshLine( m_current
);
1578 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1580 if (item
->IsSelected())
1582 item
->SetHilight(FALSE
);
1586 if (item
->HasChildren())
1588 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1589 size_t count
= children
.Count();
1590 for ( size_t n
= 0; n
< count
; ++n
)
1592 UnselectAllChildren(children
[n
]);
1597 void wxGenericTreeCtrl::UnselectAll()
1599 wxTreeItemId rootItem
= GetRootItem();
1601 // the tree might not have the root item at all
1604 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1608 // Recursive function !
1609 // To stop we must have crt_item<last_item
1611 // Tag all next children, when no more children,
1612 // Move to parent (not to tag)
1613 // Keep going... if we found last_item, we stop.
1614 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1616 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1618 if (parent
== NULL
) // This is root item
1619 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1621 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1622 int index
= children
.Index(crt_item
);
1623 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1625 size_t count
= children
.Count();
1626 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1628 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1631 return TagNextChildren(parent
, last_item
, select
);
1634 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1636 crt_item
->SetHilight(select
);
1637 RefreshLine(crt_item
);
1639 if (crt_item
==last_item
)
1642 if (crt_item
->HasChildren())
1644 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1645 size_t count
= children
.Count();
1646 for ( size_t n
= 0; n
< count
; ++n
)
1648 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1656 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1658 // item2 is not necessary after item1
1659 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1661 // choice first' and 'last' between item1 and item2
1662 if (item1
->GetY()<item2
->GetY())
1673 bool select
= m_current
->IsSelected();
1675 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1678 TagNextChildren(first
,last
,select
);
1681 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1682 bool unselect_others
,
1683 bool extended_select
)
1685 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1687 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1688 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1690 //wxCHECK_RET( ( (!unselect_others) && is_single),
1691 // wxT("this is a single selection tree") );
1693 // to keep going anyhow !!!
1696 if (item
->IsSelected())
1697 return; // nothing to do
1698 unselect_others
= TRUE
;
1699 extended_select
= FALSE
;
1701 else if ( unselect_others
&& item
->IsSelected() )
1703 // selection change if there is more than one item currently selected
1704 wxArrayTreeItemIds selected_items
;
1705 if ( GetSelections(selected_items
) == 1 )
1709 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1710 event
.m_item
= (long) item
;
1711 event
.m_itemOld
= (long) m_current
;
1712 event
.SetEventObject( this );
1713 // TODO : Here we don't send any selection mode yet !
1715 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1718 wxTreeItemId parent
= GetParent( itemId
);
1719 while (parent
.IsOk())
1721 if (!IsExpanded(parent
))
1724 parent
= GetParent( parent
);
1727 EnsureVisible( itemId
);
1730 if (unselect_others
)
1732 if (is_single
) Unselect(); // to speed up thing
1737 if (extended_select
)
1741 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1744 // don't change the mark (m_current)
1745 SelectItemRange(m_current
, item
);
1749 bool select
=TRUE
; // the default
1751 // Check if we need to toggle hilight (ctrl mode)
1752 if (!unselect_others
)
1753 select
=!item
->IsSelected();
1755 m_current
= m_key_current
= item
;
1756 m_current
->SetHilight(select
);
1757 RefreshLine( m_current
);
1760 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1761 GetEventHandler()->ProcessEvent( event
);
1764 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1765 wxArrayTreeItemIds
&array
) const
1767 if ( item
->IsSelected() )
1768 array
.Add(wxTreeItemId(item
));
1770 if ( item
->HasChildren() )
1772 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1773 size_t count
= children
.GetCount();
1774 for ( size_t n
= 0; n
< count
; ++n
)
1775 FillArray(children
[n
], array
);
1779 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1782 wxTreeItemId idRoot
= GetRootItem();
1783 if ( idRoot
.IsOk() )
1785 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1787 //else: the tree is empty, so no selections
1789 return array
.Count();
1792 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1794 if (!item
.IsOk()) return;
1796 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1798 // first expand all parent branches
1799 wxGenericTreeItem
*parent
= gitem
->GetParent();
1801 if ( HasFlag(wxTR_HIDE_ROOT
) )
1803 while ( parent
!= m_anchor
)
1806 parent
= parent
->GetParent();
1814 parent
= parent
->GetParent();
1818 //if (parent) CalculatePositions();
1823 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1825 if (!item
.IsOk()) return;
1827 // We have to call this here because the label in
1828 // question might just have been added and no screen
1829 // update taken place.
1830 if (m_dirty
) wxYieldIfNeeded();
1832 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1834 // now scroll to the item
1835 int item_y
= gitem
->GetY();
1839 GetViewStart( &start_x
, &start_y
);
1840 start_y
*= PIXELS_PER_UNIT
;
1844 GetClientSize( &client_w
, &client_h
);
1846 if (item_y
< start_y
+3)
1851 m_anchor
->GetSize( x
, y
, this );
1852 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1853 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1854 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1855 // Item should appear at top
1856 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1858 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1863 m_anchor
->GetSize( x
, y
, this );
1864 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1865 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1866 item_y
+= PIXELS_PER_UNIT
+2;
1867 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1868 // Item should appear at bottom
1869 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
);
1873 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1874 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1876 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1877 wxGenericTreeItem
**item2
)
1879 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1881 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1884 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1885 const wxTreeItemId
& item2
)
1887 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1890 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1892 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1894 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1896 wxCHECK_RET( !s_treeBeingSorted
,
1897 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1899 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1900 if ( children
.Count() > 1 )
1904 s_treeBeingSorted
= this;
1905 children
.Sort(tree_ctrl_compare_func
);
1906 s_treeBeingSorted
= NULL
;
1908 //else: don't make the tree dirty as nothing changed
1911 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1913 return m_imageListNormal
;
1916 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1918 return m_imageListButtons
;
1921 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1923 return m_imageListState
;
1926 void wxGenericTreeCtrl::CalculateLineHeight()
1928 wxClientDC
dc(this);
1929 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1931 if ( m_imageListNormal
)
1933 // Calculate a m_lineHeight value from the normal Image sizes.
1934 // May be toggle off. Then wxGenericTreeCtrl will spread when
1935 // necessary (which might look ugly).
1936 int n
= m_imageListNormal
->GetImageCount();
1937 for (int i
= 0; i
< n
; i
++)
1939 int width
= 0, height
= 0;
1940 m_imageListNormal
->GetSize(i
, width
, height
);
1941 if (height
> m_lineHeight
) m_lineHeight
= height
;
1945 if (m_imageListButtons
)
1947 // Calculate a m_lineHeight value from the Button image sizes.
1948 // May be toggle off. Then wxGenericTreeCtrl will spread when
1949 // necessary (which might look ugly).
1950 int n
= m_imageListButtons
->GetImageCount();
1951 for (int i
= 0; i
< n
; i
++)
1953 int width
= 0, height
= 0;
1954 m_imageListButtons
->GetSize(i
, width
, height
);
1955 if (height
> m_lineHeight
) m_lineHeight
= height
;
1959 if (m_lineHeight
< 30)
1960 m_lineHeight
+= 2; // at least 2 pixels
1962 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1965 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1967 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1968 m_imageListNormal
= imageList
;
1969 m_ownsImageListNormal
= FALSE
;
1971 // Don't do any drawing if we're setting the list to NULL,
1972 // since we may be in the process of deleting the tree control.
1974 CalculateLineHeight();
1977 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1979 if (m_ownsImageListState
) delete m_imageListState
;
1980 m_imageListState
= imageList
;
1981 m_ownsImageListState
= FALSE
;
1984 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1986 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1987 m_imageListButtons
= imageList
;
1988 m_ownsImageListButtons
= FALSE
;
1990 CalculateLineHeight();
1993 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1995 SetImageList(imageList
);
1996 m_ownsImageListNormal
= TRUE
;
1999 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2001 SetStateImageList(imageList
);
2002 m_ownsImageListState
= TRUE
;
2005 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2007 SetButtonsImageList(imageList
);
2008 m_ownsImageListButtons
= TRUE
;
2011 // -----------------------------------------------------------------------------
2013 // -----------------------------------------------------------------------------
2015 void wxGenericTreeCtrl::AdjustMyScrollbars()
2020 m_anchor
->GetSize( x
, y
, this );
2021 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2022 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2023 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2024 int y_pos
= GetScrollPos( wxVERTICAL
);
2025 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2029 SetScrollbars( 0, 0, 0, 0 );
2033 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2035 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2036 return item
->GetHeight();
2038 return m_lineHeight
;
2041 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2043 // TODO implement "state" icon on items
2045 wxTreeItemAttr
*attr
= item
->GetAttributes();
2046 if ( attr
&& attr
->HasFont() )
2047 dc
.SetFont(attr
->GetFont());
2048 else if (item
->IsBold())
2049 dc
.SetFont(m_boldFont
);
2051 long text_w
= 0, text_h
= 0;
2052 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2054 int image_h
= 0, image_w
= 0;
2055 int image
= item
->GetCurrentImage();
2056 if ( image
!= NO_IMAGE
)
2058 if ( m_imageListNormal
)
2060 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2069 int total_h
= GetLineHeight(item
);
2071 if ( item
->IsSelected() )
2073 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2078 if ( attr
&& attr
->HasBackgroundColour() )
2079 colBg
= attr
->GetBackgroundColour();
2081 colBg
= m_backgroundColour
;
2082 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2085 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2087 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2091 DoGetPosition(&x
, &y
);
2093 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2097 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2099 // If it's selected, and there's an image, then we should
2100 // take care to leave the area under the image painted in the
2101 // background colour.
2102 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2103 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2107 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2108 item
->GetWidth()+2, total_h
-offset
);
2112 if ( image
!= NO_IMAGE
)
2114 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2115 m_imageListNormal
->Draw( image
, dc
,
2117 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2118 wxIMAGELIST_DRAW_TRANSPARENT
);
2119 dc
.DestroyClippingRegion();
2122 dc
.SetBackgroundMode(wxTRANSPARENT
);
2123 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2124 dc
.DrawText( item
->GetText(),
2125 (wxCoord
)(image_w
+ item
->GetX()),
2126 (wxCoord
)(item
->GetY() + extraH
));
2128 // restore normal font
2129 dc
.SetFont( m_normalFont
);
2132 // Now y stands for the top of the item, whereas it used to stand for middle !
2133 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2135 int x
= level
*m_indent
;
2136 if (!HasFlag(wxTR_HIDE_ROOT
))
2140 else if (level
== 0)
2142 // always expand hidden root
2144 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2145 int count
= children
.Count();
2151 PaintLevel(children
[n
], dc
, 1, y
);
2152 } while (++n
< count
);
2154 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2156 // draw line down to last child
2157 origY
+= GetLineHeight(children
[0])>>1;
2158 oldY
+= GetLineHeight(children
[n
-1])>>1;
2159 dc
.DrawLine(3, origY
, 3, oldY
);
2165 item
->SetX(x
+m_spacing
);
2168 int h
= GetLineHeight(item
);
2170 int y_mid
= y_top
+ (h
>>1);
2173 int exposed_x
= dc
.LogicalToDeviceX(0);
2174 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2176 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2180 // don't draw rect outline if we already have the
2181 // background color under Mac
2182 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2183 #endif // !__WXMAC__
2187 if ( item
->IsSelected() )
2189 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2193 wxTreeItemAttr
*attr
= item
->GetAttributes();
2194 if (attr
&& attr
->HasTextColour())
2195 colText
= attr
->GetTextColour();
2197 colText
= GetForegroundColour();
2201 dc
.SetTextForeground(colText
);
2205 PaintItem(item
, dc
);
2207 if (HasFlag(wxTR_ROW_LINES
))
2209 // if the background colour is white, choose a
2210 // contrasting color for the lines
2211 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2212 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2213 dc
.DrawLine(0, y_top
, 10000, y_top
);
2214 dc
.DrawLine(0, y
, 10000, y
);
2217 // restore DC objects
2218 dc
.SetBrush(*wxWHITE_BRUSH
);
2219 dc
.SetPen(m_dottedPen
);
2220 dc
.SetTextForeground(*wxBLACK
);
2222 if (item
->HasPlus() && HasButtons()) // should the item show a button?
2224 if (!HasFlag(wxTR_NO_LINES
))
2226 if (x
> (signed)m_indent
)
2227 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
2228 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2229 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
2230 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
2233 if (m_imageListButtons
!= NULL
)
2235 // draw the image button here
2236 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
2237 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
2238 if (item
->IsSelected())
2239 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2240 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2241 int xx
= x
- (image_w
>>1);
2242 int yy
= y_mid
- (image_h
>>1);
2243 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
2244 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2245 wxIMAGELIST_DRAW_TRANSPARENT
);
2246 dc
.DestroyClippingRegion();
2248 else if (HasFlag(wxTR_TWIST_BUTTONS
))
2250 // draw the twisty button here
2252 if (HasFlag(wxTR_AQUA_BUTTONS
))
2254 if (item
->IsExpanded())
2255 dc
.DrawBitmap( *m_arrowDown
, x
-5, y_mid
-6, TRUE
);
2257 dc
.DrawBitmap( *m_arrowRight
, x
-5, y_mid
-6, TRUE
);
2261 dc
.SetBrush(*m_hilightBrush
);
2262 dc
.SetPen(*wxBLACK_PEN
);
2265 if (item
->IsExpanded())
2268 button
[0].y
= y_mid
-2;
2270 button
[1].y
= y_mid
-2;
2272 button
[2].y
= y_mid
+3;
2276 button
[0].y
= y_mid
-5;
2278 button
[1].y
= y_mid
+5;
2280 button
[2].y
= y_mid
;
2283 dc
.DrawPolygon(3, button
);
2284 dc
.SetPen(m_dottedPen
);
2287 else // if (HasFlag(wxTR_HAS_BUTTONS))
2289 // draw the plus sign here
2290 dc
.SetPen(*wxGREY_PEN
);
2291 dc
.SetBrush(*wxWHITE_BRUSH
);
2292 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2293 dc
.SetPen(*wxBLACK_PEN
);
2294 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2295 if (!item
->IsExpanded())
2296 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2297 dc
.SetPen(m_dottedPen
);
2300 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2302 // draw the horizontal line here
2304 if (x
> (signed)m_indent
)
2305 x_start
-= m_indent
;
2306 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2308 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2312 if (item
->IsExpanded())
2314 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2315 int count
= children
.Count();
2322 PaintLevel(children
[n
], dc
, level
, y
);
2323 } while (++n
< count
);
2325 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2327 // draw line down to last child
2328 oldY
+= GetLineHeight(children
[n
-1])>>1;
2329 if (HasButtons()) y_mid
+= 5;
2330 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2336 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2340 if ( item
->HasPlus() )
2342 // it's a folder, indicate it by a border
2347 // draw a line under the drop target because the item will be
2349 DrawLine(item
, TRUE
/* below */);
2352 SetCursor(wxCURSOR_BULLSEYE
);
2357 SetCursor(wxCURSOR_NO_ENTRY
);
2361 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2363 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2365 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2367 wxClientDC
dc(this);
2369 dc
.SetLogicalFunction(wxINVERT
);
2370 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2372 int w
= i
->GetWidth() + 2;
2373 int h
= GetLineHeight(i
) + 2;
2375 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2378 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2380 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2382 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2384 wxClientDC
dc(this);
2386 dc
.SetLogicalFunction(wxINVERT
);
2392 y
+= GetLineHeight(i
) - 1;
2395 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2398 // -----------------------------------------------------------------------------
2399 // wxWindows callbacks
2400 // -----------------------------------------------------------------------------
2402 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2410 dc
.SetFont( m_normalFont
);
2411 dc
.SetPen( m_dottedPen
);
2413 // this is now done dynamically
2414 //if(GetImageList() == NULL)
2415 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2418 PaintLevel( m_anchor
, dc
, 0, y
);
2421 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2430 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2439 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2441 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2442 te
.m_evtKey
= event
;
2443 te
.SetEventObject( this );
2444 if ( GetEventHandler()->ProcessEvent( te
) )
2446 // intercepted by the user code
2450 if ( (m_current
== 0) || (m_key_current
== 0) )
2456 // how should the selection work for this event?
2457 bool is_multiple
, extended_select
, unselect_others
;
2458 EventFlagsToSelType(GetWindowStyleFlag(),
2460 event
.ControlDown(),
2461 is_multiple
, extended_select
, unselect_others
);
2465 // * : Expand all/Collapse all
2466 // ' ' | return : activate
2467 // up : go up (not last children!)
2469 // left : go to parent
2470 // right : open if parent and go next
2471 // home : go to root
2472 // end : go to last item without opening parents
2473 // alnum : start or continue searching for the item with this prefix
2474 int keyCode
= event
.KeyCode();
2479 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2487 if ( !IsExpanded(m_current
) )
2490 ExpandAll(m_current
);
2493 //else: fall through to Collapse() it
2497 if (IsExpanded(m_current
))
2499 Collapse(m_current
);
2506 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2507 event
.m_item
= (long) m_current
;
2508 event
.SetEventObject( this );
2509 GetEventHandler()->ProcessEvent( event
);
2513 // up goes to the previous sibling or to the last
2514 // of its children if it's expanded
2517 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2520 prev
= GetParent( m_key_current
);
2521 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2523 break; // don't go to root if it is hidden
2528 wxTreeItemId current
= m_key_current
;
2529 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2530 if (current
== GetFirstChild( prev
, cookie
))
2532 // otherwise we return to where we came from
2533 SelectItem( prev
, unselect_others
, extended_select
);
2534 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2541 while ( IsExpanded(prev
) && HasChildren(prev
) )
2543 wxTreeItemId child
= GetLastChild(prev
);
2550 SelectItem( prev
, unselect_others
, extended_select
);
2551 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2556 // left arrow goes to the parent
2559 wxTreeItemId prev
= GetParent( m_current
);
2560 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2562 // don't go to root if it is hidden
2563 prev
= GetPrevSibling( m_current
);
2567 SelectItem( prev
, unselect_others
, extended_select
);
2573 // this works the same as the down arrow except that we
2574 // also expand the item if it wasn't expanded yet
2580 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2583 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2584 SelectItem( child
, unselect_others
, extended_select
);
2585 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2589 wxTreeItemId next
= GetNextSibling( m_key_current
);
2592 wxTreeItemId current
= m_key_current
;
2593 while (current
&& !next
)
2595 current
= GetParent( current
);
2596 if (current
) next
= GetNextSibling( current
);
2601 SelectItem( next
, unselect_others
, extended_select
);
2602 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2608 // <End> selects the last visible tree item
2611 wxTreeItemId last
= GetRootItem();
2613 while ( last
.IsOk() && IsExpanded(last
) )
2615 wxTreeItemId lastChild
= GetLastChild(last
);
2617 // it may happen if the item was expanded but then all of
2618 // its children have been deleted - so IsExpanded() returned
2619 // TRUE, but GetLastChild() returned invalid item
2628 SelectItem( last
, unselect_others
, extended_select
);
2633 // <Home> selects the root item
2636 wxTreeItemId prev
= GetRootItem();
2640 if ( HasFlag(wxTR_HIDE_ROOT
) )
2643 prev
= GetFirstChild(prev
, dummy
);
2648 SelectItem( prev
, unselect_others
, extended_select
);
2653 // do not use wxIsalnum() here
2654 if ( !event
.HasModifiers() &&
2655 ((keyCode
>= '0' && keyCode
<= '9') ||
2656 (keyCode
>= 'a' && keyCode
<= 'z') ||
2657 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2659 // find the next item starting with the given prefix
2660 char ch
= (char)keyCode
;
2662 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2673 // also start the timer to reset the current prefix if the user
2674 // doesn't press any more alnum keys soon -- we wouldn't want
2675 // to use this prefix for a new item search
2678 m_findTimer
= new wxTreeFindTimer(this);
2681 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2690 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2692 // JACS: removed wxYieldIfNeeded() because it can cause the window
2693 // to be deleted from under us if a close window event is pending
2698 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2699 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2700 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2701 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2702 if (flags
) return wxTreeItemId();
2704 if (m_anchor
== NULL
)
2706 flags
= wxTREE_HITTEST_NOWHERE
;
2707 return wxTreeItemId();
2710 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2714 flags
= wxTREE_HITTEST_NOWHERE
;
2715 return wxTreeItemId();
2720 // get the bounding rectangle of the item (or of its label only)
2721 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2723 bool WXUNUSED(textOnly
)) const
2725 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2727 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2730 GetViewStart(& startX
, & startY
);
2732 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2733 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2734 rect
.width
= i
->GetWidth();
2735 //rect.height = i->GetHeight();
2736 rect
.height
= GetLineHeight(i
);
2741 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2743 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2745 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2747 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2748 te
.m_item
= (long) itemEdit
;
2749 te
.SetEventObject( this );
2750 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2756 // We have to call this here because the label in
2757 // question might just have been added and no screen
2758 // update taken place.
2762 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, itemEdit
);
2767 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2768 const wxString
& value
)
2770 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2771 le
.m_item
= (long) item
;
2772 le
.SetEventObject( this );
2775 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2778 void wxGenericTreeCtrl::OnRenameTimer()
2783 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2785 if ( !m_anchor
) return;
2787 // we process left mouse up event (enables in-place edit), right down
2788 // (pass to the user code), left dbl click (activate item) and
2789 // dragging/moving events for items drag-and-drop
2790 if ( !(event
.LeftDown() ||
2792 event
.RightDown() ||
2793 event
.LeftDClick() ||
2795 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2802 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2805 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2807 if ( event
.Dragging() && !m_isDragging
)
2809 if (m_dragCount
== 0)
2814 if (m_dragCount
!= 3)
2816 // wait until user drags a bit further...
2820 wxEventType command
= event
.RightIsDown()
2821 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2822 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2824 wxTreeEvent
nevent( command
, GetId() );
2825 nevent
.m_item
= (long) m_current
;
2826 nevent
.SetEventObject(this);
2828 // by default the dragging is not supported, the user code must
2829 // explicitly allow the event for it to take place
2832 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2834 // we're going to drag this item
2835 m_isDragging
= TRUE
;
2837 // remember the old cursor because we will change it while
2839 m_oldCursor
= m_cursor
;
2841 // in a single selection control, hide the selection temporarily
2842 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2844 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2846 if ( m_oldSelection
)
2848 m_oldSelection
->SetHilight(FALSE
);
2849 RefreshLine(m_oldSelection
);
2856 else if ( event
.Moving() )
2858 if ( item
!= m_dropTarget
)
2860 // unhighlight the previous drop target
2861 DrawDropEffect(m_dropTarget
);
2863 m_dropTarget
= item
;
2865 // highlight the current drop target if any
2866 DrawDropEffect(m_dropTarget
);
2871 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2873 // erase the highlighting
2874 DrawDropEffect(m_dropTarget
);
2876 if ( m_oldSelection
)
2878 m_oldSelection
->SetHilight(TRUE
);
2879 RefreshLine(m_oldSelection
);
2880 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2883 // generate the drag end event
2884 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2886 event
.m_item
= (long) item
;
2887 event
.m_pointDrag
= pt
;
2888 event
.SetEventObject(this);
2890 (void)GetEventHandler()->ProcessEvent(event
);
2892 m_isDragging
= FALSE
;
2893 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2897 SetCursor(m_oldCursor
);
2903 // here we process only the messages which happen on tree items
2907 if (item
== NULL
) return; /* we hit the blank area */
2909 if ( event
.RightDown() )
2911 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2912 nevent
.m_item
= (long) item
;
2913 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
2914 nevent
.SetEventObject(this);
2915 GetEventHandler()->ProcessEvent(nevent
);
2917 else if ( event
.LeftUp() )
2921 if ( (item
== m_current
) &&
2922 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2923 HasFlag(wxTR_EDIT_LABELS
) )
2925 if ( m_renameTimer
)
2927 if ( m_renameTimer
->IsRunning() )
2928 m_renameTimer
->Stop();
2932 m_renameTimer
= new wxTreeRenameTimer( this );
2935 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
2938 m_lastOnSame
= FALSE
;
2941 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2943 if ( event
.LeftDown() )
2945 m_lastOnSame
= item
== m_current
;
2948 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2950 // only toggle the item for a single click, double click on
2951 // the button doesn't do anything (it toggles the item twice)
2952 if ( event
.LeftDown() )
2957 // don't select the item if the button was clicked
2961 // how should the selection work for this event?
2962 bool is_multiple
, extended_select
, unselect_others
;
2963 EventFlagsToSelType(GetWindowStyleFlag(),
2965 event
.ControlDown(),
2966 is_multiple
, extended_select
, unselect_others
);
2968 SelectItem(item
, unselect_others
, extended_select
);
2970 // For some reason, Windows isn't recognizing a left double-click,
2971 // so we need to simulate it here. Allow 200 milliseconds for now.
2972 if ( event
.LeftDClick() )
2974 // double clicking should not start editing the item label
2975 if ( m_renameTimer
)
2976 m_renameTimer
->Stop();
2978 m_lastOnSame
= FALSE
;
2980 // send activate event first
2981 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2982 nevent
.m_item
= (long) item
;
2983 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
2984 nevent
.SetEventObject( this );
2985 if ( !GetEventHandler()->ProcessEvent( nevent
) )
2987 // if the user code didn't process the activate event,
2988 // handle it ourselves by toggling the item when it is
2990 if ( item
->HasPlus() )
3000 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
3002 /* after all changes have been done to the tree control,
3003 * we actually redraw the tree when everything is over */
3005 if (!m_dirty
) return;
3009 CalculatePositions();
3011 AdjustMyScrollbars();
3014 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3019 wxTreeItemAttr
*attr
= item
->GetAttributes();
3020 if ( attr
&& attr
->HasFont() )
3021 dc
.SetFont(attr
->GetFont());
3022 else if ( item
->IsBold() )
3023 dc
.SetFont(m_boldFont
);
3025 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3028 // restore normal font
3029 dc
.SetFont( m_normalFont
);
3033 int image
= item
->GetCurrentImage();
3034 if ( image
!= NO_IMAGE
)
3036 if ( m_imageListNormal
)
3038 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3043 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3046 total_h
+= 2; // at least 2 pixels
3048 total_h
+= total_h
/10; // otherwise 10% extra spacing
3050 item
->SetHeight(total_h
);
3051 if (total_h
>m_lineHeight
)
3052 m_lineHeight
=total_h
;
3054 item
->SetWidth(image_w
+text_w
+2);
3057 // -----------------------------------------------------------------------------
3058 // for developper : y is now the top of the level
3059 // not the middle of it !
3060 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3062 int x
= level
*m_indent
;
3063 if (!HasFlag(wxTR_HIDE_ROOT
))
3067 else if (level
== 0)
3069 // a hidden root is not evaluated, but its
3070 // children are always calculated
3074 CalculateSize( item
, dc
);
3077 item
->SetX( x
+m_spacing
);
3079 y
+= GetLineHeight(item
);
3081 if ( !item
->IsExpanded() )
3083 // we don't need to calculate collapsed branches
3088 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3089 size_t n
, count
= children
.Count();
3091 for (n
= 0; n
< count
; ++n
)
3092 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3095 void wxGenericTreeCtrl::CalculatePositions()
3097 if ( !m_anchor
) return;
3099 wxClientDC
dc(this);
3102 dc
.SetFont( m_normalFont
);
3104 dc
.SetPen( m_dottedPen
);
3105 //if(GetImageList() == NULL)
3106 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3109 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3112 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3114 if (m_dirty
) return;
3116 wxSize client
= GetClientSize();
3119 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3120 rect
.width
= client
.x
;
3121 rect
.height
= client
.y
;
3123 Refresh(TRUE
, &rect
);
3125 AdjustMyScrollbars();
3128 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3130 if (m_dirty
) return;
3133 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3134 rect
.width
= GetClientSize().x
;
3135 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3137 Refresh(TRUE
, &rect
);
3140 void wxGenericTreeCtrl::RefreshSelected()
3142 // TODO: this is awfully inefficient, we should keep the list of all
3143 // selected items internally, should be much faster
3145 RefreshSelectedUnder(m_anchor
);
3148 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3150 if ( item
->IsSelected() )
3153 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3154 size_t count
= children
.GetCount();
3155 for ( size_t n
= 0; n
< count
; n
++ )
3157 RefreshSelectedUnder(children
[n
]);
3161 // ----------------------------------------------------------------------------
3162 // changing colours: we need to refresh the tree control
3163 // ----------------------------------------------------------------------------
3165 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3167 if ( !wxWindow::SetBackgroundColour(colour
) )
3175 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3177 if ( !wxWindow::SetForegroundColour(colour
) )
3185 #endif // wxUSE_TREECTRL