1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
8 // Copyright: (c) 1998 Robert Roebling and Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
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"
42 #include "wx/mac/private.h"
45 // -----------------------------------------------------------------------------
47 // -----------------------------------------------------------------------------
49 class WXDLLEXPORT wxGenericTreeItem
;
51 WX_DEFINE_EXPORTED_ARRAY(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 static const int NO_IMAGE
= -1;
59 static const int PIXELS_PER_UNIT
= 10;
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
66 static const char *aqua_arrow_right
[] = {
67 /* columns rows colors chars-per-pixel */
88 static const char *aqua_arrow_down
[] = {
89 /* columns rows colors chars-per-pixel */
109 // -----------------------------------------------------------------------------
111 // -----------------------------------------------------------------------------
113 // timer used for enabling in-place edit
114 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
117 // start editing the current item after half a second (if the mouse hasn't
118 // been clicked/moved)
119 enum { DELAY
= 500 };
121 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
123 virtual void Notify();
126 wxGenericTreeCtrl
*m_owner
;
128 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
131 // control used for in-place edit
132 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
135 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
138 void OnChar( wxKeyEvent
&event
);
139 void OnKeyUp( wxKeyEvent
&event
);
140 void OnKillFocus( wxFocusEvent
&event
);
142 bool AcceptChanges();
146 wxGenericTreeCtrl
*m_owner
;
147 wxGenericTreeItem
*m_itemEdited
;
148 wxString m_startValue
;
151 DECLARE_EVENT_TABLE()
152 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
155 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
156 // for a sufficiently long time
157 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
160 // reset the current prefix after half a second of inactivity
161 enum { DELAY
= 500 };
163 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
165 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
168 wxGenericTreeCtrl
*m_owner
;
170 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
174 class WXDLLEXPORT wxGenericTreeItem
178 wxGenericTreeItem() { m_data
= NULL
; }
179 wxGenericTreeItem( wxGenericTreeItem
*parent
,
180 const wxString
& text
,
183 wxTreeItemData
*data
);
185 ~wxGenericTreeItem();
188 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
190 const wxString
& GetText() const { return m_text
; }
191 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
192 { return m_images
[which
]; }
193 wxTreeItemData
*GetData() const { return m_data
; }
195 // returns the current image for the item (depending on its
196 // selected/expanded/whatever state)
197 int GetCurrentImage() const;
199 void SetText( const wxString
&text
);
200 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
201 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
203 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
205 void SetBold(bool bold
) { m_isBold
= bold
; }
207 int GetX() const { return m_x
; }
208 int GetY() const { return m_y
; }
210 void SetX(int x
) { m_x
= x
; }
211 void SetY(int y
) { m_y
= y
; }
213 int GetHeight() const { return m_height
; }
214 int GetWidth() const { return m_width
; }
216 void SetHeight(int h
) { m_height
= h
; }
217 void SetWidth(int w
) { m_width
= w
; }
219 wxGenericTreeItem
*GetParent() const { return m_parent
; }
222 // deletes all children notifying the treectrl about it if !NULL
224 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
226 // get count of all children (and grand children if 'recursively')
227 size_t GetChildrenCount(bool recursively
= TRUE
) const;
229 void Insert(wxGenericTreeItem
*child
, size_t index
)
230 { m_children
.Insert(child
, index
); }
232 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
234 // return the item at given position (or NULL if no item), onButton is
235 // TRUE if the point belongs to the item's button, otherwise it lies
236 // on the button's label
237 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
238 const wxGenericTreeCtrl
*,
242 void Expand() { m_isCollapsed
= FALSE
; }
243 void Collapse() { m_isCollapsed
= TRUE
; }
245 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
248 bool HasChildren() const { return !m_children
.IsEmpty(); }
249 bool IsSelected() const { return m_hasHilight
!= 0; }
250 bool IsExpanded() const { return !m_isCollapsed
; }
251 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
252 bool IsBold() const { return m_isBold
!= 0; }
255 // get them - may be NULL
256 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
257 // get them ensuring that the pointer is not NULL
258 wxTreeItemAttr
& Attr()
262 m_attr
= new wxTreeItemAttr
;
268 void SetAttributes(wxTreeItemAttr
*attr
)
270 if ( m_ownsAttr
) delete m_attr
;
274 // set them and delete when done
275 void AssignAttributes(wxTreeItemAttr
*attr
)
282 // since there can be very many of these, we save size by chosing
283 // the smallest representation for the elements and by ordering
284 // the members to avoid padding.
285 wxString m_text
; // label to be rendered for item
287 wxTreeItemData
*m_data
; // user-provided data
289 wxArrayGenericTreeItems m_children
; // list of children
290 wxGenericTreeItem
*m_parent
; // parent of this item
292 wxTreeItemAttr
*m_attr
; // attributes???
294 // tree ctrl images for the normal, selected, expanded and
295 // expanded+selected states
296 short m_images
[wxTreeItemIcon_Max
];
298 wxCoord m_x
; // (virtual) offset from top
299 wxCoord m_y
; // (virtual) offset from left
300 short m_width
; // width of this item
301 unsigned char m_height
; // height of this item
303 // use bitfields to save size
304 int m_isCollapsed
:1;
305 int m_hasHilight
:1; // same as focused
306 int m_hasPlus
:1; // used for item which doesn't have
307 // children but has a [+] button
308 int m_isBold
:1; // render the label in bold font
309 int m_ownsAttr
:1; // delete attribute when done
311 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
314 // =============================================================================
316 // =============================================================================
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 // translate the key or mouse event flags to the type of selection we're
324 static void EventFlagsToSelType(long style
,
328 bool &extended_select
,
329 bool &unselect_others
)
331 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
332 extended_select
= shiftDown
&& is_multiple
;
333 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
336 // check if the given item is under another one
337 static bool IsDescendantOf(wxGenericTreeItem
*parent
, wxGenericTreeItem
*item
)
341 if ( item
== parent
)
343 // item is a descendant of parent
347 item
= item
->GetParent();
353 // -----------------------------------------------------------------------------
354 // wxTreeRenameTimer (internal)
355 // -----------------------------------------------------------------------------
357 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
362 void wxTreeRenameTimer::Notify()
364 m_owner
->OnRenameTimer();
367 //-----------------------------------------------------------------------------
368 // wxTreeTextCtrl (internal)
369 //-----------------------------------------------------------------------------
371 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
372 EVT_CHAR (wxTreeTextCtrl::OnChar
)
373 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
374 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
377 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
378 wxGenericTreeItem
*item
)
379 : m_itemEdited(item
), m_startValue(item
->GetText())
384 int w
= m_itemEdited
->GetWidth(),
385 h
= m_itemEdited
->GetHeight();
388 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
393 int image
= item
->GetCurrentImage();
394 if ( image
!= NO_IMAGE
)
396 if ( m_owner
->m_imageListNormal
)
398 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
403 wxFAIL_MSG(_T("you must create an image list to use images!"));
407 // FIXME: what are all these hardcoded 4, 8 and 11s really?
411 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
412 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
415 bool wxTreeTextCtrl::AcceptChanges()
417 const wxString value
= GetValue();
419 if ( value
== m_startValue
)
421 // nothing changed, always accept
425 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
427 // vetoed by the user
431 // accepted, do rename the item
432 m_owner
->SetItemText(m_itemEdited
, value
);
437 void wxTreeTextCtrl::Finish()
441 m_owner
->ResetTextControl();
443 wxPendingDelete
.Append(this);
447 m_owner
->SetFocus(); // This doesn't work. TODO.
451 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
453 switch ( event
.m_keyCode
)
456 if ( !AcceptChanges() )
458 // vetoed by the user, don't disappear
465 m_owner
->OnRenameCancelled(m_itemEdited
);
473 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
477 // auto-grow the textctrl:
478 wxSize parentSize
= m_owner
->GetSize();
479 wxPoint myPos
= GetPosition();
480 wxSize mySize
= GetSize();
482 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
483 if (myPos
.x
+ sx
> parentSize
.x
)
484 sx
= parentSize
.x
- myPos
.x
;
493 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
501 if ( AcceptChanges() )
507 // -----------------------------------------------------------------------------
509 // -----------------------------------------------------------------------------
511 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
512 const wxString
& text
,
513 int image
, int selImage
,
514 wxTreeItemData
*data
)
517 m_images
[wxTreeItemIcon_Normal
] = image
;
518 m_images
[wxTreeItemIcon_Selected
] = selImage
;
519 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
520 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
525 m_isCollapsed
= TRUE
;
526 m_hasHilight
= FALSE
;
532 m_attr
= (wxTreeItemAttr
*)NULL
;
535 // We don't know the height here yet.
540 wxGenericTreeItem::~wxGenericTreeItem()
544 if (m_ownsAttr
) delete m_attr
;
546 wxASSERT_MSG( m_children
.IsEmpty(),
547 wxT("please call DeleteChildren() before deleting the item") );
550 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
552 size_t count
= m_children
.Count();
553 for ( size_t n
= 0; n
< count
; n
++ )
555 wxGenericTreeItem
*child
= m_children
[n
];
557 tree
->SendDeleteEvent(child
);
559 child
->DeleteChildren(tree
);
566 void wxGenericTreeItem::SetText( const wxString
&text
)
571 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
573 size_t count
= m_children
.Count();
577 size_t total
= count
;
578 for (size_t n
= 0; n
< count
; ++n
)
580 total
+= m_children
[n
]->GetChildrenCount();
586 void wxGenericTreeItem::GetSize( int &x
, int &y
,
587 const wxGenericTreeCtrl
*theButton
)
589 int bottomY
=m_y
+theButton
->GetLineHeight(this);
590 if ( y
< bottomY
) y
= bottomY
;
591 int width
= m_x
+ m_width
;
592 if ( x
< width
) x
= width
;
596 size_t count
= m_children
.Count();
597 for ( size_t n
= 0; n
< count
; ++n
)
599 m_children
[n
]->GetSize( x
, y
, theButton
);
604 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
605 const wxGenericTreeCtrl
*theCtrl
,
609 // for a hidden root node, don't evaluate it, but do evaluate children
610 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
613 int h
= theCtrl
->GetLineHeight(this);
614 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
616 int y_mid
= m_y
+ h
/2;
617 if (point
.y
< y_mid
)
618 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
620 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
622 // 5 is the size of the plus sign
623 int xCross
= m_x
- theCtrl
->GetSpacing();
624 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
625 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
626 HasPlus() && theCtrl
->HasButtons() )
628 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
632 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
637 // assuming every image (normal and selected) has the same size!
638 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
639 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
642 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
643 flags
|= wxTREE_HITTEST_ONITEMICON
;
645 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
651 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
652 if (point
.x
> m_x
+m_width
)
653 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
658 // if children are expanded, fall through to evaluate them
659 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
663 size_t count
= m_children
.Count();
664 for ( size_t n
= 0; n
< count
; n
++ )
666 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
674 return (wxGenericTreeItem
*) NULL
;
677 int wxGenericTreeItem::GetCurrentImage() const
679 int image
= NO_IMAGE
;
684 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
687 if ( image
== NO_IMAGE
)
689 // we usually fall back to the normal item, but try just the
690 // expanded one (and not selected) first in this case
691 image
= GetImage(wxTreeItemIcon_Expanded
);
697 image
= GetImage(wxTreeItemIcon_Selected
);
700 // maybe it doesn't have the specific image we want,
701 // try the default one instead
702 if ( image
== NO_IMAGE
) image
= GetImage();
707 // -----------------------------------------------------------------------------
708 // wxGenericTreeCtrl implementation
709 // -----------------------------------------------------------------------------
711 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
713 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
714 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
715 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
716 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
717 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
718 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
719 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
722 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
724 * wxTreeCtrl has to be a real class or we have problems with
725 * the run-time information.
728 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
731 // -----------------------------------------------------------------------------
732 // construction/destruction
733 // -----------------------------------------------------------------------------
735 void wxGenericTreeCtrl::Init()
737 m_current
= m_key_current
= m_anchor
= m_select_me
= (wxGenericTreeItem
*) NULL
;
745 m_hilightBrush
= new wxBrush
747 wxSystemSettings::GetColour
749 wxSYS_COLOUR_HIGHLIGHT
754 m_hilightUnfocusedBrush
= new wxBrush
756 wxSystemSettings::GetColour
758 wxSYS_COLOUR_BTNSHADOW
763 m_imageListNormal
= m_imageListButtons
=
764 m_imageListState
= (wxImageList
*) NULL
;
765 m_ownsImageListNormal
= m_ownsImageListButtons
=
766 m_ownsImageListState
= FALSE
;
769 m_isDragging
= FALSE
;
770 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
773 m_renameTimer
= NULL
;
776 m_lastOnSame
= FALSE
;
778 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
779 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
780 m_normalFont
.GetFamily(),
781 m_normalFont
.GetStyle(),
783 m_normalFont
.GetUnderlined(),
784 m_normalFont
.GetFaceName(),
785 m_normalFont
.GetEncoding());
788 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
793 const wxValidator
&validator
,
794 const wxString
& name
)
798 wxGetOsVersion( &major
, &minor
);
800 if (style
& wxTR_HAS_BUTTONS
) style
|= wxTR_MAC_BUTTONS
;
801 if (style
& wxTR_HAS_BUTTONS
) style
&= ~wxTR_HAS_BUTTONS
;
802 style
&= ~wxTR_LINES_AT_ROOT
;
803 style
|= wxTR_NO_LINES
;
805 style
|= wxTR_ROW_LINES
;
807 style
|= wxTR_AQUA_BUTTONS
;
810 if (style
& wxTR_AQUA_BUTTONS
)
812 m_arrowRight
= new wxBitmap( aqua_arrow_right
);
813 m_arrowDown
= new wxBitmap( aqua_arrow_down
);
821 wxScrolledWindow::Create( parent
, id
, pos
, size
,
822 style
|wxHSCROLL
|wxVSCROLL
, name
);
824 // If the tree display has no buttons, but does have
825 // connecting lines, we can use a narrower layout.
826 // It may not be a good idea to force this...
827 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
834 SetValidator( validator
);
837 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
838 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
840 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
841 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
846 wxGenericTreeCtrl::~wxGenericTreeCtrl()
848 delete m_hilightBrush
;
849 delete m_hilightUnfocusedBrush
;
856 delete m_renameTimer
;
859 if (m_ownsImageListNormal
)
860 delete m_imageListNormal
;
861 if (m_ownsImageListState
)
862 delete m_imageListState
;
863 if (m_ownsImageListButtons
)
864 delete m_imageListButtons
;
867 // -----------------------------------------------------------------------------
869 // -----------------------------------------------------------------------------
871 size_t wxGenericTreeCtrl::GetCount() const
873 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
876 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
878 m_indent
= (unsigned short) indent
;
882 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
884 m_spacing
= (unsigned short) spacing
;
888 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
890 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
892 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
895 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
897 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
899 // if we will hide the root, make sure children are visible
900 m_anchor
->SetHasPlus();
902 CalculatePositions();
905 // right now, just sets the styles. Eventually, we may
906 // want to update the inherited styles, but right now
907 // none of the parents has updatable styles
908 m_windowStyle
= styles
;
912 // -----------------------------------------------------------------------------
913 // functions to work with tree items
914 // -----------------------------------------------------------------------------
916 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
918 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
920 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
923 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
924 wxTreeItemIcon which
) const
926 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
928 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
931 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
933 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
935 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
938 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
940 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
942 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
943 return pItem
->Attr().GetTextColour();
946 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
948 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
950 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
951 return pItem
->Attr().GetBackgroundColour();
954 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
956 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
958 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
959 return pItem
->Attr().GetFont();
962 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
964 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
967 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
968 pItem
->SetText(text
);
969 CalculateSize(pItem
, dc
);
973 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
975 wxTreeItemIcon which
)
977 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
979 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
980 pItem
->SetImage(image
, which
);
983 CalculateSize(pItem
, dc
);
987 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
989 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
991 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
994 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
996 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
998 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
999 pItem
->SetHasPlus(has
);
1003 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1005 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1007 // avoid redrawing the tree if no real change
1008 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1009 if ( pItem
->IsBold() != bold
)
1011 pItem
->SetBold(bold
);
1016 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1017 const wxColour
& col
)
1019 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1021 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1022 pItem
->Attr().SetTextColour(col
);
1026 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1027 const wxColour
& col
)
1029 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1031 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1032 pItem
->Attr().SetBackgroundColour(col
);
1036 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1038 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1040 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1041 pItem
->Attr().SetFont(font
);
1045 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1047 wxScrolledWindow::SetFont(font
);
1049 m_normalFont
= font
;
1050 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1051 m_normalFont
.GetFamily(),
1052 m_normalFont
.GetStyle(),
1054 m_normalFont
.GetUnderlined(),
1055 m_normalFont
.GetFaceName(),
1056 m_normalFont
.GetEncoding());
1062 // -----------------------------------------------------------------------------
1063 // item status inquiries
1064 // -----------------------------------------------------------------------------
1066 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1068 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1070 // An item is only visible if it's not a descendant of a collapsed item
1071 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1072 wxGenericTreeItem
* parent
= pItem
->GetParent();
1075 if (!parent
->IsExpanded())
1077 parent
= parent
->GetParent();
1081 GetViewStart(& startX
, & startY
);
1083 wxSize clientSize
= GetClientSize();
1086 if (!GetBoundingRect(item
, rect
))
1088 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1090 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1092 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1098 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1100 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1102 // consider that the item does have children if it has the "+" button: it
1103 // might not have them (if it had never been expanded yet) but then it
1104 // could have them as well and it's better to err on this side rather than
1105 // disabling some operations which are restricted to the items with
1106 // children for an item which does have them
1107 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1110 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1112 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1114 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1117 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1119 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1121 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1124 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1126 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1128 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1131 // -----------------------------------------------------------------------------
1133 // -----------------------------------------------------------------------------
1135 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1137 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1139 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1142 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
1144 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1147 return GetNextChild(item
, cookie
);
1150 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
1152 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1154 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1155 if ( (size_t)cookie
< children
.Count() )
1157 return children
.Item((size_t)cookie
++);
1161 // there are no more of them
1162 return wxTreeItemId();
1166 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1168 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1170 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1171 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1174 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1176 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1178 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1179 wxGenericTreeItem
*parent
= i
->GetParent();
1180 if ( parent
== NULL
)
1182 // root item doesn't have any siblings
1183 return wxTreeItemId();
1186 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1187 int index
= siblings
.Index(i
);
1188 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1190 size_t n
= (size_t)(index
+ 1);
1191 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1194 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1196 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1198 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1199 wxGenericTreeItem
*parent
= i
->GetParent();
1200 if ( parent
== NULL
)
1202 // root item doesn't have any siblings
1203 return wxTreeItemId();
1206 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1207 int index
= siblings
.Index(i
);
1208 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1210 return index
== 0 ? wxTreeItemId()
1211 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1214 // Only for internal use right now, but should probably be public
1215 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1217 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1219 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1221 // First see if there are any children.
1222 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1223 if (children
.GetCount() > 0)
1225 return children
.Item(0);
1229 // Try a sibling of this or ancestor instead
1230 wxTreeItemId p
= item
;
1231 wxTreeItemId toFind
;
1234 toFind
= GetNextSibling(p
);
1235 p
= GetItemParent(p
);
1236 } while (p
.IsOk() && !toFind
.IsOk());
1241 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1243 wxTreeItemId id
= GetRootItem();
1252 } while (id
.IsOk());
1254 return wxTreeItemId();
1257 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1259 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1261 wxTreeItemId id
= item
;
1264 while (id
= GetNext(id
), id
.IsOk())
1270 return wxTreeItemId();
1273 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1275 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1277 wxFAIL_MSG(wxT("not implemented"));
1279 return wxTreeItemId();
1282 // called by wxTextTreeCtrl when it marks itself for deletion
1283 void wxGenericTreeCtrl::ResetTextControl()
1288 // find the first item starting with the given prefix after the given item
1289 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1290 const wxString
& prefixOrig
) const
1292 // match is case insensitive as this is more convenient to the user: having
1293 // to press Shift-letter to go to the item starting with a capital letter
1294 // would be too bothersome
1295 wxString prefix
= prefixOrig
.Lower();
1297 // determine the starting point: we shouldn't take the current item (this
1298 // allows to switch between two items starting with the same letter just by
1299 // pressing it) but we shouldn't jump to the next one if the user is
1300 // continuing to type as otherwise he might easily skip the item he wanted
1301 wxTreeItemId id
= idParent
;
1302 if ( prefix
.length() == 1 )
1307 // look for the item starting with the given prefix after it
1308 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1313 // if we haven't found anything...
1316 // ... wrap to the beginning
1318 if ( HasFlag(wxTR_HIDE_ROOT
) )
1320 // can't select virtual root
1324 // and try all the items (stop when we get to the one we started from)
1325 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1334 // -----------------------------------------------------------------------------
1336 // -----------------------------------------------------------------------------
1338 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1340 const wxString
& text
,
1341 int image
, int selImage
,
1342 wxTreeItemData
*data
)
1344 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1347 // should we give a warning here?
1348 return AddRoot(text
, image
, selImage
, data
);
1351 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1353 wxGenericTreeItem
*item
=
1354 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1358 data
->m_pItem
= (long) item
;
1361 parent
->Insert( item
, previous
);
1366 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1367 int image
, int selImage
,
1368 wxTreeItemData
*data
)
1370 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1372 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1374 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1375 image
, selImage
, data
);
1378 data
->m_pItem
= (long) m_anchor
;
1381 if (HasFlag(wxTR_HIDE_ROOT
))
1383 // if root is hidden, make sure we can navigate
1385 m_anchor
->SetHasPlus();
1387 CalculatePositions();
1390 if (!HasFlag(wxTR_MULTIPLE
))
1392 m_current
= m_key_current
= m_anchor
;
1393 m_current
->SetHilight( TRUE
);
1399 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1400 const wxString
& text
,
1401 int image
, int selImage
,
1402 wxTreeItemData
*data
)
1404 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1407 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1408 const wxTreeItemId
& idPrevious
,
1409 const wxString
& text
,
1410 int image
, int selImage
,
1411 wxTreeItemData
*data
)
1413 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1416 // should we give a warning here?
1417 return AddRoot(text
, image
, selImage
, data
);
1421 if (idPrevious
.IsOk())
1423 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1424 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1425 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1428 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1431 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1433 const wxString
& text
,
1434 int image
, int selImage
,
1435 wxTreeItemData
*data
)
1437 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1440 // should we give a warning here?
1441 return AddRoot(text
, image
, selImage
, data
);
1444 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1447 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1448 const wxString
& text
,
1449 int image
, int selImage
,
1450 wxTreeItemData
*data
)
1452 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1455 // should we give a warning here?
1456 return AddRoot(text
, image
, selImage
, data
);
1459 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1460 image
, selImage
, data
);
1463 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1465 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1466 event
.m_item
= (long) item
;
1467 event
.SetEventObject( this );
1468 ProcessEvent( event
);
1471 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1473 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1475 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1476 item
->DeleteChildren(this);
1479 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1481 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1483 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1485 wxGenericTreeItem
*parent
= item
->GetParent();
1487 // don't keep stale pointers around!
1488 if ( IsDescendantOf(item
, m_key_current
) )
1490 // Don't silently change the selection:
1491 // do it properly in idle time, so event
1492 // handlers get called.
1494 // m_key_current = parent;
1495 m_key_current
= NULL
;
1498 // m_select_me records whether we need to select
1499 // a different item, in idle time.
1500 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1502 m_select_me
= parent
;
1505 if ( IsDescendantOf(item
, m_current
) )
1507 // Don't silently change the selection:
1508 // do it properly in idle time, so event
1509 // handlers get called.
1511 // m_current = parent;
1513 m_select_me
= parent
;
1516 // remove the item from the tree
1519 parent
->GetChildren().Remove( item
); // remove by value
1521 else // deleting the root
1523 // nothing will be left in the tree
1527 // and delete all of its children and the item itself now
1528 item
->DeleteChildren(this);
1529 SendDeleteEvent(item
);
1533 void wxGenericTreeCtrl::DeleteAllItems()
1541 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1543 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1545 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1546 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1547 _T("can't expand hidden root") );
1549 if ( !item
->HasPlus() )
1552 if ( item
->IsExpanded() )
1555 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1556 event
.m_item
= (long) item
;
1557 event
.SetEventObject( this );
1559 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1561 // cancelled by program
1566 CalculatePositions();
1568 RefreshSubtree(item
);
1570 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1571 ProcessEvent( event
);
1574 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1576 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1579 if ( !IsExpanded(item
) )
1584 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1585 while ( child
.IsOk() )
1589 child
= GetNextChild(item
, cookie
);
1593 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1595 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1596 _T("can't collapse hidden root") );
1598 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1600 if ( !item
->IsExpanded() )
1603 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1604 event
.m_item
= (long) item
;
1605 event
.SetEventObject( this );
1606 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1608 // cancelled by program
1614 #if 0 // TODO why should items be collapsed recursively?
1615 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1616 size_t count
= children
.Count();
1617 for ( size_t n
= 0; n
< count
; n
++ )
1619 Collapse(children
[n
]);
1623 CalculatePositions();
1625 RefreshSubtree(item
);
1627 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1628 ProcessEvent( event
);
1631 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1634 DeleteChildren(item
);
1637 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1639 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1641 if (item
->IsExpanded())
1647 void wxGenericTreeCtrl::Unselect()
1651 m_current
->SetHilight( FALSE
);
1652 RefreshLine( m_current
);
1659 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1661 if (item
->IsSelected())
1663 item
->SetHilight(FALSE
);
1667 if (item
->HasChildren())
1669 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1670 size_t count
= children
.Count();
1671 for ( size_t n
= 0; n
< count
; ++n
)
1673 UnselectAllChildren(children
[n
]);
1678 void wxGenericTreeCtrl::UnselectAll()
1680 wxTreeItemId rootItem
= GetRootItem();
1682 // the tree might not have the root item at all
1685 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1689 // Recursive function !
1690 // To stop we must have crt_item<last_item
1692 // Tag all next children, when no more children,
1693 // Move to parent (not to tag)
1694 // Keep going... if we found last_item, we stop.
1695 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1697 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1699 if (parent
== NULL
) // This is root item
1700 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1702 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1703 int index
= children
.Index(crt_item
);
1704 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1706 size_t count
= children
.Count();
1707 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1709 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1712 return TagNextChildren(parent
, last_item
, select
);
1715 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1717 crt_item
->SetHilight(select
);
1718 RefreshLine(crt_item
);
1720 if (crt_item
==last_item
)
1723 if (crt_item
->HasChildren())
1725 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1726 size_t count
= children
.Count();
1727 for ( size_t n
= 0; n
< count
; ++n
)
1729 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1737 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1739 // item2 is not necessary after item1
1740 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1743 // choice first' and 'last' between item1 and item2
1744 if (item1
->GetY()<item2
->GetY())
1755 bool select
= m_current
->IsSelected();
1757 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1760 TagNextChildren(first
,last
,select
);
1763 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1764 bool unselect_others
,
1765 bool extended_select
)
1767 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1771 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1772 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1774 //wxCHECK_RET( ( (!unselect_others) && is_single),
1775 // wxT("this is a single selection tree") );
1777 // to keep going anyhow !!!
1780 if (item
->IsSelected())
1781 return; // nothing to do
1782 unselect_others
= TRUE
;
1783 extended_select
= FALSE
;
1785 else if ( unselect_others
&& item
->IsSelected() )
1787 // selection change if there is more than one item currently selected
1788 wxArrayTreeItemIds selected_items
;
1789 if ( GetSelections(selected_items
) == 1 )
1793 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1794 event
.m_item
= (long) item
;
1795 event
.m_itemOld
= (long) m_current
;
1796 event
.SetEventObject( this );
1797 // TODO : Here we don't send any selection mode yet !
1799 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1802 wxTreeItemId parent
= GetItemParent( itemId
);
1803 while (parent
.IsOk())
1805 if (!IsExpanded(parent
))
1808 parent
= GetItemParent( parent
);
1811 EnsureVisible( itemId
);
1814 if (unselect_others
)
1816 if (is_single
) Unselect(); // to speed up thing
1821 if (extended_select
)
1825 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1828 // don't change the mark (m_current)
1829 SelectItemRange(m_current
, item
);
1833 bool select
=TRUE
; // the default
1835 // Check if we need to toggle hilight (ctrl mode)
1836 if (!unselect_others
)
1837 select
=!item
->IsSelected();
1839 m_current
= m_key_current
= item
;
1840 m_current
->SetHilight(select
);
1841 RefreshLine( m_current
);
1844 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1845 GetEventHandler()->ProcessEvent( event
);
1848 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1849 wxArrayTreeItemIds
&array
) const
1851 if ( item
->IsSelected() )
1852 array
.Add(wxTreeItemId(item
));
1854 if ( item
->HasChildren() )
1856 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1857 size_t count
= children
.GetCount();
1858 for ( size_t n
= 0; n
< count
; ++n
)
1859 FillArray(children
[n
], array
);
1863 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1866 wxTreeItemId idRoot
= GetRootItem();
1867 if ( idRoot
.IsOk() )
1869 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1871 //else: the tree is empty, so no selections
1873 return array
.Count();
1876 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1878 if (!item
.IsOk()) return;
1880 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1882 // first expand all parent branches
1883 wxGenericTreeItem
*parent
= gitem
->GetParent();
1885 if ( HasFlag(wxTR_HIDE_ROOT
) )
1887 while ( parent
!= m_anchor
)
1890 parent
= parent
->GetParent();
1898 parent
= parent
->GetParent();
1902 //if (parent) CalculatePositions();
1907 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1909 if (!item
.IsOk()) return;
1911 // We have to call this here because the label in
1912 // question might just have been added and no screen
1913 // update taken place.
1914 if (m_dirty
) wxYieldIfNeeded();
1916 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1918 // now scroll to the item
1919 int item_y
= gitem
->GetY();
1923 GetViewStart( &start_x
, &start_y
);
1924 start_y
*= PIXELS_PER_UNIT
;
1928 GetClientSize( &client_w
, &client_h
);
1930 if (item_y
< start_y
+3)
1935 m_anchor
->GetSize( x
, y
, this );
1936 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1937 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1938 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1939 // Item should appear at top
1940 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1942 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1947 m_anchor
->GetSize( x
, y
, this );
1948 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1949 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1950 item_y
+= PIXELS_PER_UNIT
+2;
1951 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1952 // Item should appear at bottom
1953 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
);
1957 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1958 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1960 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1961 wxGenericTreeItem
**item2
)
1963 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1965 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1968 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1969 const wxTreeItemId
& item2
)
1971 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1974 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1976 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1978 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1980 wxCHECK_RET( !s_treeBeingSorted
,
1981 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1983 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1984 if ( children
.Count() > 1 )
1988 s_treeBeingSorted
= this;
1989 children
.Sort(tree_ctrl_compare_func
);
1990 s_treeBeingSorted
= NULL
;
1992 //else: don't make the tree dirty as nothing changed
1995 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1997 return m_imageListNormal
;
2000 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
2002 return m_imageListButtons
;
2005 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
2007 return m_imageListState
;
2010 void wxGenericTreeCtrl::CalculateLineHeight()
2012 wxClientDC
dc(this);
2013 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2015 if ( m_imageListNormal
)
2017 // Calculate a m_lineHeight value from the normal Image sizes.
2018 // May be toggle off. Then wxGenericTreeCtrl will spread when
2019 // necessary (which might look ugly).
2020 int n
= m_imageListNormal
->GetImageCount();
2021 for (int i
= 0; i
< n
; i
++)
2023 int width
= 0, height
= 0;
2024 m_imageListNormal
->GetSize(i
, width
, height
);
2025 if (height
> m_lineHeight
) m_lineHeight
= height
;
2029 if (m_imageListButtons
)
2031 // Calculate a m_lineHeight value from the Button image sizes.
2032 // May be toggle off. Then wxGenericTreeCtrl will spread when
2033 // necessary (which might look ugly).
2034 int n
= m_imageListButtons
->GetImageCount();
2035 for (int i
= 0; i
< n
; i
++)
2037 int width
= 0, height
= 0;
2038 m_imageListButtons
->GetSize(i
, width
, height
);
2039 if (height
> m_lineHeight
) m_lineHeight
= height
;
2043 if (m_lineHeight
< 30)
2044 m_lineHeight
+= 2; // at least 2 pixels
2046 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2049 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2051 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2052 m_imageListNormal
= imageList
;
2053 m_ownsImageListNormal
= FALSE
;
2055 // Don't do any drawing if we're setting the list to NULL,
2056 // since we may be in the process of deleting the tree control.
2058 CalculateLineHeight();
2061 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2063 if (m_ownsImageListState
) delete m_imageListState
;
2064 m_imageListState
= imageList
;
2065 m_ownsImageListState
= FALSE
;
2068 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2070 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2071 m_imageListButtons
= imageList
;
2072 m_ownsImageListButtons
= FALSE
;
2074 CalculateLineHeight();
2077 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2079 SetImageList(imageList
);
2080 m_ownsImageListNormal
= TRUE
;
2083 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2085 SetStateImageList(imageList
);
2086 m_ownsImageListState
= TRUE
;
2089 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2091 SetButtonsImageList(imageList
);
2092 m_ownsImageListButtons
= TRUE
;
2095 // -----------------------------------------------------------------------------
2097 // -----------------------------------------------------------------------------
2099 void wxGenericTreeCtrl::AdjustMyScrollbars()
2104 m_anchor
->GetSize( x
, y
, this );
2105 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2106 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2107 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2108 int y_pos
= GetScrollPos( wxVERTICAL
);
2109 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2113 SetScrollbars( 0, 0, 0, 0 );
2117 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2119 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2120 return item
->GetHeight();
2122 return m_lineHeight
;
2125 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2127 // TODO implement "state" icon on items
2129 wxTreeItemAttr
*attr
= item
->GetAttributes();
2130 if ( attr
&& attr
->HasFont() )
2131 dc
.SetFont(attr
->GetFont());
2132 else if (item
->IsBold())
2133 dc
.SetFont(m_boldFont
);
2135 long text_w
= 0, text_h
= 0;
2136 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2138 int image_h
= 0, image_w
= 0;
2139 int image
= item
->GetCurrentImage();
2140 if ( image
!= NO_IMAGE
)
2142 if ( m_imageListNormal
)
2144 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2153 int total_h
= GetLineHeight(item
);
2155 if ( item
->IsSelected() )
2157 // under mac selections are only a rectangle in case they don't have the focus
2161 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2162 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2166 dc
.SetBrush( *m_hilightBrush
) ;
2169 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2175 if ( attr
&& attr
->HasBackgroundColour() )
2176 colBg
= attr
->GetBackgroundColour();
2178 colBg
= m_backgroundColour
;
2179 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2182 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2184 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2188 DoGetPosition(&x
, &y
);
2190 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2194 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2196 // If it's selected, and there's an image, then we should
2197 // take care to leave the area under the image painted in the
2198 // background colour.
2199 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2200 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2204 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2205 item
->GetWidth()+2, total_h
-offset
);
2209 if ( image
!= NO_IMAGE
)
2211 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2212 m_imageListNormal
->Draw( image
, dc
,
2214 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2215 wxIMAGELIST_DRAW_TRANSPARENT
);
2216 dc
.DestroyClippingRegion();
2219 dc
.SetBackgroundMode(wxTRANSPARENT
);
2220 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2221 dc
.DrawText( item
->GetText(),
2222 (wxCoord
)(image_w
+ item
->GetX()),
2223 (wxCoord
)(item
->GetY() + extraH
));
2225 // restore normal font
2226 dc
.SetFont( m_normalFont
);
2229 // Now y stands for the top of the item, whereas it used to stand for middle !
2230 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2232 int x
= level
*m_indent
;
2233 if (!HasFlag(wxTR_HIDE_ROOT
))
2237 else if (level
== 0)
2239 // always expand hidden root
2241 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2242 int count
= children
.Count();
2248 PaintLevel(children
[n
], dc
, 1, y
);
2249 } while (++n
< count
);
2251 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2253 // draw line down to last child
2254 origY
+= GetLineHeight(children
[0])>>1;
2255 oldY
+= GetLineHeight(children
[n
-1])>>1;
2256 dc
.DrawLine(3, origY
, 3, oldY
);
2262 item
->SetX(x
+m_spacing
);
2265 int h
= GetLineHeight(item
);
2267 int y_mid
= y_top
+ (h
>>1);
2270 int exposed_x
= dc
.LogicalToDeviceX(0);
2271 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2273 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2277 // don't draw rect outline if we already have the
2278 // background color under Mac
2279 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2280 #endif // !__WXMAC__
2284 if ( item
->IsSelected() )
2286 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2290 wxTreeItemAttr
*attr
= item
->GetAttributes();
2291 if (attr
&& attr
->HasTextColour())
2292 colText
= attr
->GetTextColour();
2294 colText
= GetForegroundColour();
2298 dc
.SetTextForeground(colText
);
2302 PaintItem(item
, dc
);
2304 if (HasFlag(wxTR_ROW_LINES
))
2306 // if the background colour is white, choose a
2307 // contrasting color for the lines
2308 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2309 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2310 dc
.DrawLine(0, y_top
, 10000, y_top
);
2311 dc
.DrawLine(0, y
, 10000, y
);
2314 // restore DC objects
2315 dc
.SetBrush(*wxWHITE_BRUSH
);
2316 dc
.SetPen(m_dottedPen
);
2317 dc
.SetTextForeground(*wxBLACK
);
2319 if (item
->HasPlus() && HasButtons()) // should the item show a button?
2321 if (!HasFlag(wxTR_NO_LINES
))
2323 if (x
> (signed)m_indent
)
2324 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
2325 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2326 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
2327 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
2330 if (m_imageListButtons
!= NULL
)
2332 // draw the image button here
2333 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
2334 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
2335 if (item
->IsSelected())
2336 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2337 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2338 int xx
= x
- (image_w
>>1);
2339 int yy
= y_mid
- (image_h
>>1);
2340 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
2341 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2342 wxIMAGELIST_DRAW_TRANSPARENT
);
2343 dc
.DestroyClippingRegion();
2345 else if (HasFlag(wxTR_TWIST_BUTTONS
))
2347 // draw the twisty button here
2349 if (HasFlag(wxTR_AQUA_BUTTONS
))
2351 // This causes update problems, so disabling for now.
2352 #if 0 // def __WXMAC__
2353 wxMacPortSetter
helper(&dc
) ;
2354 wxMacWindowClipper
clipper(this) ;
2355 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
2358 int loc_y
= y_mid
- 6 ;
2359 MacWindowToRootWindow( & loc_x
, & loc_y
) ;
2360 Rect bounds
= { loc_y
, loc_x
, loc_y
+ 18 , loc_x
+ 12 } ;
2361 ThemeButtonDrawInfo info
= { kThemeStateActive
, item
->IsExpanded() ? kThemeDisclosureDown
: kThemeDisclosureRight
,
2362 kThemeAdornmentNone
};
2363 DrawThemeButton( &bounds
, kThemeDisclosureButton
,
2364 &info
, NULL
, NULL
, NULL
, NULL
) ;
2366 if (item
->IsExpanded())
2367 dc
.DrawBitmap( *m_arrowDown
, x
-5, y_mid
-6, TRUE
);
2369 dc
.DrawBitmap( *m_arrowRight
, x
-5, y_mid
-6, TRUE
);
2374 dc
.SetBrush(*m_hilightBrush
);
2375 dc
.SetPen(*wxBLACK_PEN
);
2378 if (item
->IsExpanded())
2381 button
[0].y
= y_mid
-2;
2383 button
[1].y
= y_mid
-2;
2385 button
[2].y
= y_mid
+3;
2389 button
[0].y
= y_mid
-5;
2391 button
[1].y
= y_mid
+5;
2393 button
[2].y
= y_mid
;
2396 dc
.DrawPolygon(3, button
);
2397 dc
.SetPen(m_dottedPen
);
2400 else // if (HasFlag(wxTR_HAS_BUTTONS))
2402 // draw the plus sign here
2403 dc
.SetPen(*wxGREY_PEN
);
2404 dc
.SetBrush(*wxWHITE_BRUSH
);
2405 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2406 dc
.SetPen(*wxBLACK_PEN
);
2407 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2408 if (!item
->IsExpanded())
2409 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2410 dc
.SetPen(m_dottedPen
);
2413 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2415 // draw the horizontal line here
2417 if (x
> (signed)m_indent
)
2418 x_start
-= m_indent
;
2419 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2421 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2425 if (item
->IsExpanded())
2427 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2428 int count
= children
.Count();
2435 PaintLevel(children
[n
], dc
, level
, y
);
2436 } while (++n
< count
);
2438 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2440 // draw line down to last child
2441 oldY
+= GetLineHeight(children
[n
-1])>>1;
2442 if (HasButtons()) y_mid
+= 5;
2444 // Only draw the portion of the line that is visible, in case it is huge
2445 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2446 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2447 yOrigin
= abs(yOrigin
);
2448 GetClientSize(&width
, &height
);
2450 // Move end points to the begining/end of the view?
2451 if (y_mid
< yOrigin
)
2453 if (oldY
> yOrigin
+ height
)
2454 oldY
= yOrigin
+ height
;
2456 // after the adjustments if y_mid is larger than oldY then the line
2457 // isn't visible at all so don't draw anything
2459 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2465 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2469 if ( item
->HasPlus() )
2471 // it's a folder, indicate it by a border
2476 // draw a line under the drop target because the item will be
2478 DrawLine(item
, TRUE
/* below */);
2481 SetCursor(wxCURSOR_BULLSEYE
);
2486 SetCursor(wxCURSOR_NO_ENTRY
);
2490 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2492 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2494 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2496 wxClientDC
dc(this);
2498 dc
.SetLogicalFunction(wxINVERT
);
2499 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2501 int w
= i
->GetWidth() + 2;
2502 int h
= GetLineHeight(i
) + 2;
2504 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2507 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2509 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2511 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2513 wxClientDC
dc(this);
2515 dc
.SetLogicalFunction(wxINVERT
);
2521 y
+= GetLineHeight(i
) - 1;
2524 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2527 // -----------------------------------------------------------------------------
2528 // wxWindows callbacks
2529 // -----------------------------------------------------------------------------
2531 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2539 dc
.SetFont( m_normalFont
);
2540 dc
.SetPen( m_dottedPen
);
2542 // this is now done dynamically
2543 //if(GetImageList() == NULL)
2544 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2547 PaintLevel( m_anchor
, dc
, 0, y
);
2550 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2559 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2568 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2570 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2571 te
.m_evtKey
= event
;
2572 te
.SetEventObject( this );
2573 if ( GetEventHandler()->ProcessEvent( te
) )
2575 // intercepted by the user code
2579 if ( (m_current
== 0) || (m_key_current
== 0) )
2585 // how should the selection work for this event?
2586 bool is_multiple
, extended_select
, unselect_others
;
2587 EventFlagsToSelType(GetWindowStyleFlag(),
2589 event
.ControlDown(),
2590 is_multiple
, extended_select
, unselect_others
);
2594 // * : Expand all/Collapse all
2595 // ' ' | return : activate
2596 // up : go up (not last children!)
2598 // left : go to parent
2599 // right : open if parent and go next
2600 // home : go to root
2601 // end : go to last item without opening parents
2602 // alnum : start or continue searching for the item with this prefix
2603 int keyCode
= event
.GetKeyCode();
2608 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2616 if ( !IsExpanded(m_current
) )
2619 ExpandAll(m_current
);
2622 //else: fall through to Collapse() it
2626 if (IsExpanded(m_current
))
2628 Collapse(m_current
);
2634 if ( !event
.HasModifiers() )
2636 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2637 event
.m_item
= (long) m_current
;
2638 event
.SetEventObject( this );
2639 GetEventHandler()->ProcessEvent( event
);
2642 // in any case, also generate the normal key event for this key,
2643 // even if we generated the ACTIVATED event above: this is what
2644 // wxMSW does and it makes sense because you might not want to
2645 // process ACTIVATED event at all and handle Space and Return
2646 // directly (and differently) which would be impossible otherwise
2650 // up goes to the previous sibling or to the last
2651 // of its children if it's expanded
2654 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2657 prev
= GetItemParent( m_key_current
);
2658 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2660 break; // don't go to root if it is hidden
2665 wxTreeItemId current
= m_key_current
;
2666 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2667 if (current
== GetFirstChild( prev
, cookie
))
2669 // otherwise we return to where we came from
2670 SelectItem( prev
, unselect_others
, extended_select
);
2671 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2678 while ( IsExpanded(prev
) && HasChildren(prev
) )
2680 wxTreeItemId child
= GetLastChild(prev
);
2687 SelectItem( prev
, unselect_others
, extended_select
);
2688 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2693 // left arrow goes to the parent
2696 wxTreeItemId prev
= GetItemParent( m_current
);
2697 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2699 // don't go to root if it is hidden
2700 prev
= GetPrevSibling( m_current
);
2704 SelectItem( prev
, unselect_others
, extended_select
);
2710 // this works the same as the down arrow except that we
2711 // also expand the item if it wasn't expanded yet
2717 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2720 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2721 SelectItem( child
, unselect_others
, extended_select
);
2722 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2726 wxTreeItemId next
= GetNextSibling( m_key_current
);
2729 wxTreeItemId current
= m_key_current
;
2730 while (current
&& !next
)
2732 current
= GetItemParent( current
);
2733 if (current
) next
= GetNextSibling( current
);
2738 SelectItem( next
, unselect_others
, extended_select
);
2739 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2745 // <End> selects the last visible tree item
2748 wxTreeItemId last
= GetRootItem();
2750 while ( last
.IsOk() && IsExpanded(last
) )
2752 wxTreeItemId lastChild
= GetLastChild(last
);
2754 // it may happen if the item was expanded but then all of
2755 // its children have been deleted - so IsExpanded() returned
2756 // TRUE, but GetLastChild() returned invalid item
2765 SelectItem( last
, unselect_others
, extended_select
);
2770 // <Home> selects the root item
2773 wxTreeItemId prev
= GetRootItem();
2777 if ( HasFlag(wxTR_HIDE_ROOT
) )
2780 prev
= GetFirstChild(prev
, dummy
);
2785 SelectItem( prev
, unselect_others
, extended_select
);
2790 // do not use wxIsalnum() here
2791 if ( !event
.HasModifiers() &&
2792 ((keyCode
>= '0' && keyCode
<= '9') ||
2793 (keyCode
>= 'a' && keyCode
<= 'z') ||
2794 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2796 // find the next item starting with the given prefix
2797 char ch
= (char)keyCode
;
2799 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2810 // also start the timer to reset the current prefix if the user
2811 // doesn't press any more alnum keys soon -- we wouldn't want
2812 // to use this prefix for a new item search
2815 m_findTimer
= new wxTreeFindTimer(this);
2818 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2827 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2829 // JACS: removed wxYieldIfNeeded() because it can cause the window
2830 // to be deleted from under us if a close window event is pending
2835 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2836 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2837 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2838 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2839 if (flags
) return wxTreeItemId();
2841 if (m_anchor
== NULL
)
2843 flags
= wxTREE_HITTEST_NOWHERE
;
2844 return wxTreeItemId();
2847 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2851 flags
= wxTREE_HITTEST_NOWHERE
;
2852 return wxTreeItemId();
2857 // get the bounding rectangle of the item (or of its label only)
2858 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2860 bool WXUNUSED(textOnly
)) const
2862 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2864 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2867 GetViewStart(& startX
, & startY
);
2869 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2870 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2871 rect
.width
= i
->GetWidth();
2872 //rect.height = i->GetHeight();
2873 rect
.height
= GetLineHeight(i
);
2878 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2880 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2882 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2884 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2885 te
.m_item
= (long) itemEdit
;
2886 te
.SetEventObject( this );
2887 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2893 // We have to call this here because the label in
2894 // question might just have been added and no screen
2895 // update taken place.
2899 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2901 m_textCtrl
->SetFocus();
2904 // returns a pointer to the text edit control if the item is being
2905 // edited, NULL otherwise (it's assumed that no more than one item may
2906 // be edited simultaneously)
2907 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2912 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2913 const wxString
& value
)
2915 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2916 le
.m_item
= (long) item
;
2917 le
.SetEventObject( this );
2919 le
.m_editCancelled
= FALSE
;
2921 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2924 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2926 // let owner know that the edit was cancelled
2927 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2928 le
.m_item
= (long) item
;
2929 le
.SetEventObject( this );
2930 le
.m_label
= wxEmptyString
;
2931 le
.m_editCancelled
= FALSE
;
2933 GetEventHandler()->ProcessEvent( le
);
2939 void wxGenericTreeCtrl::OnRenameTimer()
2944 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2946 if ( !m_anchor
) return;
2948 // we process left mouse up event (enables in-place edit), right down
2949 // (pass to the user code), left dbl click (activate item) and
2950 // dragging/moving events for items drag-and-drop
2951 if ( !(event
.LeftDown() ||
2953 event
.RightDown() ||
2954 event
.LeftDClick() ||
2956 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2963 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2966 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2968 if ( event
.Dragging() && !m_isDragging
)
2970 if (m_dragCount
== 0)
2975 if (m_dragCount
!= 3)
2977 // wait until user drags a bit further...
2981 wxEventType command
= event
.RightIsDown()
2982 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2983 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2985 wxTreeEvent
nevent( command
, GetId() );
2986 nevent
.m_item
= (long) m_current
;
2987 nevent
.SetEventObject(this);
2989 // by default the dragging is not supported, the user code must
2990 // explicitly allow the event for it to take place
2993 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2995 // we're going to drag this item
2996 m_isDragging
= TRUE
;
2998 // remember the old cursor because we will change it while
3000 m_oldCursor
= m_cursor
;
3002 // in a single selection control, hide the selection temporarily
3003 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3005 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3007 if ( m_oldSelection
)
3009 m_oldSelection
->SetHilight(FALSE
);
3010 RefreshLine(m_oldSelection
);
3017 else if ( event
.Moving() )
3019 if ( item
!= m_dropTarget
)
3021 // unhighlight the previous drop target
3022 DrawDropEffect(m_dropTarget
);
3024 m_dropTarget
= item
;
3026 // highlight the current drop target if any
3027 DrawDropEffect(m_dropTarget
);
3032 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3034 // erase the highlighting
3035 DrawDropEffect(m_dropTarget
);
3037 if ( m_oldSelection
)
3039 m_oldSelection
->SetHilight(TRUE
);
3040 RefreshLine(m_oldSelection
);
3041 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3044 // generate the drag end event
3045 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3047 event
.m_item
= (long) item
;
3048 event
.m_pointDrag
= pt
;
3049 event
.SetEventObject(this);
3051 (void)GetEventHandler()->ProcessEvent(event
);
3053 m_isDragging
= FALSE
;
3054 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3058 SetCursor(m_oldCursor
);
3064 // here we process only the messages which happen on tree items
3068 if (item
== NULL
) return; /* we hit the blank area */
3070 if ( event
.RightDown() )
3072 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3073 nevent
.m_item
= (long) item
;
3074 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3075 nevent
.SetEventObject(this);
3076 GetEventHandler()->ProcessEvent(nevent
);
3078 else if ( event
.LeftUp() )
3082 if ( (item
== m_current
) &&
3083 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3084 HasFlag(wxTR_EDIT_LABELS
) )
3086 if ( m_renameTimer
)
3088 if ( m_renameTimer
->IsRunning() )
3089 m_renameTimer
->Stop();
3093 m_renameTimer
= new wxTreeRenameTimer( this );
3096 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
3099 m_lastOnSame
= FALSE
;
3102 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3104 if ( event
.LeftDown() )
3106 m_lastOnSame
= item
== m_current
;
3109 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3111 // only toggle the item for a single click, double click on
3112 // the button doesn't do anything (it toggles the item twice)
3113 if ( event
.LeftDown() )
3118 // don't select the item if the button was clicked
3122 // how should the selection work for this event?
3123 bool is_multiple
, extended_select
, unselect_others
;
3124 EventFlagsToSelType(GetWindowStyleFlag(),
3126 event
.ControlDown(),
3127 is_multiple
, extended_select
, unselect_others
);
3129 SelectItem(item
, unselect_others
, extended_select
);
3131 // For some reason, Windows isn't recognizing a left double-click,
3132 // so we need to simulate it here. Allow 200 milliseconds for now.
3133 if ( event
.LeftDClick() )
3135 // double clicking should not start editing the item label
3136 if ( m_renameTimer
)
3137 m_renameTimer
->Stop();
3139 m_lastOnSame
= FALSE
;
3141 // send activate event first
3142 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3143 nevent
.m_item
= (long) item
;
3144 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3145 nevent
.SetEventObject( this );
3146 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3148 // if the user code didn't process the activate event,
3149 // handle it ourselves by toggling the item when it is
3151 if ( item
->HasPlus() )
3161 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
3163 // Check if we need to select the root item
3164 // because nothing else has been selected.
3165 // Delaying it means that we can invoke event handlers
3166 // as required, when a first item is selected.
3167 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3170 SelectItem(m_select_me
);
3171 else if (GetRootItem().IsOk())
3172 SelectItem(GetRootItem());
3175 /* after all changes have been done to the tree control,
3176 * we actually redraw the tree when everything is over */
3178 if (!m_dirty
) return;
3182 CalculatePositions();
3184 AdjustMyScrollbars();
3187 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3192 wxTreeItemAttr
*attr
= item
->GetAttributes();
3193 if ( attr
&& attr
->HasFont() )
3194 dc
.SetFont(attr
->GetFont());
3195 else if ( item
->IsBold() )
3196 dc
.SetFont(m_boldFont
);
3198 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3201 // restore normal font
3202 dc
.SetFont( m_normalFont
);
3206 int image
= item
->GetCurrentImage();
3207 if ( image
!= NO_IMAGE
)
3209 if ( m_imageListNormal
)
3211 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3216 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3219 total_h
+= 2; // at least 2 pixels
3221 total_h
+= total_h
/10; // otherwise 10% extra spacing
3223 item
->SetHeight(total_h
);
3224 if (total_h
>m_lineHeight
)
3225 m_lineHeight
=total_h
;
3227 item
->SetWidth(image_w
+text_w
+2);
3230 // -----------------------------------------------------------------------------
3231 // for developper : y is now the top of the level
3232 // not the middle of it !
3233 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3235 int x
= level
*m_indent
;
3236 if (!HasFlag(wxTR_HIDE_ROOT
))
3240 else if (level
== 0)
3242 // a hidden root is not evaluated, but its
3243 // children are always calculated
3247 CalculateSize( item
, dc
);
3250 item
->SetX( x
+m_spacing
);
3252 y
+= GetLineHeight(item
);
3254 if ( !item
->IsExpanded() )
3256 // we don't need to calculate collapsed branches
3261 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3262 size_t n
, count
= children
.Count();
3264 for (n
= 0; n
< count
; ++n
)
3265 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3268 void wxGenericTreeCtrl::CalculatePositions()
3270 if ( !m_anchor
) return;
3272 wxClientDC
dc(this);
3275 dc
.SetFont( m_normalFont
);
3277 dc
.SetPen( m_dottedPen
);
3278 //if(GetImageList() == NULL)
3279 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3282 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3285 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3287 if (m_dirty
) return;
3289 wxSize client
= GetClientSize();
3292 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3293 rect
.width
= client
.x
;
3294 rect
.height
= client
.y
;
3296 Refresh(TRUE
, &rect
);
3298 AdjustMyScrollbars();
3301 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3303 if (m_dirty
) return;
3306 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3307 rect
.width
= GetClientSize().x
;
3308 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3310 Refresh(TRUE
, &rect
);
3313 void wxGenericTreeCtrl::RefreshSelected()
3315 // TODO: this is awfully inefficient, we should keep the list of all
3316 // selected items internally, should be much faster
3318 RefreshSelectedUnder(m_anchor
);
3321 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3323 if ( item
->IsSelected() )
3326 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3327 size_t count
= children
.GetCount();
3328 for ( size_t n
= 0; n
< count
; n
++ )
3330 RefreshSelectedUnder(children
[n
]);
3334 // ----------------------------------------------------------------------------
3335 // changing colours: we need to refresh the tree control
3336 // ----------------------------------------------------------------------------
3338 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3340 if ( !wxWindow::SetBackgroundColour(colour
) )
3348 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3350 if ( !wxWindow::SetForegroundColour(colour
) )
3358 #endif // wxUSE_TREECTRL