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
452 m_owner
->OnRenameCancelled(m_itemEdited
);
460 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
464 // auto-grow the textctrl:
465 wxSize parentSize
= m_owner
->GetSize();
466 wxPoint myPos
= GetPosition();
467 wxSize mySize
= GetSize();
469 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
470 if (myPos
.x
+ sx
> parentSize
.x
)
471 sx
= parentSize
.x
- myPos
.x
;
480 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
488 if ( AcceptChanges() )
494 // -----------------------------------------------------------------------------
496 // -----------------------------------------------------------------------------
498 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
499 const wxString
& text
,
500 int image
, int selImage
,
501 wxTreeItemData
*data
)
504 m_images
[wxTreeItemIcon_Normal
] = image
;
505 m_images
[wxTreeItemIcon_Selected
] = selImage
;
506 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
507 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
512 m_isCollapsed
= TRUE
;
513 m_hasHilight
= FALSE
;
519 m_attr
= (wxTreeItemAttr
*)NULL
;
522 // We don't know the height here yet.
527 wxGenericTreeItem::~wxGenericTreeItem()
531 if (m_ownsAttr
) delete m_attr
;
533 wxASSERT_MSG( m_children
.IsEmpty(),
534 wxT("please call DeleteChildren() before deleting the item") );
537 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
539 size_t count
= m_children
.Count();
540 for ( size_t n
= 0; n
< count
; n
++ )
542 wxGenericTreeItem
*child
= m_children
[n
];
544 tree
->SendDeleteEvent(child
);
546 child
->DeleteChildren(tree
);
553 void wxGenericTreeItem::SetText( const wxString
&text
)
558 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
560 size_t count
= m_children
.Count();
564 size_t total
= count
;
565 for (size_t n
= 0; n
< count
; ++n
)
567 total
+= m_children
[n
]->GetChildrenCount();
573 void wxGenericTreeItem::GetSize( int &x
, int &y
,
574 const wxGenericTreeCtrl
*theButton
)
576 int bottomY
=m_y
+theButton
->GetLineHeight(this);
577 if ( y
< bottomY
) y
= bottomY
;
578 int width
= m_x
+ m_width
;
579 if ( x
< width
) x
= width
;
583 size_t count
= m_children
.Count();
584 for ( size_t n
= 0; n
< count
; ++n
)
586 m_children
[n
]->GetSize( x
, y
, theButton
);
591 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
592 const wxGenericTreeCtrl
*theCtrl
,
596 // for a hidden root node, don't evaluate it, but do evaluate children
597 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
600 int h
= theCtrl
->GetLineHeight(this);
601 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
603 int y_mid
= m_y
+ h
/2;
604 if (point
.y
< y_mid
)
605 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
607 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
609 // 5 is the size of the plus sign
610 int xCross
= m_x
- theCtrl
->GetSpacing();
611 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
612 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
613 HasPlus() && theCtrl
->HasButtons() )
615 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
619 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
624 // assuming every image (normal and selected) has the same size!
625 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
626 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
629 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
630 flags
|= wxTREE_HITTEST_ONITEMICON
;
632 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
638 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
639 if (point
.x
> m_x
+m_width
)
640 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
645 // if children are expanded, fall through to evaluate them
646 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
650 size_t count
= m_children
.Count();
651 for ( size_t n
= 0; n
< count
; n
++ )
653 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
661 return (wxGenericTreeItem
*) NULL
;
664 int wxGenericTreeItem::GetCurrentImage() const
666 int image
= NO_IMAGE
;
671 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
674 if ( image
== NO_IMAGE
)
676 // we usually fall back to the normal item, but try just the
677 // expanded one (and not selected) first in this case
678 image
= GetImage(wxTreeItemIcon_Expanded
);
684 image
= GetImage(wxTreeItemIcon_Selected
);
687 // maybe it doesn't have the specific image we want,
688 // try the default one instead
689 if ( image
== NO_IMAGE
) image
= GetImage();
694 // -----------------------------------------------------------------------------
695 // wxGenericTreeCtrl implementation
696 // -----------------------------------------------------------------------------
698 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
700 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
701 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
702 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
703 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
704 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
705 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
706 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
709 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
711 * wxTreeCtrl has to be a real class or we have problems with
712 * the run-time information.
715 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
718 // -----------------------------------------------------------------------------
719 // construction/destruction
720 // -----------------------------------------------------------------------------
722 void wxGenericTreeCtrl::Init()
724 m_current
= m_key_current
= m_anchor
= (wxGenericTreeItem
*) NULL
;
732 m_hilightBrush
= new wxBrush
734 wxSystemSettings::GetColour
736 wxSYS_COLOUR_HIGHLIGHT
741 m_hilightUnfocusedBrush
= new wxBrush
743 wxSystemSettings::GetColour
745 wxSYS_COLOUR_BTNSHADOW
750 m_imageListNormal
= m_imageListButtons
=
751 m_imageListState
= (wxImageList
*) NULL
;
752 m_ownsImageListNormal
= m_ownsImageListButtons
=
753 m_ownsImageListState
= FALSE
;
756 m_isDragging
= FALSE
;
757 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
759 m_renameTimer
= NULL
;
762 m_lastOnSame
= FALSE
;
764 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
765 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
766 m_normalFont
.GetFamily(),
767 m_normalFont
.GetStyle(),
769 m_normalFont
.GetUnderlined());
772 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
777 const wxValidator
&validator
,
778 const wxString
& name
)
782 wxGetOsVersion( &major
, &minor
);
784 if (style
& wxTR_HAS_BUTTONS
) style
|= wxTR_MAC_BUTTONS
;
785 if (style
& wxTR_HAS_BUTTONS
) style
&= ~wxTR_HAS_BUTTONS
;
786 style
&= ~wxTR_LINES_AT_ROOT
;
787 style
|= wxTR_NO_LINES
;
789 style
|= wxTR_ROW_LINES
;
791 style
|= wxTR_AQUA_BUTTONS
;
794 if (style
& wxTR_AQUA_BUTTONS
)
796 m_arrowRight
= new wxBitmap( aqua_arrow_right
);
797 m_arrowDown
= new wxBitmap( aqua_arrow_down
);
805 wxScrolledWindow::Create( parent
, id
, pos
, size
,
806 style
|wxHSCROLL
|wxVSCROLL
, name
);
808 // If the tree display has no buttons, but does have
809 // connecting lines, we can use a narrower layout.
810 // It may not be a good idea to force this...
811 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
818 SetValidator( validator
);
821 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
822 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
824 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
825 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
830 wxGenericTreeCtrl::~wxGenericTreeCtrl()
832 delete m_hilightBrush
;
833 delete m_hilightUnfocusedBrush
;
840 delete m_renameTimer
;
843 if (m_ownsImageListNormal
)
844 delete m_imageListNormal
;
845 if (m_ownsImageListState
)
846 delete m_imageListState
;
847 if (m_ownsImageListButtons
)
848 delete m_imageListButtons
;
851 // -----------------------------------------------------------------------------
853 // -----------------------------------------------------------------------------
855 size_t wxGenericTreeCtrl::GetCount() const
857 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
860 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
862 m_indent
= (unsigned short) indent
;
866 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
868 m_spacing
= (unsigned short) spacing
;
872 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
874 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
876 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
879 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
881 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
883 // if we will hide the root, make sure children are visible
884 m_anchor
->SetHasPlus();
886 CalculatePositions();
889 // right now, just sets the styles. Eventually, we may
890 // want to update the inherited styles, but right now
891 // none of the parents has updatable styles
892 m_windowStyle
= styles
;
896 // -----------------------------------------------------------------------------
897 // functions to work with tree items
898 // -----------------------------------------------------------------------------
900 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
902 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
904 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
907 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
908 wxTreeItemIcon which
) const
910 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
912 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
915 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
917 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
919 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
922 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
924 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
927 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
928 pItem
->SetText(text
);
929 CalculateSize(pItem
, dc
);
933 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
935 wxTreeItemIcon which
)
937 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
939 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
940 pItem
->SetImage(image
, which
);
943 CalculateSize(pItem
, dc
);
947 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
949 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
951 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
954 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
956 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
958 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
959 pItem
->SetHasPlus(has
);
963 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
965 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
967 // avoid redrawing the tree if no real change
968 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
969 if ( pItem
->IsBold() != bold
)
971 pItem
->SetBold(bold
);
976 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
979 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
981 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
982 pItem
->Attr().SetTextColour(col
);
986 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
989 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
991 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
992 pItem
->Attr().SetBackgroundColour(col
);
996 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
998 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1000 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1001 pItem
->Attr().SetFont(font
);
1005 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1007 wxScrolledWindow::SetFont(font
);
1009 m_normalFont
= font
;
1010 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
1011 m_normalFont
.GetFamily(),
1012 m_normalFont
.GetStyle(),
1014 m_normalFont
.GetUnderlined());
1020 // -----------------------------------------------------------------------------
1021 // item status inquiries
1022 // -----------------------------------------------------------------------------
1024 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1026 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1028 // An item is only visible if it's not a descendant of a collapsed item
1029 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1030 wxGenericTreeItem
* parent
= pItem
->GetParent();
1033 if (!parent
->IsExpanded())
1035 parent
= parent
->GetParent();
1039 GetViewStart(& startX
, & startY
);
1041 wxSize clientSize
= GetClientSize();
1044 if (!GetBoundingRect(item
, rect
))
1046 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1048 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1050 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1056 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1058 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1060 // consider that the item does have children if it has the "+" button: it
1061 // might not have them (if it had never been expanded yet) but then it
1062 // could have them as well and it's better to err on this side rather than
1063 // disabling some operations which are restricted to the items with
1064 // children for an item which does have them
1065 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1068 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1070 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1072 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1075 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1077 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1079 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1082 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1084 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1086 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1089 // -----------------------------------------------------------------------------
1091 // -----------------------------------------------------------------------------
1093 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1095 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1097 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1100 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
1102 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1105 return GetNextChild(item
, cookie
);
1108 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
1110 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1112 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1113 if ( (size_t)cookie
< children
.Count() )
1115 return children
.Item((size_t)cookie
++);
1119 // there are no more of them
1120 return wxTreeItemId();
1124 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1126 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1128 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1129 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1132 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1134 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1136 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1137 wxGenericTreeItem
*parent
= i
->GetParent();
1138 if ( parent
== NULL
)
1140 // root item doesn't have any siblings
1141 return wxTreeItemId();
1144 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1145 int index
= siblings
.Index(i
);
1146 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1148 size_t n
= (size_t)(index
+ 1);
1149 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1152 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1154 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1156 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1157 wxGenericTreeItem
*parent
= i
->GetParent();
1158 if ( parent
== NULL
)
1160 // root item doesn't have any siblings
1161 return wxTreeItemId();
1164 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1165 int index
= siblings
.Index(i
);
1166 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1168 return index
== 0 ? wxTreeItemId()
1169 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1172 // Only for internal use right now, but should probably be public
1173 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1175 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1177 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1179 // First see if there are any children.
1180 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1181 if (children
.GetCount() > 0)
1183 return children
.Item(0);
1187 // Try a sibling of this or ancestor instead
1188 wxTreeItemId p
= item
;
1189 wxTreeItemId toFind
;
1192 toFind
= GetNextSibling(p
);
1194 } while (p
.IsOk() && !toFind
.IsOk());
1199 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1201 wxTreeItemId id
= GetRootItem();
1210 } while (id
.IsOk());
1212 return wxTreeItemId();
1215 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1217 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1219 wxTreeItemId id
= item
;
1222 while (id
= GetNext(id
), id
.IsOk())
1228 return wxTreeItemId();
1231 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1233 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1235 wxFAIL_MSG(wxT("not implemented"));
1237 return wxTreeItemId();
1240 // find the first item starting with the given prefix after the given item
1241 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1242 const wxString
& prefixOrig
) const
1244 // match is case insensitive as this is more convenient to the user: having
1245 // to press Shift-letter to go to the item starting with a capital letter
1246 // would be too bothersome
1247 wxString prefix
= prefixOrig
.Lower();
1249 // determine the starting point: we shouldn't take the current item (this
1250 // allows to switch between two items starting with the same letter just by
1251 // pressing it) but we shouldn't jump to the next one if the user is
1252 // continuing to type as otherwise he might easily skip the item he wanted
1253 wxTreeItemId id
= idParent
;
1254 if ( prefix
.length() == 1 )
1259 // look for the item starting with the given prefix after it
1260 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1265 // if we haven't found anything...
1268 // ... wrap to the beginning
1270 if ( HasFlag(wxTR_HIDE_ROOT
) )
1272 // can't select virtual root
1276 // and try all the items (stop when we get to the one we started from)
1277 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1286 // -----------------------------------------------------------------------------
1288 // -----------------------------------------------------------------------------
1290 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1292 const wxString
& text
,
1293 int image
, int selImage
,
1294 wxTreeItemData
*data
)
1296 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1299 // should we give a warning here?
1300 return AddRoot(text
, image
, selImage
, data
);
1303 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1305 wxGenericTreeItem
*item
=
1306 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1310 data
->m_pItem
= (long) item
;
1313 parent
->Insert( item
, previous
);
1318 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1319 int image
, int selImage
,
1320 wxTreeItemData
*data
)
1322 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1324 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1326 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1327 image
, selImage
, data
);
1330 data
->m_pItem
= (long) m_anchor
;
1333 if (HasFlag(wxTR_HIDE_ROOT
))
1335 // if root is hidden, make sure we can navigate
1337 m_anchor
->SetHasPlus();
1339 CalculatePositions();
1342 if (!HasFlag(wxTR_MULTIPLE
))
1344 m_current
= m_key_current
= m_anchor
;
1345 m_current
->SetHilight( TRUE
);
1351 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1352 const wxString
& text
,
1353 int image
, int selImage
,
1354 wxTreeItemData
*data
)
1356 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1359 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1360 const wxTreeItemId
& idPrevious
,
1361 const wxString
& text
,
1362 int image
, int selImage
,
1363 wxTreeItemData
*data
)
1365 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1368 // should we give a warning here?
1369 return AddRoot(text
, image
, selImage
, data
);
1373 if (idPrevious
.IsOk())
1375 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1376 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1377 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1380 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1383 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1385 const wxString
& text
,
1386 int image
, int selImage
,
1387 wxTreeItemData
*data
)
1389 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1392 // should we give a warning here?
1393 return AddRoot(text
, image
, selImage
, data
);
1396 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1399 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1400 const wxString
& text
,
1401 int image
, int selImage
,
1402 wxTreeItemData
*data
)
1404 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1407 // should we give a warning here?
1408 return AddRoot(text
, image
, selImage
, data
);
1411 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1412 image
, selImage
, data
);
1415 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1417 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1418 event
.m_item
= (long) item
;
1419 event
.SetEventObject( this );
1420 ProcessEvent( event
);
1423 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1425 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1427 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1428 item
->DeleteChildren(this);
1431 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1433 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1435 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1437 wxGenericTreeItem
*parent
= item
->GetParent();
1439 // don't keep stale pointers around!
1440 if ( IsDescendantOf(item
, m_key_current
) )
1442 m_key_current
= parent
;
1445 if ( IsDescendantOf(item
, m_current
) )
1450 // remove the item from the tree
1453 parent
->GetChildren().Remove( item
); // remove by value
1455 else // deleting the root
1457 // nothing will be left in the tree
1461 // and delete all of its children and the item itself now
1462 item
->DeleteChildren(this);
1463 SendDeleteEvent(item
);
1467 void wxGenericTreeCtrl::DeleteAllItems()
1475 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1477 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1479 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1480 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1481 _T("can't expand hidden root") );
1483 if ( !item
->HasPlus() )
1486 if ( item
->IsExpanded() )
1489 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1490 event
.m_item
= (long) item
;
1491 event
.SetEventObject( this );
1493 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1495 // cancelled by program
1500 CalculatePositions();
1502 RefreshSubtree(item
);
1504 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1505 ProcessEvent( event
);
1508 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1510 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1513 if ( !IsExpanded(item
) )
1518 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1519 while ( child
.IsOk() )
1523 child
= GetNextChild(item
, cookie
);
1527 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1529 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1530 _T("can't collapse hidden root") );
1532 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1534 if ( !item
->IsExpanded() )
1537 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1538 event
.m_item
= (long) item
;
1539 event
.SetEventObject( this );
1540 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1542 // cancelled by program
1548 #if 0 // TODO why should items be collapsed recursively?
1549 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1550 size_t count
= children
.Count();
1551 for ( size_t n
= 0; n
< count
; n
++ )
1553 Collapse(children
[n
]);
1557 CalculatePositions();
1559 RefreshSubtree(item
);
1561 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1562 ProcessEvent( event
);
1565 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1568 DeleteChildren(item
);
1571 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1573 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1575 if (item
->IsExpanded())
1581 void wxGenericTreeCtrl::Unselect()
1585 m_current
->SetHilight( FALSE
);
1586 RefreshLine( m_current
);
1592 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1594 if (item
->IsSelected())
1596 item
->SetHilight(FALSE
);
1600 if (item
->HasChildren())
1602 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1603 size_t count
= children
.Count();
1604 for ( size_t n
= 0; n
< count
; ++n
)
1606 UnselectAllChildren(children
[n
]);
1611 void wxGenericTreeCtrl::UnselectAll()
1613 wxTreeItemId rootItem
= GetRootItem();
1615 // the tree might not have the root item at all
1618 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1622 // Recursive function !
1623 // To stop we must have crt_item<last_item
1625 // Tag all next children, when no more children,
1626 // Move to parent (not to tag)
1627 // Keep going... if we found last_item, we stop.
1628 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1630 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1632 if (parent
== NULL
) // This is root item
1633 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1635 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1636 int index
= children
.Index(crt_item
);
1637 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1639 size_t count
= children
.Count();
1640 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1642 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1645 return TagNextChildren(parent
, last_item
, select
);
1648 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1650 crt_item
->SetHilight(select
);
1651 RefreshLine(crt_item
);
1653 if (crt_item
==last_item
)
1656 if (crt_item
->HasChildren())
1658 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1659 size_t count
= children
.Count();
1660 for ( size_t n
= 0; n
< count
; ++n
)
1662 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1670 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1672 // item2 is not necessary after item1
1673 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1675 // choice first' and 'last' between item1 and item2
1676 if (item1
->GetY()<item2
->GetY())
1687 bool select
= m_current
->IsSelected();
1689 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1692 TagNextChildren(first
,last
,select
);
1695 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1696 bool unselect_others
,
1697 bool extended_select
)
1699 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1701 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1702 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1704 //wxCHECK_RET( ( (!unselect_others) && is_single),
1705 // wxT("this is a single selection tree") );
1707 // to keep going anyhow !!!
1710 if (item
->IsSelected())
1711 return; // nothing to do
1712 unselect_others
= TRUE
;
1713 extended_select
= FALSE
;
1715 else if ( unselect_others
&& item
->IsSelected() )
1717 // selection change if there is more than one item currently selected
1718 wxArrayTreeItemIds selected_items
;
1719 if ( GetSelections(selected_items
) == 1 )
1723 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1724 event
.m_item
= (long) item
;
1725 event
.m_itemOld
= (long) m_current
;
1726 event
.SetEventObject( this );
1727 // TODO : Here we don't send any selection mode yet !
1729 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1732 wxTreeItemId parent
= GetParent( itemId
);
1733 while (parent
.IsOk())
1735 if (!IsExpanded(parent
))
1738 parent
= GetParent( parent
);
1741 EnsureVisible( itemId
);
1744 if (unselect_others
)
1746 if (is_single
) Unselect(); // to speed up thing
1751 if (extended_select
)
1755 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1758 // don't change the mark (m_current)
1759 SelectItemRange(m_current
, item
);
1763 bool select
=TRUE
; // the default
1765 // Check if we need to toggle hilight (ctrl mode)
1766 if (!unselect_others
)
1767 select
=!item
->IsSelected();
1769 m_current
= m_key_current
= item
;
1770 m_current
->SetHilight(select
);
1771 RefreshLine( m_current
);
1774 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1775 GetEventHandler()->ProcessEvent( event
);
1778 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1779 wxArrayTreeItemIds
&array
) const
1781 if ( item
->IsSelected() )
1782 array
.Add(wxTreeItemId(item
));
1784 if ( item
->HasChildren() )
1786 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1787 size_t count
= children
.GetCount();
1788 for ( size_t n
= 0; n
< count
; ++n
)
1789 FillArray(children
[n
], array
);
1793 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1796 wxTreeItemId idRoot
= GetRootItem();
1797 if ( idRoot
.IsOk() )
1799 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1801 //else: the tree is empty, so no selections
1803 return array
.Count();
1806 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1808 if (!item
.IsOk()) return;
1810 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1812 // first expand all parent branches
1813 wxGenericTreeItem
*parent
= gitem
->GetParent();
1815 if ( HasFlag(wxTR_HIDE_ROOT
) )
1817 while ( parent
!= m_anchor
)
1820 parent
= parent
->GetParent();
1828 parent
= parent
->GetParent();
1832 //if (parent) CalculatePositions();
1837 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1839 if (!item
.IsOk()) return;
1841 // We have to call this here because the label in
1842 // question might just have been added and no screen
1843 // update taken place.
1844 if (m_dirty
) wxYieldIfNeeded();
1846 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1848 // now scroll to the item
1849 int item_y
= gitem
->GetY();
1853 GetViewStart( &start_x
, &start_y
);
1854 start_y
*= PIXELS_PER_UNIT
;
1858 GetClientSize( &client_w
, &client_h
);
1860 if (item_y
< start_y
+3)
1865 m_anchor
->GetSize( x
, y
, this );
1866 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1867 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1868 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1869 // Item should appear at top
1870 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1872 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1877 m_anchor
->GetSize( x
, y
, this );
1878 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1879 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1880 item_y
+= PIXELS_PER_UNIT
+2;
1881 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1882 // Item should appear at bottom
1883 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
);
1887 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1888 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1890 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1891 wxGenericTreeItem
**item2
)
1893 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1895 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1898 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1899 const wxTreeItemId
& item2
)
1901 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1904 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1906 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1908 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1910 wxCHECK_RET( !s_treeBeingSorted
,
1911 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1913 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1914 if ( children
.Count() > 1 )
1918 s_treeBeingSorted
= this;
1919 children
.Sort(tree_ctrl_compare_func
);
1920 s_treeBeingSorted
= NULL
;
1922 //else: don't make the tree dirty as nothing changed
1925 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1927 return m_imageListNormal
;
1930 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1932 return m_imageListButtons
;
1935 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1937 return m_imageListState
;
1940 void wxGenericTreeCtrl::CalculateLineHeight()
1942 wxClientDC
dc(this);
1943 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1945 if ( m_imageListNormal
)
1947 // Calculate a m_lineHeight value from the normal Image sizes.
1948 // May be toggle off. Then wxGenericTreeCtrl will spread when
1949 // necessary (which might look ugly).
1950 int n
= m_imageListNormal
->GetImageCount();
1951 for (int i
= 0; i
< n
; i
++)
1953 int width
= 0, height
= 0;
1954 m_imageListNormal
->GetSize(i
, width
, height
);
1955 if (height
> m_lineHeight
) m_lineHeight
= height
;
1959 if (m_imageListButtons
)
1961 // Calculate a m_lineHeight value from the Button image sizes.
1962 // May be toggle off. Then wxGenericTreeCtrl will spread when
1963 // necessary (which might look ugly).
1964 int n
= m_imageListButtons
->GetImageCount();
1965 for (int i
= 0; i
< n
; i
++)
1967 int width
= 0, height
= 0;
1968 m_imageListButtons
->GetSize(i
, width
, height
);
1969 if (height
> m_lineHeight
) m_lineHeight
= height
;
1973 if (m_lineHeight
< 30)
1974 m_lineHeight
+= 2; // at least 2 pixels
1976 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1979 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1981 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1982 m_imageListNormal
= imageList
;
1983 m_ownsImageListNormal
= FALSE
;
1985 // Don't do any drawing if we're setting the list to NULL,
1986 // since we may be in the process of deleting the tree control.
1988 CalculateLineHeight();
1991 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1993 if (m_ownsImageListState
) delete m_imageListState
;
1994 m_imageListState
= imageList
;
1995 m_ownsImageListState
= FALSE
;
1998 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2000 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2001 m_imageListButtons
= imageList
;
2002 m_ownsImageListButtons
= FALSE
;
2004 CalculateLineHeight();
2007 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2009 SetImageList(imageList
);
2010 m_ownsImageListNormal
= TRUE
;
2013 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2015 SetStateImageList(imageList
);
2016 m_ownsImageListState
= TRUE
;
2019 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2021 SetButtonsImageList(imageList
);
2022 m_ownsImageListButtons
= TRUE
;
2025 // -----------------------------------------------------------------------------
2027 // -----------------------------------------------------------------------------
2029 void wxGenericTreeCtrl::AdjustMyScrollbars()
2034 m_anchor
->GetSize( x
, y
, this );
2035 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2036 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2037 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2038 int y_pos
= GetScrollPos( wxVERTICAL
);
2039 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2043 SetScrollbars( 0, 0, 0, 0 );
2047 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2049 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2050 return item
->GetHeight();
2052 return m_lineHeight
;
2055 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2057 // TODO implement "state" icon on items
2059 wxTreeItemAttr
*attr
= item
->GetAttributes();
2060 if ( attr
&& attr
->HasFont() )
2061 dc
.SetFont(attr
->GetFont());
2062 else if (item
->IsBold())
2063 dc
.SetFont(m_boldFont
);
2065 long text_w
= 0, text_h
= 0;
2066 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2068 int image_h
= 0, image_w
= 0;
2069 int image
= item
->GetCurrentImage();
2070 if ( image
!= NO_IMAGE
)
2072 if ( m_imageListNormal
)
2074 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2083 int total_h
= GetLineHeight(item
);
2085 if ( item
->IsSelected() )
2087 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2092 if ( attr
&& attr
->HasBackgroundColour() )
2093 colBg
= attr
->GetBackgroundColour();
2095 colBg
= m_backgroundColour
;
2096 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2099 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2101 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2105 DoGetPosition(&x
, &y
);
2107 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2111 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2113 // If it's selected, and there's an image, then we should
2114 // take care to leave the area under the image painted in the
2115 // background colour.
2116 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2117 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2121 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2122 item
->GetWidth()+2, total_h
-offset
);
2126 if ( image
!= NO_IMAGE
)
2128 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2129 m_imageListNormal
->Draw( image
, dc
,
2131 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2132 wxIMAGELIST_DRAW_TRANSPARENT
);
2133 dc
.DestroyClippingRegion();
2136 dc
.SetBackgroundMode(wxTRANSPARENT
);
2137 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2138 dc
.DrawText( item
->GetText(),
2139 (wxCoord
)(image_w
+ item
->GetX()),
2140 (wxCoord
)(item
->GetY() + extraH
));
2142 // restore normal font
2143 dc
.SetFont( m_normalFont
);
2146 // Now y stands for the top of the item, whereas it used to stand for middle !
2147 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2149 int x
= level
*m_indent
;
2150 if (!HasFlag(wxTR_HIDE_ROOT
))
2154 else if (level
== 0)
2156 // always expand hidden root
2158 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2159 int count
= children
.Count();
2165 PaintLevel(children
[n
], dc
, 1, y
);
2166 } while (++n
< count
);
2168 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2170 // draw line down to last child
2171 origY
+= GetLineHeight(children
[0])>>1;
2172 oldY
+= GetLineHeight(children
[n
-1])>>1;
2173 dc
.DrawLine(3, origY
, 3, oldY
);
2179 item
->SetX(x
+m_spacing
);
2182 int h
= GetLineHeight(item
);
2184 int y_mid
= y_top
+ (h
>>1);
2187 int exposed_x
= dc
.LogicalToDeviceX(0);
2188 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2190 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2194 // don't draw rect outline if we already have the
2195 // background color under Mac
2196 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2197 #endif // !__WXMAC__
2201 if ( item
->IsSelected() )
2203 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2207 wxTreeItemAttr
*attr
= item
->GetAttributes();
2208 if (attr
&& attr
->HasTextColour())
2209 colText
= attr
->GetTextColour();
2211 colText
= GetForegroundColour();
2215 dc
.SetTextForeground(colText
);
2219 PaintItem(item
, dc
);
2221 if (HasFlag(wxTR_ROW_LINES
))
2223 // if the background colour is white, choose a
2224 // contrasting color for the lines
2225 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2226 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2227 dc
.DrawLine(0, y_top
, 10000, y_top
);
2228 dc
.DrawLine(0, y
, 10000, y
);
2231 // restore DC objects
2232 dc
.SetBrush(*wxWHITE_BRUSH
);
2233 dc
.SetPen(m_dottedPen
);
2234 dc
.SetTextForeground(*wxBLACK
);
2236 if (item
->HasPlus() && HasButtons()) // should the item show a button?
2238 if (!HasFlag(wxTR_NO_LINES
))
2240 if (x
> (signed)m_indent
)
2241 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
2242 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2243 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
2244 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
2247 if (m_imageListButtons
!= NULL
)
2249 // draw the image button here
2250 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
2251 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
2252 if (item
->IsSelected())
2253 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2254 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2255 int xx
= x
- (image_w
>>1);
2256 int yy
= y_mid
- (image_h
>>1);
2257 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
2258 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2259 wxIMAGELIST_DRAW_TRANSPARENT
);
2260 dc
.DestroyClippingRegion();
2262 else if (HasFlag(wxTR_TWIST_BUTTONS
))
2264 // draw the twisty button here
2266 if (HasFlag(wxTR_AQUA_BUTTONS
))
2268 if (item
->IsExpanded())
2269 dc
.DrawBitmap( *m_arrowDown
, x
-5, y_mid
-6, TRUE
);
2271 dc
.DrawBitmap( *m_arrowRight
, x
-5, y_mid
-6, TRUE
);
2275 dc
.SetBrush(*m_hilightBrush
);
2276 dc
.SetPen(*wxBLACK_PEN
);
2279 if (item
->IsExpanded())
2282 button
[0].y
= y_mid
-2;
2284 button
[1].y
= y_mid
-2;
2286 button
[2].y
= y_mid
+3;
2290 button
[0].y
= y_mid
-5;
2292 button
[1].y
= y_mid
+5;
2294 button
[2].y
= y_mid
;
2297 dc
.DrawPolygon(3, button
);
2298 dc
.SetPen(m_dottedPen
);
2301 else // if (HasFlag(wxTR_HAS_BUTTONS))
2303 // draw the plus sign here
2304 dc
.SetPen(*wxGREY_PEN
);
2305 dc
.SetBrush(*wxWHITE_BRUSH
);
2306 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2307 dc
.SetPen(*wxBLACK_PEN
);
2308 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2309 if (!item
->IsExpanded())
2310 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2311 dc
.SetPen(m_dottedPen
);
2314 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2316 // draw the horizontal line here
2318 if (x
> (signed)m_indent
)
2319 x_start
-= m_indent
;
2320 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2322 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2326 if (item
->IsExpanded())
2328 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2329 int count
= children
.Count();
2336 PaintLevel(children
[n
], dc
, level
, y
);
2337 } while (++n
< count
);
2339 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2341 // draw line down to last child
2342 oldY
+= GetLineHeight(children
[n
-1])>>1;
2343 if (HasButtons()) y_mid
+= 5;
2344 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2350 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2354 if ( item
->HasPlus() )
2356 // it's a folder, indicate it by a border
2361 // draw a line under the drop target because the item will be
2363 DrawLine(item
, TRUE
/* below */);
2366 SetCursor(wxCURSOR_BULLSEYE
);
2371 SetCursor(wxCURSOR_NO_ENTRY
);
2375 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2377 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2379 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2381 wxClientDC
dc(this);
2383 dc
.SetLogicalFunction(wxINVERT
);
2384 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2386 int w
= i
->GetWidth() + 2;
2387 int h
= GetLineHeight(i
) + 2;
2389 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2392 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2394 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2396 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2398 wxClientDC
dc(this);
2400 dc
.SetLogicalFunction(wxINVERT
);
2406 y
+= GetLineHeight(i
) - 1;
2409 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2412 // -----------------------------------------------------------------------------
2413 // wxWindows callbacks
2414 // -----------------------------------------------------------------------------
2416 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2424 dc
.SetFont( m_normalFont
);
2425 dc
.SetPen( m_dottedPen
);
2427 // this is now done dynamically
2428 //if(GetImageList() == NULL)
2429 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2432 PaintLevel( m_anchor
, dc
, 0, y
);
2435 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2444 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2453 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2455 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2456 te
.m_evtKey
= event
;
2457 te
.SetEventObject( this );
2458 if ( GetEventHandler()->ProcessEvent( te
) )
2460 // intercepted by the user code
2464 if ( (m_current
== 0) || (m_key_current
== 0) )
2470 // how should the selection work for this event?
2471 bool is_multiple
, extended_select
, unselect_others
;
2472 EventFlagsToSelType(GetWindowStyleFlag(),
2474 event
.ControlDown(),
2475 is_multiple
, extended_select
, unselect_others
);
2479 // * : Expand all/Collapse all
2480 // ' ' | return : activate
2481 // up : go up (not last children!)
2483 // left : go to parent
2484 // right : open if parent and go next
2485 // home : go to root
2486 // end : go to last item without opening parents
2487 // alnum : start or continue searching for the item with this prefix
2488 int keyCode
= event
.KeyCode();
2493 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2501 if ( !IsExpanded(m_current
) )
2504 ExpandAll(m_current
);
2507 //else: fall through to Collapse() it
2511 if (IsExpanded(m_current
))
2513 Collapse(m_current
);
2519 if ( !event
.HasModifiers() )
2521 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2522 event
.m_item
= (long) m_current
;
2523 event
.SetEventObject( this );
2524 GetEventHandler()->ProcessEvent( event
);
2527 // in any case, also generate the normal key event for this key,
2528 // even if we generated the ACTIVATED event above: this is what
2529 // wxMSW does and it makes sense because you might not want to
2530 // process ACTIVATED event at all and handle Space and Return
2531 // directly (and differently) which would be impossible otherwise
2535 // up goes to the previous sibling or to the last
2536 // of its children if it's expanded
2539 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2542 prev
= GetParent( m_key_current
);
2543 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2545 break; // don't go to root if it is hidden
2550 wxTreeItemId current
= m_key_current
;
2551 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2552 if (current
== GetFirstChild( prev
, cookie
))
2554 // otherwise we return to where we came from
2555 SelectItem( prev
, unselect_others
, extended_select
);
2556 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2563 while ( IsExpanded(prev
) && HasChildren(prev
) )
2565 wxTreeItemId child
= GetLastChild(prev
);
2572 SelectItem( prev
, unselect_others
, extended_select
);
2573 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2578 // left arrow goes to the parent
2581 wxTreeItemId prev
= GetParent( m_current
);
2582 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2584 // don't go to root if it is hidden
2585 prev
= GetPrevSibling( m_current
);
2589 SelectItem( prev
, unselect_others
, extended_select
);
2595 // this works the same as the down arrow except that we
2596 // also expand the item if it wasn't expanded yet
2602 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2605 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2606 SelectItem( child
, unselect_others
, extended_select
);
2607 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2611 wxTreeItemId next
= GetNextSibling( m_key_current
);
2614 wxTreeItemId current
= m_key_current
;
2615 while (current
&& !next
)
2617 current
= GetParent( current
);
2618 if (current
) next
= GetNextSibling( current
);
2623 SelectItem( next
, unselect_others
, extended_select
);
2624 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2630 // <End> selects the last visible tree item
2633 wxTreeItemId last
= GetRootItem();
2635 while ( last
.IsOk() && IsExpanded(last
) )
2637 wxTreeItemId lastChild
= GetLastChild(last
);
2639 // it may happen if the item was expanded but then all of
2640 // its children have been deleted - so IsExpanded() returned
2641 // TRUE, but GetLastChild() returned invalid item
2650 SelectItem( last
, unselect_others
, extended_select
);
2655 // <Home> selects the root item
2658 wxTreeItemId prev
= GetRootItem();
2662 if ( HasFlag(wxTR_HIDE_ROOT
) )
2665 prev
= GetFirstChild(prev
, dummy
);
2670 SelectItem( prev
, unselect_others
, extended_select
);
2675 // do not use wxIsalnum() here
2676 if ( !event
.HasModifiers() &&
2677 ((keyCode
>= '0' && keyCode
<= '9') ||
2678 (keyCode
>= 'a' && keyCode
<= 'z') ||
2679 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2681 // find the next item starting with the given prefix
2682 char ch
= (char)keyCode
;
2684 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2695 // also start the timer to reset the current prefix if the user
2696 // doesn't press any more alnum keys soon -- we wouldn't want
2697 // to use this prefix for a new item search
2700 m_findTimer
= new wxTreeFindTimer(this);
2703 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2712 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2714 // JACS: removed wxYieldIfNeeded() because it can cause the window
2715 // to be deleted from under us if a close window event is pending
2720 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2721 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2722 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2723 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2724 if (flags
) return wxTreeItemId();
2726 if (m_anchor
== NULL
)
2728 flags
= wxTREE_HITTEST_NOWHERE
;
2729 return wxTreeItemId();
2732 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2736 flags
= wxTREE_HITTEST_NOWHERE
;
2737 return wxTreeItemId();
2742 // get the bounding rectangle of the item (or of its label only)
2743 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2745 bool WXUNUSED(textOnly
)) const
2747 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2749 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2752 GetViewStart(& startX
, & startY
);
2754 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2755 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2756 rect
.width
= i
->GetWidth();
2757 //rect.height = i->GetHeight();
2758 rect
.height
= GetLineHeight(i
);
2763 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2765 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2767 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2769 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2770 te
.m_item
= (long) itemEdit
;
2771 te
.SetEventObject( this );
2772 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2778 // We have to call this here because the label in
2779 // question might just have been added and no screen
2780 // update taken place.
2784 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, itemEdit
);
2789 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2790 const wxString
& value
)
2792 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2793 le
.m_item
= (long) item
;
2794 le
.SetEventObject( this );
2796 le
.m_editCancelled
= FALSE
;
2798 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2801 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2803 // let owner know that the edit was cancelled
2804 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2805 le
.m_item
= (long) item
;
2806 le
.SetEventObject( this );
2807 le
.m_label
= wxEmptyString
;
2808 le
.m_editCancelled
= FALSE
;
2810 GetEventHandler()->ProcessEvent( le
);
2816 void wxGenericTreeCtrl::OnRenameTimer()
2821 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2823 if ( !m_anchor
) return;
2825 // we process left mouse up event (enables in-place edit), right down
2826 // (pass to the user code), left dbl click (activate item) and
2827 // dragging/moving events for items drag-and-drop
2828 if ( !(event
.LeftDown() ||
2830 event
.RightDown() ||
2831 event
.LeftDClick() ||
2833 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2840 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2843 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2845 if ( event
.Dragging() && !m_isDragging
)
2847 if (m_dragCount
== 0)
2852 if (m_dragCount
!= 3)
2854 // wait until user drags a bit further...
2858 wxEventType command
= event
.RightIsDown()
2859 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2860 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2862 wxTreeEvent
nevent( command
, GetId() );
2863 nevent
.m_item
= (long) m_current
;
2864 nevent
.SetEventObject(this);
2866 // by default the dragging is not supported, the user code must
2867 // explicitly allow the event for it to take place
2870 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2872 // we're going to drag this item
2873 m_isDragging
= TRUE
;
2875 // remember the old cursor because we will change it while
2877 m_oldCursor
= m_cursor
;
2879 // in a single selection control, hide the selection temporarily
2880 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2882 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2884 if ( m_oldSelection
)
2886 m_oldSelection
->SetHilight(FALSE
);
2887 RefreshLine(m_oldSelection
);
2894 else if ( event
.Moving() )
2896 if ( item
!= m_dropTarget
)
2898 // unhighlight the previous drop target
2899 DrawDropEffect(m_dropTarget
);
2901 m_dropTarget
= item
;
2903 // highlight the current drop target if any
2904 DrawDropEffect(m_dropTarget
);
2909 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2911 // erase the highlighting
2912 DrawDropEffect(m_dropTarget
);
2914 if ( m_oldSelection
)
2916 m_oldSelection
->SetHilight(TRUE
);
2917 RefreshLine(m_oldSelection
);
2918 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2921 // generate the drag end event
2922 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2924 event
.m_item
= (long) item
;
2925 event
.m_pointDrag
= pt
;
2926 event
.SetEventObject(this);
2928 (void)GetEventHandler()->ProcessEvent(event
);
2930 m_isDragging
= FALSE
;
2931 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2935 SetCursor(m_oldCursor
);
2941 // here we process only the messages which happen on tree items
2945 if (item
== NULL
) return; /* we hit the blank area */
2947 if ( event
.RightDown() )
2949 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2950 nevent
.m_item
= (long) item
;
2951 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
2952 nevent
.SetEventObject(this);
2953 GetEventHandler()->ProcessEvent(nevent
);
2955 else if ( event
.LeftUp() )
2959 if ( (item
== m_current
) &&
2960 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2961 HasFlag(wxTR_EDIT_LABELS
) )
2963 if ( m_renameTimer
)
2965 if ( m_renameTimer
->IsRunning() )
2966 m_renameTimer
->Stop();
2970 m_renameTimer
= new wxTreeRenameTimer( this );
2973 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
2976 m_lastOnSame
= FALSE
;
2979 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2981 if ( event
.LeftDown() )
2983 m_lastOnSame
= item
== m_current
;
2986 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2988 // only toggle the item for a single click, double click on
2989 // the button doesn't do anything (it toggles the item twice)
2990 if ( event
.LeftDown() )
2995 // don't select the item if the button was clicked
2999 // how should the selection work for this event?
3000 bool is_multiple
, extended_select
, unselect_others
;
3001 EventFlagsToSelType(GetWindowStyleFlag(),
3003 event
.ControlDown(),
3004 is_multiple
, extended_select
, unselect_others
);
3006 SelectItem(item
, unselect_others
, extended_select
);
3008 // For some reason, Windows isn't recognizing a left double-click,
3009 // so we need to simulate it here. Allow 200 milliseconds for now.
3010 if ( event
.LeftDClick() )
3012 // double clicking should not start editing the item label
3013 if ( m_renameTimer
)
3014 m_renameTimer
->Stop();
3016 m_lastOnSame
= FALSE
;
3018 // send activate event first
3019 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3020 nevent
.m_item
= (long) item
;
3021 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3022 nevent
.SetEventObject( this );
3023 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3025 // if the user code didn't process the activate event,
3026 // handle it ourselves by toggling the item when it is
3028 if ( item
->HasPlus() )
3038 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
3040 /* after all changes have been done to the tree control,
3041 * we actually redraw the tree when everything is over */
3043 if (!m_dirty
) return;
3047 CalculatePositions();
3049 AdjustMyScrollbars();
3052 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3057 wxTreeItemAttr
*attr
= item
->GetAttributes();
3058 if ( attr
&& attr
->HasFont() )
3059 dc
.SetFont(attr
->GetFont());
3060 else if ( item
->IsBold() )
3061 dc
.SetFont(m_boldFont
);
3063 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3066 // restore normal font
3067 dc
.SetFont( m_normalFont
);
3071 int image
= item
->GetCurrentImage();
3072 if ( image
!= NO_IMAGE
)
3074 if ( m_imageListNormal
)
3076 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3081 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3084 total_h
+= 2; // at least 2 pixels
3086 total_h
+= total_h
/10; // otherwise 10% extra spacing
3088 item
->SetHeight(total_h
);
3089 if (total_h
>m_lineHeight
)
3090 m_lineHeight
=total_h
;
3092 item
->SetWidth(image_w
+text_w
+2);
3095 // -----------------------------------------------------------------------------
3096 // for developper : y is now the top of the level
3097 // not the middle of it !
3098 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3100 int x
= level
*m_indent
;
3101 if (!HasFlag(wxTR_HIDE_ROOT
))
3105 else if (level
== 0)
3107 // a hidden root is not evaluated, but its
3108 // children are always calculated
3112 CalculateSize( item
, dc
);
3115 item
->SetX( x
+m_spacing
);
3117 y
+= GetLineHeight(item
);
3119 if ( !item
->IsExpanded() )
3121 // we don't need to calculate collapsed branches
3126 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3127 size_t n
, count
= children
.Count();
3129 for (n
= 0; n
< count
; ++n
)
3130 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3133 void wxGenericTreeCtrl::CalculatePositions()
3135 if ( !m_anchor
) return;
3137 wxClientDC
dc(this);
3140 dc
.SetFont( m_normalFont
);
3142 dc
.SetPen( m_dottedPen
);
3143 //if(GetImageList() == NULL)
3144 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3147 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3150 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3152 if (m_dirty
) return;
3154 wxSize client
= GetClientSize();
3157 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3158 rect
.width
= client
.x
;
3159 rect
.height
= client
.y
;
3161 Refresh(TRUE
, &rect
);
3163 AdjustMyScrollbars();
3166 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3168 if (m_dirty
) return;
3171 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3172 rect
.width
= GetClientSize().x
;
3173 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3175 Refresh(TRUE
, &rect
);
3178 void wxGenericTreeCtrl::RefreshSelected()
3180 // TODO: this is awfully inefficient, we should keep the list of all
3181 // selected items internally, should be much faster
3183 RefreshSelectedUnder(m_anchor
);
3186 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3188 if ( item
->IsSelected() )
3191 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3192 size_t count
= children
.GetCount();
3193 for ( size_t n
= 0; n
< count
; n
++ )
3195 RefreshSelectedUnder(children
[n
]);
3199 // ----------------------------------------------------------------------------
3200 // changing colours: we need to refresh the tree control
3201 // ----------------------------------------------------------------------------
3203 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3205 if ( !wxWindow::SetBackgroundColour(colour
) )
3213 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3215 if ( !wxWindow::SetForegroundColour(colour
) )
3223 #endif // wxUSE_TREECTRL