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(),
770 m_normalFont
.GetFaceName(),
771 m_normalFont
.GetEncoding());
774 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
779 const wxValidator
&validator
,
780 const wxString
& name
)
784 wxGetOsVersion( &major
, &minor
);
786 if (style
& wxTR_HAS_BUTTONS
) style
|= wxTR_MAC_BUTTONS
;
787 if (style
& wxTR_HAS_BUTTONS
) style
&= ~wxTR_HAS_BUTTONS
;
788 style
&= ~wxTR_LINES_AT_ROOT
;
789 style
|= wxTR_NO_LINES
;
791 style
|= wxTR_ROW_LINES
;
793 style
|= wxTR_AQUA_BUTTONS
;
796 if (style
& wxTR_AQUA_BUTTONS
)
798 m_arrowRight
= new wxBitmap( aqua_arrow_right
);
799 m_arrowDown
= new wxBitmap( aqua_arrow_down
);
807 wxScrolledWindow::Create( parent
, id
, pos
, size
,
808 style
|wxHSCROLL
|wxVSCROLL
, name
);
810 // If the tree display has no buttons, but does have
811 // connecting lines, we can use a narrower layout.
812 // It may not be a good idea to force this...
813 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
820 SetValidator( validator
);
823 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
824 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
826 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
827 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
832 wxGenericTreeCtrl::~wxGenericTreeCtrl()
834 delete m_hilightBrush
;
835 delete m_hilightUnfocusedBrush
;
842 delete m_renameTimer
;
845 if (m_ownsImageListNormal
)
846 delete m_imageListNormal
;
847 if (m_ownsImageListState
)
848 delete m_imageListState
;
849 if (m_ownsImageListButtons
)
850 delete m_imageListButtons
;
853 // -----------------------------------------------------------------------------
855 // -----------------------------------------------------------------------------
857 size_t wxGenericTreeCtrl::GetCount() const
859 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
862 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
864 m_indent
= (unsigned short) indent
;
868 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
870 m_spacing
= (unsigned short) spacing
;
874 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
876 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
878 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
881 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
883 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
885 // if we will hide the root, make sure children are visible
886 m_anchor
->SetHasPlus();
888 CalculatePositions();
891 // right now, just sets the styles. Eventually, we may
892 // want to update the inherited styles, but right now
893 // none of the parents has updatable styles
894 m_windowStyle
= styles
;
898 // -----------------------------------------------------------------------------
899 // functions to work with tree items
900 // -----------------------------------------------------------------------------
902 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
904 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
906 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
909 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
910 wxTreeItemIcon which
) const
912 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
914 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
917 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
919 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
921 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
924 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
926 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
928 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
929 return pItem
->Attr().GetTextColour();
932 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
934 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
936 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
937 return pItem
->Attr().GetBackgroundColour();
940 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
942 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
944 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
945 return pItem
->Attr().GetFont();
948 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
950 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
953 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
954 pItem
->SetText(text
);
955 CalculateSize(pItem
, dc
);
959 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
961 wxTreeItemIcon which
)
963 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
965 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
966 pItem
->SetImage(image
, which
);
969 CalculateSize(pItem
, dc
);
973 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
975 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
977 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
980 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
982 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
984 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
985 pItem
->SetHasPlus(has
);
989 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
991 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
993 // avoid redrawing the tree if no real change
994 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
995 if ( pItem
->IsBold() != bold
)
997 pItem
->SetBold(bold
);
1002 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1003 const wxColour
& col
)
1005 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1007 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1008 pItem
->Attr().SetTextColour(col
);
1012 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1013 const wxColour
& col
)
1015 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1017 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1018 pItem
->Attr().SetBackgroundColour(col
);
1022 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1024 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1026 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1027 pItem
->Attr().SetFont(font
);
1031 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1033 wxScrolledWindow::SetFont(font
);
1035 m_normalFont
= font
;
1036 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1037 m_normalFont
.GetFamily(),
1038 m_normalFont
.GetStyle(),
1040 m_normalFont
.GetUnderlined(),
1041 m_normalFont
.GetFaceName(),
1042 m_normalFont
.GetEncoding());
1048 // -----------------------------------------------------------------------------
1049 // item status inquiries
1050 // -----------------------------------------------------------------------------
1052 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1054 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1056 // An item is only visible if it's not a descendant of a collapsed item
1057 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1058 wxGenericTreeItem
* parent
= pItem
->GetParent();
1061 if (!parent
->IsExpanded())
1063 parent
= parent
->GetParent();
1067 GetViewStart(& startX
, & startY
);
1069 wxSize clientSize
= GetClientSize();
1072 if (!GetBoundingRect(item
, rect
))
1074 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1076 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1078 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1084 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1086 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1088 // consider that the item does have children if it has the "+" button: it
1089 // might not have them (if it had never been expanded yet) but then it
1090 // could have them as well and it's better to err on this side rather than
1091 // disabling some operations which are restricted to the items with
1092 // children for an item which does have them
1093 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1096 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1098 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1100 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1103 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1105 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1107 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1110 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1112 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1114 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1117 // -----------------------------------------------------------------------------
1119 // -----------------------------------------------------------------------------
1121 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1123 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1125 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1128 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
1130 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1133 return GetNextChild(item
, cookie
);
1136 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
1138 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1140 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1141 if ( (size_t)cookie
< children
.Count() )
1143 return children
.Item((size_t)cookie
++);
1147 // there are no more of them
1148 return wxTreeItemId();
1152 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1154 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1156 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1157 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1160 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1162 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1164 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1165 wxGenericTreeItem
*parent
= i
->GetParent();
1166 if ( parent
== NULL
)
1168 // root item doesn't have any siblings
1169 return wxTreeItemId();
1172 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1173 int index
= siblings
.Index(i
);
1174 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1176 size_t n
= (size_t)(index
+ 1);
1177 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1180 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1182 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1184 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1185 wxGenericTreeItem
*parent
= i
->GetParent();
1186 if ( parent
== NULL
)
1188 // root item doesn't have any siblings
1189 return wxTreeItemId();
1192 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1193 int index
= siblings
.Index(i
);
1194 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1196 return index
== 0 ? wxTreeItemId()
1197 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1200 // Only for internal use right now, but should probably be public
1201 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1203 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1205 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1207 // First see if there are any children.
1208 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1209 if (children
.GetCount() > 0)
1211 return children
.Item(0);
1215 // Try a sibling of this or ancestor instead
1216 wxTreeItemId p
= item
;
1217 wxTreeItemId toFind
;
1220 toFind
= GetNextSibling(p
);
1222 } while (p
.IsOk() && !toFind
.IsOk());
1227 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1229 wxTreeItemId id
= GetRootItem();
1238 } while (id
.IsOk());
1240 return wxTreeItemId();
1243 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1245 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1247 wxTreeItemId id
= item
;
1250 while (id
= GetNext(id
), id
.IsOk())
1256 return wxTreeItemId();
1259 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1261 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1263 wxFAIL_MSG(wxT("not implemented"));
1265 return wxTreeItemId();
1268 // find the first item starting with the given prefix after the given item
1269 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1270 const wxString
& prefixOrig
) const
1272 // match is case insensitive as this is more convenient to the user: having
1273 // to press Shift-letter to go to the item starting with a capital letter
1274 // would be too bothersome
1275 wxString prefix
= prefixOrig
.Lower();
1277 // determine the starting point: we shouldn't take the current item (this
1278 // allows to switch between two items starting with the same letter just by
1279 // pressing it) but we shouldn't jump to the next one if the user is
1280 // continuing to type as otherwise he might easily skip the item he wanted
1281 wxTreeItemId id
= idParent
;
1282 if ( prefix
.length() == 1 )
1287 // look for the item starting with the given prefix after it
1288 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1293 // if we haven't found anything...
1296 // ... wrap to the beginning
1298 if ( HasFlag(wxTR_HIDE_ROOT
) )
1300 // can't select virtual root
1304 // and try all the items (stop when we get to the one we started from)
1305 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1314 // -----------------------------------------------------------------------------
1316 // -----------------------------------------------------------------------------
1318 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1320 const wxString
& text
,
1321 int image
, int selImage
,
1322 wxTreeItemData
*data
)
1324 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1327 // should we give a warning here?
1328 return AddRoot(text
, image
, selImage
, data
);
1331 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1333 wxGenericTreeItem
*item
=
1334 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1338 data
->m_pItem
= (long) item
;
1341 parent
->Insert( item
, previous
);
1346 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1347 int image
, int selImage
,
1348 wxTreeItemData
*data
)
1350 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1352 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1354 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1355 image
, selImage
, data
);
1358 data
->m_pItem
= (long) m_anchor
;
1361 if (HasFlag(wxTR_HIDE_ROOT
))
1363 // if root is hidden, make sure we can navigate
1365 m_anchor
->SetHasPlus();
1367 CalculatePositions();
1370 if (!HasFlag(wxTR_MULTIPLE
))
1372 m_current
= m_key_current
= m_anchor
;
1373 m_current
->SetHilight( TRUE
);
1379 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1380 const wxString
& text
,
1381 int image
, int selImage
,
1382 wxTreeItemData
*data
)
1384 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1387 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1388 const wxTreeItemId
& idPrevious
,
1389 const wxString
& text
,
1390 int image
, int selImage
,
1391 wxTreeItemData
*data
)
1393 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1396 // should we give a warning here?
1397 return AddRoot(text
, image
, selImage
, data
);
1401 if (idPrevious
.IsOk())
1403 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1404 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1405 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1408 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1411 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1413 const wxString
& text
,
1414 int image
, int selImage
,
1415 wxTreeItemData
*data
)
1417 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1420 // should we give a warning here?
1421 return AddRoot(text
, image
, selImage
, data
);
1424 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1427 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1428 const wxString
& text
,
1429 int image
, int selImage
,
1430 wxTreeItemData
*data
)
1432 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1435 // should we give a warning here?
1436 return AddRoot(text
, image
, selImage
, data
);
1439 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1440 image
, selImage
, data
);
1443 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1445 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1446 event
.m_item
= (long) item
;
1447 event
.SetEventObject( this );
1448 ProcessEvent( event
);
1451 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1453 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1455 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1456 item
->DeleteChildren(this);
1459 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1461 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1463 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1465 wxGenericTreeItem
*parent
= item
->GetParent();
1467 // don't keep stale pointers around!
1468 if ( IsDescendantOf(item
, m_key_current
) )
1470 m_key_current
= parent
;
1473 if ( IsDescendantOf(item
, m_current
) )
1478 // remove the item from the tree
1481 parent
->GetChildren().Remove( item
); // remove by value
1483 else // deleting the root
1485 // nothing will be left in the tree
1489 // and delete all of its children and the item itself now
1490 item
->DeleteChildren(this);
1491 SendDeleteEvent(item
);
1495 void wxGenericTreeCtrl::DeleteAllItems()
1503 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1505 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1507 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1508 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1509 _T("can't expand hidden root") );
1511 if ( !item
->HasPlus() )
1514 if ( item
->IsExpanded() )
1517 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1518 event
.m_item
= (long) item
;
1519 event
.SetEventObject( this );
1521 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1523 // cancelled by program
1528 CalculatePositions();
1530 RefreshSubtree(item
);
1532 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1533 ProcessEvent( event
);
1536 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1538 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1541 if ( !IsExpanded(item
) )
1546 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1547 while ( child
.IsOk() )
1551 child
= GetNextChild(item
, cookie
);
1555 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1557 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1558 _T("can't collapse hidden root") );
1560 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1562 if ( !item
->IsExpanded() )
1565 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1566 event
.m_item
= (long) item
;
1567 event
.SetEventObject( this );
1568 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1570 // cancelled by program
1576 #if 0 // TODO why should items be collapsed recursively?
1577 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1578 size_t count
= children
.Count();
1579 for ( size_t n
= 0; n
< count
; n
++ )
1581 Collapse(children
[n
]);
1585 CalculatePositions();
1587 RefreshSubtree(item
);
1589 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1590 ProcessEvent( event
);
1593 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1596 DeleteChildren(item
);
1599 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1601 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1603 if (item
->IsExpanded())
1609 void wxGenericTreeCtrl::Unselect()
1613 m_current
->SetHilight( FALSE
);
1614 RefreshLine( m_current
);
1620 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1622 if (item
->IsSelected())
1624 item
->SetHilight(FALSE
);
1628 if (item
->HasChildren())
1630 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1631 size_t count
= children
.Count();
1632 for ( size_t n
= 0; n
< count
; ++n
)
1634 UnselectAllChildren(children
[n
]);
1639 void wxGenericTreeCtrl::UnselectAll()
1641 wxTreeItemId rootItem
= GetRootItem();
1643 // the tree might not have the root item at all
1646 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1650 // Recursive function !
1651 // To stop we must have crt_item<last_item
1653 // Tag all next children, when no more children,
1654 // Move to parent (not to tag)
1655 // Keep going... if we found last_item, we stop.
1656 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1658 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1660 if (parent
== NULL
) // This is root item
1661 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1663 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1664 int index
= children
.Index(crt_item
);
1665 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1667 size_t count
= children
.Count();
1668 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1670 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1673 return TagNextChildren(parent
, last_item
, select
);
1676 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1678 crt_item
->SetHilight(select
);
1679 RefreshLine(crt_item
);
1681 if (crt_item
==last_item
)
1684 if (crt_item
->HasChildren())
1686 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1687 size_t count
= children
.Count();
1688 for ( size_t n
= 0; n
< count
; ++n
)
1690 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1698 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1700 // item2 is not necessary after item1
1701 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1703 // choice first' and 'last' between item1 and item2
1704 if (item1
->GetY()<item2
->GetY())
1715 bool select
= m_current
->IsSelected();
1717 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1720 TagNextChildren(first
,last
,select
);
1723 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1724 bool unselect_others
,
1725 bool extended_select
)
1727 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1729 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1730 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1732 //wxCHECK_RET( ( (!unselect_others) && is_single),
1733 // wxT("this is a single selection tree") );
1735 // to keep going anyhow !!!
1738 if (item
->IsSelected())
1739 return; // nothing to do
1740 unselect_others
= TRUE
;
1741 extended_select
= FALSE
;
1743 else if ( unselect_others
&& item
->IsSelected() )
1745 // selection change if there is more than one item currently selected
1746 wxArrayTreeItemIds selected_items
;
1747 if ( GetSelections(selected_items
) == 1 )
1751 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1752 event
.m_item
= (long) item
;
1753 event
.m_itemOld
= (long) m_current
;
1754 event
.SetEventObject( this );
1755 // TODO : Here we don't send any selection mode yet !
1757 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1760 wxTreeItemId parent
= GetParent( itemId
);
1761 while (parent
.IsOk())
1763 if (!IsExpanded(parent
))
1766 parent
= GetParent( parent
);
1769 EnsureVisible( itemId
);
1772 if (unselect_others
)
1774 if (is_single
) Unselect(); // to speed up thing
1779 if (extended_select
)
1783 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1786 // don't change the mark (m_current)
1787 SelectItemRange(m_current
, item
);
1791 bool select
=TRUE
; // the default
1793 // Check if we need to toggle hilight (ctrl mode)
1794 if (!unselect_others
)
1795 select
=!item
->IsSelected();
1797 m_current
= m_key_current
= item
;
1798 m_current
->SetHilight(select
);
1799 RefreshLine( m_current
);
1802 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1803 GetEventHandler()->ProcessEvent( event
);
1806 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1807 wxArrayTreeItemIds
&array
) const
1809 if ( item
->IsSelected() )
1810 array
.Add(wxTreeItemId(item
));
1812 if ( item
->HasChildren() )
1814 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1815 size_t count
= children
.GetCount();
1816 for ( size_t n
= 0; n
< count
; ++n
)
1817 FillArray(children
[n
], array
);
1821 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1824 wxTreeItemId idRoot
= GetRootItem();
1825 if ( idRoot
.IsOk() )
1827 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1829 //else: the tree is empty, so no selections
1831 return array
.Count();
1834 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1836 if (!item
.IsOk()) return;
1838 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1840 // first expand all parent branches
1841 wxGenericTreeItem
*parent
= gitem
->GetParent();
1843 if ( HasFlag(wxTR_HIDE_ROOT
) )
1845 while ( parent
!= m_anchor
)
1848 parent
= parent
->GetParent();
1856 parent
= parent
->GetParent();
1860 //if (parent) CalculatePositions();
1865 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1867 if (!item
.IsOk()) return;
1869 // We have to call this here because the label in
1870 // question might just have been added and no screen
1871 // update taken place.
1872 if (m_dirty
) wxYieldIfNeeded();
1874 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1876 // now scroll to the item
1877 int item_y
= gitem
->GetY();
1881 GetViewStart( &start_x
, &start_y
);
1882 start_y
*= PIXELS_PER_UNIT
;
1886 GetClientSize( &client_w
, &client_h
);
1888 if (item_y
< start_y
+3)
1893 m_anchor
->GetSize( x
, y
, this );
1894 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1895 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1896 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1897 // Item should appear at top
1898 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1900 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1905 m_anchor
->GetSize( x
, y
, this );
1906 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1907 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1908 item_y
+= PIXELS_PER_UNIT
+2;
1909 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1910 // Item should appear at bottom
1911 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
);
1915 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1916 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1918 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1919 wxGenericTreeItem
**item2
)
1921 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1923 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1926 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1927 const wxTreeItemId
& item2
)
1929 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1932 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1934 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1936 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1938 wxCHECK_RET( !s_treeBeingSorted
,
1939 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1941 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1942 if ( children
.Count() > 1 )
1946 s_treeBeingSorted
= this;
1947 children
.Sort(tree_ctrl_compare_func
);
1948 s_treeBeingSorted
= NULL
;
1950 //else: don't make the tree dirty as nothing changed
1953 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1955 return m_imageListNormal
;
1958 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1960 return m_imageListButtons
;
1963 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1965 return m_imageListState
;
1968 void wxGenericTreeCtrl::CalculateLineHeight()
1970 wxClientDC
dc(this);
1971 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1973 if ( m_imageListNormal
)
1975 // Calculate a m_lineHeight value from the normal Image sizes.
1976 // May be toggle off. Then wxGenericTreeCtrl will spread when
1977 // necessary (which might look ugly).
1978 int n
= m_imageListNormal
->GetImageCount();
1979 for (int i
= 0; i
< n
; i
++)
1981 int width
= 0, height
= 0;
1982 m_imageListNormal
->GetSize(i
, width
, height
);
1983 if (height
> m_lineHeight
) m_lineHeight
= height
;
1987 if (m_imageListButtons
)
1989 // Calculate a m_lineHeight value from the Button image sizes.
1990 // May be toggle off. Then wxGenericTreeCtrl will spread when
1991 // necessary (which might look ugly).
1992 int n
= m_imageListButtons
->GetImageCount();
1993 for (int i
= 0; i
< n
; i
++)
1995 int width
= 0, height
= 0;
1996 m_imageListButtons
->GetSize(i
, width
, height
);
1997 if (height
> m_lineHeight
) m_lineHeight
= height
;
2001 if (m_lineHeight
< 30)
2002 m_lineHeight
+= 2; // at least 2 pixels
2004 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2007 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2009 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2010 m_imageListNormal
= imageList
;
2011 m_ownsImageListNormal
= FALSE
;
2013 // Don't do any drawing if we're setting the list to NULL,
2014 // since we may be in the process of deleting the tree control.
2016 CalculateLineHeight();
2019 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2021 if (m_ownsImageListState
) delete m_imageListState
;
2022 m_imageListState
= imageList
;
2023 m_ownsImageListState
= FALSE
;
2026 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2028 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2029 m_imageListButtons
= imageList
;
2030 m_ownsImageListButtons
= FALSE
;
2032 CalculateLineHeight();
2035 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2037 SetImageList(imageList
);
2038 m_ownsImageListNormal
= TRUE
;
2041 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2043 SetStateImageList(imageList
);
2044 m_ownsImageListState
= TRUE
;
2047 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2049 SetButtonsImageList(imageList
);
2050 m_ownsImageListButtons
= TRUE
;
2053 // -----------------------------------------------------------------------------
2055 // -----------------------------------------------------------------------------
2057 void wxGenericTreeCtrl::AdjustMyScrollbars()
2062 m_anchor
->GetSize( x
, y
, this );
2063 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2064 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2065 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2066 int y_pos
= GetScrollPos( wxVERTICAL
);
2067 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2071 SetScrollbars( 0, 0, 0, 0 );
2075 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2077 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2078 return item
->GetHeight();
2080 return m_lineHeight
;
2083 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2085 // TODO implement "state" icon on items
2087 wxTreeItemAttr
*attr
= item
->GetAttributes();
2088 if ( attr
&& attr
->HasFont() )
2089 dc
.SetFont(attr
->GetFont());
2090 else if (item
->IsBold())
2091 dc
.SetFont(m_boldFont
);
2093 long text_w
= 0, text_h
= 0;
2094 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2096 int image_h
= 0, image_w
= 0;
2097 int image
= item
->GetCurrentImage();
2098 if ( image
!= NO_IMAGE
)
2100 if ( m_imageListNormal
)
2102 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2111 int total_h
= GetLineHeight(item
);
2113 if ( item
->IsSelected() )
2115 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2120 if ( attr
&& attr
->HasBackgroundColour() )
2121 colBg
= attr
->GetBackgroundColour();
2123 colBg
= m_backgroundColour
;
2124 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2127 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2129 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2133 DoGetPosition(&x
, &y
);
2135 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2139 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2141 // If it's selected, and there's an image, then we should
2142 // take care to leave the area under the image painted in the
2143 // background colour.
2144 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2145 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2149 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2150 item
->GetWidth()+2, total_h
-offset
);
2154 if ( image
!= NO_IMAGE
)
2156 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2157 m_imageListNormal
->Draw( image
, dc
,
2159 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2160 wxIMAGELIST_DRAW_TRANSPARENT
);
2161 dc
.DestroyClippingRegion();
2164 dc
.SetBackgroundMode(wxTRANSPARENT
);
2165 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2166 dc
.DrawText( item
->GetText(),
2167 (wxCoord
)(image_w
+ item
->GetX()),
2168 (wxCoord
)(item
->GetY() + extraH
));
2170 // restore normal font
2171 dc
.SetFont( m_normalFont
);
2174 // Now y stands for the top of the item, whereas it used to stand for middle !
2175 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2177 int x
= level
*m_indent
;
2178 if (!HasFlag(wxTR_HIDE_ROOT
))
2182 else if (level
== 0)
2184 // always expand hidden root
2186 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2187 int count
= children
.Count();
2193 PaintLevel(children
[n
], dc
, 1, y
);
2194 } while (++n
< count
);
2196 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2198 // draw line down to last child
2199 origY
+= GetLineHeight(children
[0])>>1;
2200 oldY
+= GetLineHeight(children
[n
-1])>>1;
2201 dc
.DrawLine(3, origY
, 3, oldY
);
2207 item
->SetX(x
+m_spacing
);
2210 int h
= GetLineHeight(item
);
2212 int y_mid
= y_top
+ (h
>>1);
2215 int exposed_x
= dc
.LogicalToDeviceX(0);
2216 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2218 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2222 // don't draw rect outline if we already have the
2223 // background color under Mac
2224 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2225 #endif // !__WXMAC__
2229 if ( item
->IsSelected() )
2231 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2235 wxTreeItemAttr
*attr
= item
->GetAttributes();
2236 if (attr
&& attr
->HasTextColour())
2237 colText
= attr
->GetTextColour();
2239 colText
= GetForegroundColour();
2243 dc
.SetTextForeground(colText
);
2247 PaintItem(item
, dc
);
2249 if (HasFlag(wxTR_ROW_LINES
))
2251 // if the background colour is white, choose a
2252 // contrasting color for the lines
2253 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2254 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2255 dc
.DrawLine(0, y_top
, 10000, y_top
);
2256 dc
.DrawLine(0, y
, 10000, y
);
2259 // restore DC objects
2260 dc
.SetBrush(*wxWHITE_BRUSH
);
2261 dc
.SetPen(m_dottedPen
);
2262 dc
.SetTextForeground(*wxBLACK
);
2264 if (item
->HasPlus() && HasButtons()) // should the item show a button?
2266 if (!HasFlag(wxTR_NO_LINES
))
2268 if (x
> (signed)m_indent
)
2269 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
2270 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2271 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
2272 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
2275 if (m_imageListButtons
!= NULL
)
2277 // draw the image button here
2278 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
2279 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
2280 if (item
->IsSelected())
2281 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2282 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2283 int xx
= x
- (image_w
>>1);
2284 int yy
= y_mid
- (image_h
>>1);
2285 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
2286 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2287 wxIMAGELIST_DRAW_TRANSPARENT
);
2288 dc
.DestroyClippingRegion();
2290 else if (HasFlag(wxTR_TWIST_BUTTONS
))
2292 // draw the twisty button here
2294 if (HasFlag(wxTR_AQUA_BUTTONS
))
2296 if (item
->IsExpanded())
2297 dc
.DrawBitmap( *m_arrowDown
, x
-5, y_mid
-6, TRUE
);
2299 dc
.DrawBitmap( *m_arrowRight
, x
-5, y_mid
-6, TRUE
);
2303 dc
.SetBrush(*m_hilightBrush
);
2304 dc
.SetPen(*wxBLACK_PEN
);
2307 if (item
->IsExpanded())
2310 button
[0].y
= y_mid
-2;
2312 button
[1].y
= y_mid
-2;
2314 button
[2].y
= y_mid
+3;
2318 button
[0].y
= y_mid
-5;
2320 button
[1].y
= y_mid
+5;
2322 button
[2].y
= y_mid
;
2325 dc
.DrawPolygon(3, button
);
2326 dc
.SetPen(m_dottedPen
);
2329 else // if (HasFlag(wxTR_HAS_BUTTONS))
2331 // draw the plus sign here
2332 dc
.SetPen(*wxGREY_PEN
);
2333 dc
.SetBrush(*wxWHITE_BRUSH
);
2334 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2335 dc
.SetPen(*wxBLACK_PEN
);
2336 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2337 if (!item
->IsExpanded())
2338 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2339 dc
.SetPen(m_dottedPen
);
2342 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2344 // draw the horizontal line here
2346 if (x
> (signed)m_indent
)
2347 x_start
-= m_indent
;
2348 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2350 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2354 if (item
->IsExpanded())
2356 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2357 int count
= children
.Count();
2364 PaintLevel(children
[n
], dc
, level
, y
);
2365 } while (++n
< count
);
2367 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2369 // draw line down to last child
2370 oldY
+= GetLineHeight(children
[n
-1])>>1;
2371 if (HasButtons()) y_mid
+= 5;
2372 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2378 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2382 if ( item
->HasPlus() )
2384 // it's a folder, indicate it by a border
2389 // draw a line under the drop target because the item will be
2391 DrawLine(item
, TRUE
/* below */);
2394 SetCursor(wxCURSOR_BULLSEYE
);
2399 SetCursor(wxCURSOR_NO_ENTRY
);
2403 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2405 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2407 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2409 wxClientDC
dc(this);
2411 dc
.SetLogicalFunction(wxINVERT
);
2412 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2414 int w
= i
->GetWidth() + 2;
2415 int h
= GetLineHeight(i
) + 2;
2417 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2420 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2422 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2424 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2426 wxClientDC
dc(this);
2428 dc
.SetLogicalFunction(wxINVERT
);
2434 y
+= GetLineHeight(i
) - 1;
2437 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2440 // -----------------------------------------------------------------------------
2441 // wxWindows callbacks
2442 // -----------------------------------------------------------------------------
2444 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2452 dc
.SetFont( m_normalFont
);
2453 dc
.SetPen( m_dottedPen
);
2455 // this is now done dynamically
2456 //if(GetImageList() == NULL)
2457 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2460 PaintLevel( m_anchor
, dc
, 0, y
);
2463 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2472 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2481 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2483 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2484 te
.m_evtKey
= event
;
2485 te
.SetEventObject( this );
2486 if ( GetEventHandler()->ProcessEvent( te
) )
2488 // intercepted by the user code
2492 if ( (m_current
== 0) || (m_key_current
== 0) )
2498 // how should the selection work for this event?
2499 bool is_multiple
, extended_select
, unselect_others
;
2500 EventFlagsToSelType(GetWindowStyleFlag(),
2502 event
.ControlDown(),
2503 is_multiple
, extended_select
, unselect_others
);
2507 // * : Expand all/Collapse all
2508 // ' ' | return : activate
2509 // up : go up (not last children!)
2511 // left : go to parent
2512 // right : open if parent and go next
2513 // home : go to root
2514 // end : go to last item without opening parents
2515 // alnum : start or continue searching for the item with this prefix
2516 int keyCode
= event
.KeyCode();
2521 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2529 if ( !IsExpanded(m_current
) )
2532 ExpandAll(m_current
);
2535 //else: fall through to Collapse() it
2539 if (IsExpanded(m_current
))
2541 Collapse(m_current
);
2547 if ( !event
.HasModifiers() )
2549 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2550 event
.m_item
= (long) m_current
;
2551 event
.SetEventObject( this );
2552 GetEventHandler()->ProcessEvent( event
);
2555 // in any case, also generate the normal key event for this key,
2556 // even if we generated the ACTIVATED event above: this is what
2557 // wxMSW does and it makes sense because you might not want to
2558 // process ACTIVATED event at all and handle Space and Return
2559 // directly (and differently) which would be impossible otherwise
2563 // up goes to the previous sibling or to the last
2564 // of its children if it's expanded
2567 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2570 prev
= GetParent( m_key_current
);
2571 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2573 break; // don't go to root if it is hidden
2578 wxTreeItemId current
= m_key_current
;
2579 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2580 if (current
== GetFirstChild( prev
, cookie
))
2582 // otherwise we return to where we came from
2583 SelectItem( prev
, unselect_others
, extended_select
);
2584 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2591 while ( IsExpanded(prev
) && HasChildren(prev
) )
2593 wxTreeItemId child
= GetLastChild(prev
);
2600 SelectItem( prev
, unselect_others
, extended_select
);
2601 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2606 // left arrow goes to the parent
2609 wxTreeItemId prev
= GetParent( m_current
);
2610 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2612 // don't go to root if it is hidden
2613 prev
= GetPrevSibling( m_current
);
2617 SelectItem( prev
, unselect_others
, extended_select
);
2623 // this works the same as the down arrow except that we
2624 // also expand the item if it wasn't expanded yet
2630 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2633 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2634 SelectItem( child
, unselect_others
, extended_select
);
2635 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2639 wxTreeItemId next
= GetNextSibling( m_key_current
);
2642 wxTreeItemId current
= m_key_current
;
2643 while (current
&& !next
)
2645 current
= GetParent( current
);
2646 if (current
) next
= GetNextSibling( current
);
2651 SelectItem( next
, unselect_others
, extended_select
);
2652 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2658 // <End> selects the last visible tree item
2661 wxTreeItemId last
= GetRootItem();
2663 while ( last
.IsOk() && IsExpanded(last
) )
2665 wxTreeItemId lastChild
= GetLastChild(last
);
2667 // it may happen if the item was expanded but then all of
2668 // its children have been deleted - so IsExpanded() returned
2669 // TRUE, but GetLastChild() returned invalid item
2678 SelectItem( last
, unselect_others
, extended_select
);
2683 // <Home> selects the root item
2686 wxTreeItemId prev
= GetRootItem();
2690 if ( HasFlag(wxTR_HIDE_ROOT
) )
2693 prev
= GetFirstChild(prev
, dummy
);
2698 SelectItem( prev
, unselect_others
, extended_select
);
2703 // do not use wxIsalnum() here
2704 if ( !event
.HasModifiers() &&
2705 ((keyCode
>= '0' && keyCode
<= '9') ||
2706 (keyCode
>= 'a' && keyCode
<= 'z') ||
2707 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2709 // find the next item starting with the given prefix
2710 char ch
= (char)keyCode
;
2712 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2723 // also start the timer to reset the current prefix if the user
2724 // doesn't press any more alnum keys soon -- we wouldn't want
2725 // to use this prefix for a new item search
2728 m_findTimer
= new wxTreeFindTimer(this);
2731 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2740 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2742 // JACS: removed wxYieldIfNeeded() because it can cause the window
2743 // to be deleted from under us if a close window event is pending
2748 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2749 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2750 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2751 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2752 if (flags
) return wxTreeItemId();
2754 if (m_anchor
== NULL
)
2756 flags
= wxTREE_HITTEST_NOWHERE
;
2757 return wxTreeItemId();
2760 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2764 flags
= wxTREE_HITTEST_NOWHERE
;
2765 return wxTreeItemId();
2770 // get the bounding rectangle of the item (or of its label only)
2771 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2773 bool WXUNUSED(textOnly
)) const
2775 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2777 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2780 GetViewStart(& startX
, & startY
);
2782 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2783 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2784 rect
.width
= i
->GetWidth();
2785 //rect.height = i->GetHeight();
2786 rect
.height
= GetLineHeight(i
);
2791 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2793 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2795 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2797 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2798 te
.m_item
= (long) itemEdit
;
2799 te
.SetEventObject( this );
2800 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2806 // We have to call this here because the label in
2807 // question might just have been added and no screen
2808 // update taken place.
2812 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, itemEdit
);
2817 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2818 const wxString
& value
)
2820 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2821 le
.m_item
= (long) item
;
2822 le
.SetEventObject( this );
2824 le
.m_editCancelled
= FALSE
;
2826 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2829 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2831 // let owner know that the edit was cancelled
2832 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2833 le
.m_item
= (long) item
;
2834 le
.SetEventObject( this );
2835 le
.m_label
= wxEmptyString
;
2836 le
.m_editCancelled
= FALSE
;
2838 GetEventHandler()->ProcessEvent( le
);
2844 void wxGenericTreeCtrl::OnRenameTimer()
2849 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2851 if ( !m_anchor
) return;
2853 // we process left mouse up event (enables in-place edit), right down
2854 // (pass to the user code), left dbl click (activate item) and
2855 // dragging/moving events for items drag-and-drop
2856 if ( !(event
.LeftDown() ||
2858 event
.RightDown() ||
2859 event
.LeftDClick() ||
2861 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2868 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2871 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2873 if ( event
.Dragging() && !m_isDragging
)
2875 if (m_dragCount
== 0)
2880 if (m_dragCount
!= 3)
2882 // wait until user drags a bit further...
2886 wxEventType command
= event
.RightIsDown()
2887 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2888 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2890 wxTreeEvent
nevent( command
, GetId() );
2891 nevent
.m_item
= (long) m_current
;
2892 nevent
.SetEventObject(this);
2894 // by default the dragging is not supported, the user code must
2895 // explicitly allow the event for it to take place
2898 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2900 // we're going to drag this item
2901 m_isDragging
= TRUE
;
2903 // remember the old cursor because we will change it while
2905 m_oldCursor
= m_cursor
;
2907 // in a single selection control, hide the selection temporarily
2908 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2910 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2912 if ( m_oldSelection
)
2914 m_oldSelection
->SetHilight(FALSE
);
2915 RefreshLine(m_oldSelection
);
2922 else if ( event
.Moving() )
2924 if ( item
!= m_dropTarget
)
2926 // unhighlight the previous drop target
2927 DrawDropEffect(m_dropTarget
);
2929 m_dropTarget
= item
;
2931 // highlight the current drop target if any
2932 DrawDropEffect(m_dropTarget
);
2937 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2939 // erase the highlighting
2940 DrawDropEffect(m_dropTarget
);
2942 if ( m_oldSelection
)
2944 m_oldSelection
->SetHilight(TRUE
);
2945 RefreshLine(m_oldSelection
);
2946 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2949 // generate the drag end event
2950 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2952 event
.m_item
= (long) item
;
2953 event
.m_pointDrag
= pt
;
2954 event
.SetEventObject(this);
2956 (void)GetEventHandler()->ProcessEvent(event
);
2958 m_isDragging
= FALSE
;
2959 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2963 SetCursor(m_oldCursor
);
2969 // here we process only the messages which happen on tree items
2973 if (item
== NULL
) return; /* we hit the blank area */
2975 if ( event
.RightDown() )
2977 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2978 nevent
.m_item
= (long) item
;
2979 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
2980 nevent
.SetEventObject(this);
2981 GetEventHandler()->ProcessEvent(nevent
);
2983 else if ( event
.LeftUp() )
2987 if ( (item
== m_current
) &&
2988 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2989 HasFlag(wxTR_EDIT_LABELS
) )
2991 if ( m_renameTimer
)
2993 if ( m_renameTimer
->IsRunning() )
2994 m_renameTimer
->Stop();
2998 m_renameTimer
= new wxTreeRenameTimer( this );
3001 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
3004 m_lastOnSame
= FALSE
;
3007 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3009 if ( event
.LeftDown() )
3011 m_lastOnSame
= item
== m_current
;
3014 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3016 // only toggle the item for a single click, double click on
3017 // the button doesn't do anything (it toggles the item twice)
3018 if ( event
.LeftDown() )
3023 // don't select the item if the button was clicked
3027 // how should the selection work for this event?
3028 bool is_multiple
, extended_select
, unselect_others
;
3029 EventFlagsToSelType(GetWindowStyleFlag(),
3031 event
.ControlDown(),
3032 is_multiple
, extended_select
, unselect_others
);
3034 SelectItem(item
, unselect_others
, extended_select
);
3036 // For some reason, Windows isn't recognizing a left double-click,
3037 // so we need to simulate it here. Allow 200 milliseconds for now.
3038 if ( event
.LeftDClick() )
3040 // double clicking should not start editing the item label
3041 if ( m_renameTimer
)
3042 m_renameTimer
->Stop();
3044 m_lastOnSame
= FALSE
;
3046 // send activate event first
3047 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3048 nevent
.m_item
= (long) item
;
3049 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3050 nevent
.SetEventObject( this );
3051 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3053 // if the user code didn't process the activate event,
3054 // handle it ourselves by toggling the item when it is
3056 if ( item
->HasPlus() )
3066 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
3068 /* after all changes have been done to the tree control,
3069 * we actually redraw the tree when everything is over */
3071 if (!m_dirty
) return;
3075 CalculatePositions();
3077 AdjustMyScrollbars();
3080 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3085 wxTreeItemAttr
*attr
= item
->GetAttributes();
3086 if ( attr
&& attr
->HasFont() )
3087 dc
.SetFont(attr
->GetFont());
3088 else if ( item
->IsBold() )
3089 dc
.SetFont(m_boldFont
);
3091 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3094 // restore normal font
3095 dc
.SetFont( m_normalFont
);
3099 int image
= item
->GetCurrentImage();
3100 if ( image
!= NO_IMAGE
)
3102 if ( m_imageListNormal
)
3104 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3109 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3112 total_h
+= 2; // at least 2 pixels
3114 total_h
+= total_h
/10; // otherwise 10% extra spacing
3116 item
->SetHeight(total_h
);
3117 if (total_h
>m_lineHeight
)
3118 m_lineHeight
=total_h
;
3120 item
->SetWidth(image_w
+text_w
+2);
3123 // -----------------------------------------------------------------------------
3124 // for developper : y is now the top of the level
3125 // not the middle of it !
3126 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3128 int x
= level
*m_indent
;
3129 if (!HasFlag(wxTR_HIDE_ROOT
))
3133 else if (level
== 0)
3135 // a hidden root is not evaluated, but its
3136 // children are always calculated
3140 CalculateSize( item
, dc
);
3143 item
->SetX( x
+m_spacing
);
3145 y
+= GetLineHeight(item
);
3147 if ( !item
->IsExpanded() )
3149 // we don't need to calculate collapsed branches
3154 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3155 size_t n
, count
= children
.Count();
3157 for (n
= 0; n
< count
; ++n
)
3158 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3161 void wxGenericTreeCtrl::CalculatePositions()
3163 if ( !m_anchor
) return;
3165 wxClientDC
dc(this);
3168 dc
.SetFont( m_normalFont
);
3170 dc
.SetPen( m_dottedPen
);
3171 //if(GetImageList() == NULL)
3172 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3175 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3178 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3180 if (m_dirty
) return;
3182 wxSize client
= GetClientSize();
3185 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3186 rect
.width
= client
.x
;
3187 rect
.height
= client
.y
;
3189 Refresh(TRUE
, &rect
);
3191 AdjustMyScrollbars();
3194 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3196 if (m_dirty
) return;
3199 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3200 rect
.width
= GetClientSize().x
;
3201 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3203 Refresh(TRUE
, &rect
);
3206 void wxGenericTreeCtrl::RefreshSelected()
3208 // TODO: this is awfully inefficient, we should keep the list of all
3209 // selected items internally, should be much faster
3211 RefreshSelectedUnder(m_anchor
);
3214 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3216 if ( item
->IsSelected() )
3219 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3220 size_t count
= children
.GetCount();
3221 for ( size_t n
= 0; n
< count
; n
++ )
3223 RefreshSelectedUnder(children
[n
]);
3227 // ----------------------------------------------------------------------------
3228 // changing colours: we need to refresh the tree control
3229 // ----------------------------------------------------------------------------
3231 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3233 if ( !wxWindow::SetBackgroundColour(colour
) )
3241 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3243 if ( !wxWindow::SetForegroundColour(colour
) )
3251 #endif // wxUSE_TREECTRL