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
)
721 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
723 * wxTreeCtrl has to be a real class or we have problems with
724 * the run-time information.
727 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
730 // -----------------------------------------------------------------------------
731 // construction/destruction
732 // -----------------------------------------------------------------------------
734 void wxGenericTreeCtrl::Init()
736 m_current
= m_key_current
= m_anchor
= m_select_me
= (wxGenericTreeItem
*) NULL
;
744 m_hilightBrush
= new wxBrush
746 wxSystemSettings::GetColour
748 wxSYS_COLOUR_HIGHLIGHT
753 m_hilightUnfocusedBrush
= new wxBrush
755 wxSystemSettings::GetColour
757 wxSYS_COLOUR_BTNSHADOW
762 m_imageListNormal
= m_imageListButtons
=
763 m_imageListState
= (wxImageList
*) NULL
;
764 m_ownsImageListNormal
= m_ownsImageListButtons
=
765 m_ownsImageListState
= FALSE
;
768 m_isDragging
= FALSE
;
769 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
772 m_renameTimer
= NULL
;
775 m_lastOnSame
= FALSE
;
777 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
778 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
779 m_normalFont
.GetFamily(),
780 m_normalFont
.GetStyle(),
782 m_normalFont
.GetUnderlined(),
783 m_normalFont
.GetFaceName(),
784 m_normalFont
.GetEncoding());
787 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
792 const wxValidator
&validator
,
793 const wxString
& name
)
797 wxGetOsVersion( &major
, &minor
);
799 if (style
& wxTR_HAS_BUTTONS
) style
|= wxTR_MAC_BUTTONS
;
800 if (style
& wxTR_HAS_BUTTONS
) style
&= ~wxTR_HAS_BUTTONS
;
801 style
&= ~wxTR_LINES_AT_ROOT
;
802 style
|= wxTR_NO_LINES
;
804 style
|= wxTR_ROW_LINES
;
806 style
|= wxTR_AQUA_BUTTONS
;
809 if (style
& wxTR_AQUA_BUTTONS
)
811 m_arrowRight
= new wxBitmap( aqua_arrow_right
);
812 m_arrowDown
= new wxBitmap( aqua_arrow_down
);
820 wxScrolledWindow::Create( parent
, id
, pos
, size
,
821 style
|wxHSCROLL
|wxVSCROLL
, name
);
823 // If the tree display has no buttons, but does have
824 // connecting lines, we can use a narrower layout.
825 // It may not be a good idea to force this...
826 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
833 SetValidator( validator
);
836 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
837 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
839 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
840 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
845 wxGenericTreeCtrl::~wxGenericTreeCtrl()
847 delete m_hilightBrush
;
848 delete m_hilightUnfocusedBrush
;
855 delete m_renameTimer
;
858 if (m_ownsImageListNormal
)
859 delete m_imageListNormal
;
860 if (m_ownsImageListState
)
861 delete m_imageListState
;
862 if (m_ownsImageListButtons
)
863 delete m_imageListButtons
;
866 // -----------------------------------------------------------------------------
868 // -----------------------------------------------------------------------------
870 size_t wxGenericTreeCtrl::GetCount() const
872 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
875 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
877 m_indent
= (unsigned short) indent
;
881 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
883 m_spacing
= (unsigned short) spacing
;
887 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
889 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
891 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
894 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
896 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
898 // if we will hide the root, make sure children are visible
899 m_anchor
->SetHasPlus();
901 CalculatePositions();
904 // right now, just sets the styles. Eventually, we may
905 // want to update the inherited styles, but right now
906 // none of the parents has updatable styles
907 m_windowStyle
= styles
;
911 // -----------------------------------------------------------------------------
912 // functions to work with tree items
913 // -----------------------------------------------------------------------------
915 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
917 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
919 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
922 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
923 wxTreeItemIcon which
) const
925 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
927 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
930 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
932 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
934 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
937 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
939 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
941 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
942 return pItem
->Attr().GetTextColour();
945 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
947 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
949 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
950 return pItem
->Attr().GetBackgroundColour();
953 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
955 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
957 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
958 return pItem
->Attr().GetFont();
961 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
963 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
966 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
967 pItem
->SetText(text
);
968 CalculateSize(pItem
, dc
);
972 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
974 wxTreeItemIcon which
)
976 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
978 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
979 pItem
->SetImage(image
, which
);
982 CalculateSize(pItem
, dc
);
986 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
988 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
990 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
993 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
995 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
997 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
998 pItem
->SetHasPlus(has
);
1002 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1004 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1006 // avoid redrawing the tree if no real change
1007 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1008 if ( pItem
->IsBold() != bold
)
1010 pItem
->SetBold(bold
);
1015 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1016 const wxColour
& col
)
1018 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1020 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1021 pItem
->Attr().SetTextColour(col
);
1025 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1026 const wxColour
& col
)
1028 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1030 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1031 pItem
->Attr().SetBackgroundColour(col
);
1035 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1037 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1039 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1040 pItem
->Attr().SetFont(font
);
1044 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1046 wxScrolledWindow::SetFont(font
);
1048 m_normalFont
= font
;
1049 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1050 m_normalFont
.GetFamily(),
1051 m_normalFont
.GetStyle(),
1053 m_normalFont
.GetUnderlined(),
1054 m_normalFont
.GetFaceName(),
1055 m_normalFont
.GetEncoding());
1061 // -----------------------------------------------------------------------------
1062 // item status inquiries
1063 // -----------------------------------------------------------------------------
1065 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1067 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1069 // An item is only visible if it's not a descendant of a collapsed item
1070 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1071 wxGenericTreeItem
* parent
= pItem
->GetParent();
1074 if (!parent
->IsExpanded())
1076 parent
= parent
->GetParent();
1080 GetViewStart(& startX
, & startY
);
1082 wxSize clientSize
= GetClientSize();
1085 if (!GetBoundingRect(item
, rect
))
1087 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1089 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1091 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1097 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1099 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1101 // consider that the item does have children if it has the "+" button: it
1102 // might not have them (if it had never been expanded yet) but then it
1103 // could have them as well and it's better to err on this side rather than
1104 // disabling some operations which are restricted to the items with
1105 // children for an item which does have them
1106 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1109 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1111 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1113 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1116 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1118 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1120 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1123 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1125 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1127 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1130 // -----------------------------------------------------------------------------
1132 // -----------------------------------------------------------------------------
1134 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1136 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1138 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1141 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
1143 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1146 return GetNextChild(item
, cookie
);
1149 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
1151 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1153 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1154 if ( (size_t)cookie
< children
.Count() )
1156 return children
.Item((size_t)cookie
++);
1160 // there are no more of them
1161 return wxTreeItemId();
1165 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1167 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1169 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1170 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1173 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1175 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1177 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1178 wxGenericTreeItem
*parent
= i
->GetParent();
1179 if ( parent
== NULL
)
1181 // root item doesn't have any siblings
1182 return wxTreeItemId();
1185 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1186 int index
= siblings
.Index(i
);
1187 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1189 size_t n
= (size_t)(index
+ 1);
1190 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1193 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1195 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1197 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1198 wxGenericTreeItem
*parent
= i
->GetParent();
1199 if ( parent
== NULL
)
1201 // root item doesn't have any siblings
1202 return wxTreeItemId();
1205 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1206 int index
= siblings
.Index(i
);
1207 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1209 return index
== 0 ? wxTreeItemId()
1210 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1213 // Only for internal use right now, but should probably be public
1214 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1216 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1218 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1220 // First see if there are any children.
1221 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1222 if (children
.GetCount() > 0)
1224 return children
.Item(0);
1228 // Try a sibling of this or ancestor instead
1229 wxTreeItemId p
= item
;
1230 wxTreeItemId toFind
;
1233 toFind
= GetNextSibling(p
);
1234 p
= GetItemParent(p
);
1235 } while (p
.IsOk() && !toFind
.IsOk());
1240 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1242 wxTreeItemId id
= GetRootItem();
1251 } while (id
.IsOk());
1253 return wxTreeItemId();
1256 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1258 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1260 wxTreeItemId id
= item
;
1263 while (id
= GetNext(id
), id
.IsOk())
1269 return wxTreeItemId();
1272 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1274 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1276 wxFAIL_MSG(wxT("not implemented"));
1278 return wxTreeItemId();
1281 // called by wxTextTreeCtrl when it marks itself for deletion
1282 void wxGenericTreeCtrl::ResetTextControl()
1287 // find the first item starting with the given prefix after the given item
1288 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1289 const wxString
& prefixOrig
) const
1291 // match is case insensitive as this is more convenient to the user: having
1292 // to press Shift-letter to go to the item starting with a capital letter
1293 // would be too bothersome
1294 wxString prefix
= prefixOrig
.Lower();
1296 // determine the starting point: we shouldn't take the current item (this
1297 // allows to switch between two items starting with the same letter just by
1298 // pressing it) but we shouldn't jump to the next one if the user is
1299 // continuing to type as otherwise he might easily skip the item he wanted
1300 wxTreeItemId id
= idParent
;
1301 if ( prefix
.length() == 1 )
1306 // look for the item starting with the given prefix after it
1307 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1312 // if we haven't found anything...
1315 // ... wrap to the beginning
1317 if ( HasFlag(wxTR_HIDE_ROOT
) )
1319 // can't select virtual root
1323 // and try all the items (stop when we get to the one we started from)
1324 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1333 // -----------------------------------------------------------------------------
1335 // -----------------------------------------------------------------------------
1337 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1339 const wxString
& text
,
1340 int image
, int selImage
,
1341 wxTreeItemData
*data
)
1343 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1346 // should we give a warning here?
1347 return AddRoot(text
, image
, selImage
, data
);
1350 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1352 wxGenericTreeItem
*item
=
1353 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1357 data
->m_pItem
= (long) item
;
1360 parent
->Insert( item
, previous
);
1365 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1366 int image
, int selImage
,
1367 wxTreeItemData
*data
)
1369 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1371 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1373 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1374 image
, selImage
, data
);
1377 data
->m_pItem
= (long) m_anchor
;
1380 if (HasFlag(wxTR_HIDE_ROOT
))
1382 // if root is hidden, make sure we can navigate
1384 m_anchor
->SetHasPlus();
1386 CalculatePositions();
1389 if (!HasFlag(wxTR_MULTIPLE
))
1391 m_current
= m_key_current
= m_anchor
;
1392 m_current
->SetHilight( TRUE
);
1398 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1399 const wxString
& text
,
1400 int image
, int selImage
,
1401 wxTreeItemData
*data
)
1403 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1406 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1407 const wxTreeItemId
& idPrevious
,
1408 const wxString
& text
,
1409 int image
, int selImage
,
1410 wxTreeItemData
*data
)
1412 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1415 // should we give a warning here?
1416 return AddRoot(text
, image
, selImage
, data
);
1420 if (idPrevious
.IsOk())
1422 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1423 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1424 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1427 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1430 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1432 const wxString
& text
,
1433 int image
, int selImage
,
1434 wxTreeItemData
*data
)
1436 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1439 // should we give a warning here?
1440 return AddRoot(text
, image
, selImage
, data
);
1443 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1446 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1447 const wxString
& text
,
1448 int image
, int selImage
,
1449 wxTreeItemData
*data
)
1451 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1454 // should we give a warning here?
1455 return AddRoot(text
, image
, selImage
, data
);
1458 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1459 image
, selImage
, data
);
1462 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1464 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1465 event
.m_item
= (long) item
;
1466 event
.SetEventObject( this );
1467 ProcessEvent( event
);
1470 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1472 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1474 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1475 item
->DeleteChildren(this);
1478 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1480 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1482 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1484 wxGenericTreeItem
*parent
= item
->GetParent();
1486 // don't keep stale pointers around!
1487 if ( IsDescendantOf(item
, m_key_current
) )
1489 // Don't silently change the selection:
1490 // do it properly in idle time, so event
1491 // handlers get called.
1493 // m_key_current = parent;
1494 m_key_current
= NULL
;
1497 // m_select_me records whether we need to select
1498 // a different item, in idle time.
1499 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1501 m_select_me
= parent
;
1504 if ( IsDescendantOf(item
, m_current
) )
1506 // Don't silently change the selection:
1507 // do it properly in idle time, so event
1508 // handlers get called.
1510 // m_current = parent;
1512 m_select_me
= parent
;
1515 // remove the item from the tree
1518 parent
->GetChildren().Remove( item
); // remove by value
1520 else // deleting the root
1522 // nothing will be left in the tree
1526 // and delete all of its children and the item itself now
1527 item
->DeleteChildren(this);
1528 SendDeleteEvent(item
);
1532 void wxGenericTreeCtrl::DeleteAllItems()
1540 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1542 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1544 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1545 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1546 _T("can't expand hidden root") );
1548 if ( !item
->HasPlus() )
1551 if ( item
->IsExpanded() )
1554 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1555 event
.m_item
= (long) item
;
1556 event
.SetEventObject( this );
1558 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1560 // cancelled by program
1565 CalculatePositions();
1567 RefreshSubtree(item
);
1569 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1570 ProcessEvent( event
);
1573 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1575 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1578 if ( !IsExpanded(item
) )
1583 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1584 while ( child
.IsOk() )
1588 child
= GetNextChild(item
, cookie
);
1592 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1594 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1595 _T("can't collapse hidden root") );
1597 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1599 if ( !item
->IsExpanded() )
1602 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1603 event
.m_item
= (long) item
;
1604 event
.SetEventObject( this );
1605 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1607 // cancelled by program
1613 #if 0 // TODO why should items be collapsed recursively?
1614 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1615 size_t count
= children
.Count();
1616 for ( size_t n
= 0; n
< count
; n
++ )
1618 Collapse(children
[n
]);
1622 CalculatePositions();
1624 RefreshSubtree(item
);
1626 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1627 ProcessEvent( event
);
1630 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1633 DeleteChildren(item
);
1636 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1638 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1640 if (item
->IsExpanded())
1646 void wxGenericTreeCtrl::Unselect()
1650 m_current
->SetHilight( FALSE
);
1651 RefreshLine( m_current
);
1658 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1660 if (item
->IsSelected())
1662 item
->SetHilight(FALSE
);
1666 if (item
->HasChildren())
1668 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1669 size_t count
= children
.Count();
1670 for ( size_t n
= 0; n
< count
; ++n
)
1672 UnselectAllChildren(children
[n
]);
1677 void wxGenericTreeCtrl::UnselectAll()
1679 wxTreeItemId rootItem
= GetRootItem();
1681 // the tree might not have the root item at all
1684 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1688 // Recursive function !
1689 // To stop we must have crt_item<last_item
1691 // Tag all next children, when no more children,
1692 // Move to parent (not to tag)
1693 // Keep going... if we found last_item, we stop.
1694 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1696 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1698 if (parent
== NULL
) // This is root item
1699 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1701 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1702 int index
= children
.Index(crt_item
);
1703 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1705 size_t count
= children
.Count();
1706 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1708 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1711 return TagNextChildren(parent
, last_item
, select
);
1714 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1716 crt_item
->SetHilight(select
);
1717 RefreshLine(crt_item
);
1719 if (crt_item
==last_item
)
1722 if (crt_item
->HasChildren())
1724 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1725 size_t count
= children
.Count();
1726 for ( size_t n
= 0; n
< count
; ++n
)
1728 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1736 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1738 // item2 is not necessary after item1
1739 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1742 // choice first' and 'last' between item1 and item2
1743 if (item1
->GetY()<item2
->GetY())
1754 bool select
= m_current
->IsSelected();
1756 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1759 TagNextChildren(first
,last
,select
);
1762 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1763 bool unselect_others
,
1764 bool extended_select
)
1766 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1770 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1771 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1773 //wxCHECK_RET( ( (!unselect_others) && is_single),
1774 // wxT("this is a single selection tree") );
1776 // to keep going anyhow !!!
1779 if (item
->IsSelected())
1780 return; // nothing to do
1781 unselect_others
= TRUE
;
1782 extended_select
= FALSE
;
1784 else if ( unselect_others
&& item
->IsSelected() )
1786 // selection change if there is more than one item currently selected
1787 wxArrayTreeItemIds selected_items
;
1788 if ( GetSelections(selected_items
) == 1 )
1792 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1793 event
.m_item
= (long) item
;
1794 event
.m_itemOld
= (long) m_current
;
1795 event
.SetEventObject( this );
1796 // TODO : Here we don't send any selection mode yet !
1798 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1801 wxTreeItemId parent
= GetItemParent( itemId
);
1802 while (parent
.IsOk())
1804 if (!IsExpanded(parent
))
1807 parent
= GetItemParent( parent
);
1810 EnsureVisible( itemId
);
1813 if (unselect_others
)
1815 if (is_single
) Unselect(); // to speed up thing
1820 if (extended_select
)
1824 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1827 // don't change the mark (m_current)
1828 SelectItemRange(m_current
, item
);
1832 bool select
=TRUE
; // the default
1834 // Check if we need to toggle hilight (ctrl mode)
1835 if (!unselect_others
)
1836 select
=!item
->IsSelected();
1838 m_current
= m_key_current
= item
;
1839 m_current
->SetHilight(select
);
1840 RefreshLine( m_current
);
1843 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1844 GetEventHandler()->ProcessEvent( event
);
1847 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1848 wxArrayTreeItemIds
&array
) const
1850 if ( item
->IsSelected() )
1851 array
.Add(wxTreeItemId(item
));
1853 if ( item
->HasChildren() )
1855 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1856 size_t count
= children
.GetCount();
1857 for ( size_t n
= 0; n
< count
; ++n
)
1858 FillArray(children
[n
], array
);
1862 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1865 wxTreeItemId idRoot
= GetRootItem();
1866 if ( idRoot
.IsOk() )
1868 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1870 //else: the tree is empty, so no selections
1872 return array
.Count();
1875 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1877 if (!item
.IsOk()) return;
1879 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1881 // first expand all parent branches
1882 wxGenericTreeItem
*parent
= gitem
->GetParent();
1884 if ( HasFlag(wxTR_HIDE_ROOT
) )
1886 while ( parent
&& parent
!= m_anchor
)
1889 parent
= parent
->GetParent();
1897 parent
= parent
->GetParent();
1901 //if (parent) CalculatePositions();
1906 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1908 if (!item
.IsOk()) return;
1910 // We have to call this here because the label in
1911 // question might just have been added and no screen
1912 // update taken place.
1913 if (m_dirty
) wxYieldIfNeeded();
1915 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1917 // now scroll to the item
1918 int item_y
= gitem
->GetY();
1922 GetViewStart( &start_x
, &start_y
);
1923 start_y
*= PIXELS_PER_UNIT
;
1927 GetClientSize( &client_w
, &client_h
);
1929 if (item_y
< start_y
+3)
1934 m_anchor
->GetSize( x
, y
, this );
1935 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1936 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1937 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1938 // Item should appear at top
1939 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1941 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1946 m_anchor
->GetSize( x
, y
, this );
1947 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1948 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1949 item_y
+= PIXELS_PER_UNIT
+2;
1950 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1951 // Item should appear at bottom
1952 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
);
1956 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1957 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1959 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1960 wxGenericTreeItem
**item2
)
1962 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1964 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1967 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1968 const wxTreeItemId
& item2
)
1970 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1973 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1975 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1977 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1979 wxCHECK_RET( !s_treeBeingSorted
,
1980 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1982 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1983 if ( children
.Count() > 1 )
1987 s_treeBeingSorted
= this;
1988 children
.Sort(tree_ctrl_compare_func
);
1989 s_treeBeingSorted
= NULL
;
1991 //else: don't make the tree dirty as nothing changed
1994 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1996 return m_imageListNormal
;
1999 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
2001 return m_imageListButtons
;
2004 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
2006 return m_imageListState
;
2009 void wxGenericTreeCtrl::CalculateLineHeight()
2011 wxClientDC
dc(this);
2012 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2014 if ( m_imageListNormal
)
2016 // Calculate a m_lineHeight value from the normal Image sizes.
2017 // May be toggle off. Then wxGenericTreeCtrl will spread when
2018 // necessary (which might look ugly).
2019 int n
= m_imageListNormal
->GetImageCount();
2020 for (int i
= 0; i
< n
; i
++)
2022 int width
= 0, height
= 0;
2023 m_imageListNormal
->GetSize(i
, width
, height
);
2024 if (height
> m_lineHeight
) m_lineHeight
= height
;
2028 if (m_imageListButtons
)
2030 // Calculate a m_lineHeight value from the Button image sizes.
2031 // May be toggle off. Then wxGenericTreeCtrl will spread when
2032 // necessary (which might look ugly).
2033 int n
= m_imageListButtons
->GetImageCount();
2034 for (int i
= 0; i
< n
; i
++)
2036 int width
= 0, height
= 0;
2037 m_imageListButtons
->GetSize(i
, width
, height
);
2038 if (height
> m_lineHeight
) m_lineHeight
= height
;
2042 if (m_lineHeight
< 30)
2043 m_lineHeight
+= 2; // at least 2 pixels
2045 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2048 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2050 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2051 m_imageListNormal
= imageList
;
2052 m_ownsImageListNormal
= FALSE
;
2054 // Don't do any drawing if we're setting the list to NULL,
2055 // since we may be in the process of deleting the tree control.
2057 CalculateLineHeight();
2060 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2062 if (m_ownsImageListState
) delete m_imageListState
;
2063 m_imageListState
= imageList
;
2064 m_ownsImageListState
= FALSE
;
2067 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2069 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2070 m_imageListButtons
= imageList
;
2071 m_ownsImageListButtons
= FALSE
;
2073 CalculateLineHeight();
2076 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2078 SetImageList(imageList
);
2079 m_ownsImageListNormal
= TRUE
;
2082 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2084 SetStateImageList(imageList
);
2085 m_ownsImageListState
= TRUE
;
2088 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2090 SetButtonsImageList(imageList
);
2091 m_ownsImageListButtons
= TRUE
;
2094 // -----------------------------------------------------------------------------
2096 // -----------------------------------------------------------------------------
2098 void wxGenericTreeCtrl::AdjustMyScrollbars()
2103 m_anchor
->GetSize( x
, y
, this );
2104 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2105 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2106 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2107 int y_pos
= GetScrollPos( wxVERTICAL
);
2108 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2112 SetScrollbars( 0, 0, 0, 0 );
2116 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2118 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2119 return item
->GetHeight();
2121 return m_lineHeight
;
2124 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2126 // TODO implement "state" icon on items
2128 wxTreeItemAttr
*attr
= item
->GetAttributes();
2129 if ( attr
&& attr
->HasFont() )
2130 dc
.SetFont(attr
->GetFont());
2131 else if (item
->IsBold())
2132 dc
.SetFont(m_boldFont
);
2134 long text_w
= 0, text_h
= 0;
2135 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2137 int image_h
= 0, image_w
= 0;
2138 int image
= item
->GetCurrentImage();
2139 if ( image
!= NO_IMAGE
)
2141 if ( m_imageListNormal
)
2143 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2152 int total_h
= GetLineHeight(item
);
2154 if ( item
->IsSelected() )
2156 // under mac selections are only a rectangle in case they don't have the focus
2160 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2161 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2165 dc
.SetBrush( *m_hilightBrush
) ;
2168 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2174 if ( attr
&& attr
->HasBackgroundColour() )
2175 colBg
= attr
->GetBackgroundColour();
2177 colBg
= m_backgroundColour
;
2178 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2181 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2183 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2187 DoGetPosition(&x
, &y
);
2189 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2193 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2195 // If it's selected, and there's an image, then we should
2196 // take care to leave the area under the image painted in the
2197 // background colour.
2198 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2199 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2203 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2204 item
->GetWidth()+2, total_h
-offset
);
2208 if ( image
!= NO_IMAGE
)
2210 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2211 m_imageListNormal
->Draw( image
, dc
,
2213 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2214 wxIMAGELIST_DRAW_TRANSPARENT
);
2215 dc
.DestroyClippingRegion();
2218 dc
.SetBackgroundMode(wxTRANSPARENT
);
2219 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2220 dc
.DrawText( item
->GetText(),
2221 (wxCoord
)(image_w
+ item
->GetX()),
2222 (wxCoord
)(item
->GetY() + extraH
));
2224 // restore normal font
2225 dc
.SetFont( m_normalFont
);
2228 // Now y stands for the top of the item, whereas it used to stand for middle !
2229 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2231 int x
= level
*m_indent
;
2232 if (!HasFlag(wxTR_HIDE_ROOT
))
2236 else if (level
== 0)
2238 // always expand hidden root
2240 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2241 int count
= children
.Count();
2247 PaintLevel(children
[n
], dc
, 1, y
);
2248 } while (++n
< count
);
2250 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2252 // draw line down to last child
2253 origY
+= GetLineHeight(children
[0])>>1;
2254 oldY
+= GetLineHeight(children
[n
-1])>>1;
2255 dc
.DrawLine(3, origY
, 3, oldY
);
2261 item
->SetX(x
+m_spacing
);
2264 int h
= GetLineHeight(item
);
2266 int y_mid
= y_top
+ (h
>>1);
2269 int exposed_x
= dc
.LogicalToDeviceX(0);
2270 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2272 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2276 // don't draw rect outline if we already have the
2277 // background color under Mac
2278 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2279 #endif // !__WXMAC__
2283 if ( item
->IsSelected() )
2285 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2289 wxTreeItemAttr
*attr
= item
->GetAttributes();
2290 if (attr
&& attr
->HasTextColour())
2291 colText
= attr
->GetTextColour();
2293 colText
= GetForegroundColour();
2297 dc
.SetTextForeground(colText
);
2301 PaintItem(item
, dc
);
2303 if (HasFlag(wxTR_ROW_LINES
))
2305 // if the background colour is white, choose a
2306 // contrasting color for the lines
2307 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2308 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2309 dc
.DrawLine(0, y_top
, 10000, y_top
);
2310 dc
.DrawLine(0, y
, 10000, y
);
2313 // restore DC objects
2314 dc
.SetBrush(*wxWHITE_BRUSH
);
2315 dc
.SetPen(m_dottedPen
);
2316 dc
.SetTextForeground(*wxBLACK
);
2318 if (item
->HasPlus() && HasButtons()) // should the item show a button?
2320 if (!HasFlag(wxTR_NO_LINES
))
2322 if (x
> (signed)m_indent
)
2323 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
2324 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2325 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
2326 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
2329 if (m_imageListButtons
!= NULL
)
2331 // draw the image button here
2332 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
2333 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
2334 if (item
->IsSelected())
2335 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2336 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2337 int xx
= x
- (image_w
>>1);
2338 int yy
= y_mid
- (image_h
>>1);
2339 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
2340 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2341 wxIMAGELIST_DRAW_TRANSPARENT
);
2342 dc
.DestroyClippingRegion();
2344 else if (HasFlag(wxTR_TWIST_BUTTONS
))
2346 // draw the twisty button here
2348 if (HasFlag(wxTR_AQUA_BUTTONS
))
2350 // This causes update problems, so disabling for now.
2351 #if 0 // def __WXMAC__
2352 wxMacPortSetter
helper(&dc
) ;
2353 wxMacWindowClipper
clipper(this) ;
2354 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
2357 int loc_y
= y_mid
- 6 ;
2358 MacWindowToRootWindow( & loc_x
, & loc_y
) ;
2359 Rect bounds
= { loc_y
, loc_x
, loc_y
+ 18 , loc_x
+ 12 } ;
2360 ThemeButtonDrawInfo info
= { kThemeStateActive
, item
->IsExpanded() ? kThemeDisclosureDown
: kThemeDisclosureRight
,
2361 kThemeAdornmentNone
};
2362 DrawThemeButton( &bounds
, kThemeDisclosureButton
,
2363 &info
, NULL
, NULL
, NULL
, NULL
) ;
2365 if (item
->IsExpanded())
2366 dc
.DrawBitmap( *m_arrowDown
, x
-5, y_mid
-6, TRUE
);
2368 dc
.DrawBitmap( *m_arrowRight
, x
-5, y_mid
-6, TRUE
);
2373 dc
.SetBrush(*m_hilightBrush
);
2374 dc
.SetPen(*wxBLACK_PEN
);
2377 if (item
->IsExpanded())
2380 button
[0].y
= y_mid
-2;
2382 button
[1].y
= y_mid
-2;
2384 button
[2].y
= y_mid
+3;
2388 button
[0].y
= y_mid
-5;
2390 button
[1].y
= y_mid
+5;
2392 button
[2].y
= y_mid
;
2395 dc
.DrawPolygon(3, button
);
2396 dc
.SetPen(m_dottedPen
);
2399 else // if (HasFlag(wxTR_HAS_BUTTONS))
2401 // draw the plus sign here
2402 dc
.SetPen(*wxGREY_PEN
);
2403 dc
.SetBrush(*wxWHITE_BRUSH
);
2404 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2405 dc
.SetPen(*wxBLACK_PEN
);
2406 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2407 if (!item
->IsExpanded())
2408 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2409 dc
.SetPen(m_dottedPen
);
2412 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2414 // draw the horizontal line here
2416 if (x
> (signed)m_indent
)
2417 x_start
-= m_indent
;
2418 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2420 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2424 if (item
->IsExpanded())
2426 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2427 int count
= children
.Count();
2434 PaintLevel(children
[n
], dc
, level
, y
);
2435 } while (++n
< count
);
2437 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2439 // draw line down to last child
2440 oldY
+= GetLineHeight(children
[n
-1])>>1;
2441 if (HasButtons()) y_mid
+= 5;
2443 // Only draw the portion of the line that is visible, in case it is huge
2444 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2445 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2446 yOrigin
= abs(yOrigin
);
2447 GetClientSize(&width
, &height
);
2449 // Move end points to the begining/end of the view?
2450 if (y_mid
< yOrigin
)
2452 if (oldY
> yOrigin
+ height
)
2453 oldY
= yOrigin
+ height
;
2455 // after the adjustments if y_mid is larger than oldY then the line
2456 // isn't visible at all so don't draw anything
2458 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2464 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2468 if ( item
->HasPlus() )
2470 // it's a folder, indicate it by a border
2475 // draw a line under the drop target because the item will be
2477 DrawLine(item
, TRUE
/* below */);
2480 SetCursor(wxCURSOR_BULLSEYE
);
2485 SetCursor(wxCURSOR_NO_ENTRY
);
2489 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2491 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2493 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2495 wxClientDC
dc(this);
2497 dc
.SetLogicalFunction(wxINVERT
);
2498 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2500 int w
= i
->GetWidth() + 2;
2501 int h
= GetLineHeight(i
) + 2;
2503 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2506 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2508 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2510 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2512 wxClientDC
dc(this);
2514 dc
.SetLogicalFunction(wxINVERT
);
2520 y
+= GetLineHeight(i
) - 1;
2523 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2526 // -----------------------------------------------------------------------------
2527 // wxWindows callbacks
2528 // -----------------------------------------------------------------------------
2530 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2538 dc
.SetFont( m_normalFont
);
2539 dc
.SetPen( m_dottedPen
);
2541 // this is now done dynamically
2542 //if(GetImageList() == NULL)
2543 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2546 PaintLevel( m_anchor
, dc
, 0, y
);
2549 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2558 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2567 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2569 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2570 te
.m_evtKey
= event
;
2571 te
.SetEventObject( this );
2572 if ( GetEventHandler()->ProcessEvent( te
) )
2574 // intercepted by the user code
2578 if ( (m_current
== 0) || (m_key_current
== 0) )
2584 // how should the selection work for this event?
2585 bool is_multiple
, extended_select
, unselect_others
;
2586 EventFlagsToSelType(GetWindowStyleFlag(),
2588 event
.ControlDown(),
2589 is_multiple
, extended_select
, unselect_others
);
2593 // * : Expand all/Collapse all
2594 // ' ' | return : activate
2595 // up : go up (not last children!)
2597 // left : go to parent
2598 // right : open if parent and go next
2599 // home : go to root
2600 // end : go to last item without opening parents
2601 // alnum : start or continue searching for the item with this prefix
2602 int keyCode
= event
.GetKeyCode();
2607 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2615 if ( !IsExpanded(m_current
) )
2618 ExpandAll(m_current
);
2621 //else: fall through to Collapse() it
2625 if (IsExpanded(m_current
))
2627 Collapse(m_current
);
2633 if ( !event
.HasModifiers() )
2635 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2636 event
.m_item
= (long) m_current
;
2637 event
.SetEventObject( this );
2638 GetEventHandler()->ProcessEvent( event
);
2641 // in any case, also generate the normal key event for this key,
2642 // even if we generated the ACTIVATED event above: this is what
2643 // wxMSW does and it makes sense because you might not want to
2644 // process ACTIVATED event at all and handle Space and Return
2645 // directly (and differently) which would be impossible otherwise
2649 // up goes to the previous sibling or to the last
2650 // of its children if it's expanded
2653 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2656 prev
= GetItemParent( m_key_current
);
2657 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2659 break; // don't go to root if it is hidden
2664 wxTreeItemId current
= m_key_current
;
2665 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2666 if (current
== GetFirstChild( prev
, cookie
))
2668 // otherwise we return to where we came from
2669 SelectItem( prev
, unselect_others
, extended_select
);
2670 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2677 while ( IsExpanded(prev
) && HasChildren(prev
) )
2679 wxTreeItemId child
= GetLastChild(prev
);
2686 SelectItem( prev
, unselect_others
, extended_select
);
2687 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2692 // left arrow goes to the parent
2695 wxTreeItemId prev
= GetItemParent( m_current
);
2696 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2698 // don't go to root if it is hidden
2699 prev
= GetPrevSibling( m_current
);
2703 SelectItem( prev
, unselect_others
, extended_select
);
2709 // this works the same as the down arrow except that we
2710 // also expand the item if it wasn't expanded yet
2716 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2719 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2720 SelectItem( child
, unselect_others
, extended_select
);
2721 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2725 wxTreeItemId next
= GetNextSibling( m_key_current
);
2728 wxTreeItemId current
= m_key_current
;
2729 while (current
&& !next
)
2731 current
= GetItemParent( current
);
2732 if (current
) next
= GetNextSibling( current
);
2737 SelectItem( next
, unselect_others
, extended_select
);
2738 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2744 // <End> selects the last visible tree item
2747 wxTreeItemId last
= GetRootItem();
2749 while ( last
.IsOk() && IsExpanded(last
) )
2751 wxTreeItemId lastChild
= GetLastChild(last
);
2753 // it may happen if the item was expanded but then all of
2754 // its children have been deleted - so IsExpanded() returned
2755 // TRUE, but GetLastChild() returned invalid item
2764 SelectItem( last
, unselect_others
, extended_select
);
2769 // <Home> selects the root item
2772 wxTreeItemId prev
= GetRootItem();
2776 if ( HasFlag(wxTR_HIDE_ROOT
) )
2779 prev
= GetFirstChild(prev
, dummy
);
2784 SelectItem( prev
, unselect_others
, extended_select
);
2789 // do not use wxIsalnum() here
2790 if ( !event
.HasModifiers() &&
2791 ((keyCode
>= '0' && keyCode
<= '9') ||
2792 (keyCode
>= 'a' && keyCode
<= 'z') ||
2793 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2795 // find the next item starting with the given prefix
2796 char ch
= (char)keyCode
;
2798 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2809 // also start the timer to reset the current prefix if the user
2810 // doesn't press any more alnum keys soon -- we wouldn't want
2811 // to use this prefix for a new item search
2814 m_findTimer
= new wxTreeFindTimer(this);
2817 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2826 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2828 // JACS: removed wxYieldIfNeeded() because it can cause the window
2829 // to be deleted from under us if a close window event is pending
2834 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2835 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2836 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2837 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2838 if (flags
) return wxTreeItemId();
2840 if (m_anchor
== NULL
)
2842 flags
= wxTREE_HITTEST_NOWHERE
;
2843 return wxTreeItemId();
2846 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2850 flags
= wxTREE_HITTEST_NOWHERE
;
2851 return wxTreeItemId();
2856 // get the bounding rectangle of the item (or of its label only)
2857 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2859 bool WXUNUSED(textOnly
)) const
2861 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2863 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2866 GetViewStart(& startX
, & startY
);
2868 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2869 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2870 rect
.width
= i
->GetWidth();
2871 //rect.height = i->GetHeight();
2872 rect
.height
= GetLineHeight(i
);
2877 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2879 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2881 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2883 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2884 te
.m_item
= (long) itemEdit
;
2885 te
.SetEventObject( this );
2886 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2892 // We have to call this here because the label in
2893 // question might just have been added and no screen
2894 // update taken place.
2898 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2900 m_textCtrl
->SetFocus();
2903 // returns a pointer to the text edit control if the item is being
2904 // edited, NULL otherwise (it's assumed that no more than one item may
2905 // be edited simultaneously)
2906 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2911 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2912 const wxString
& value
)
2914 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2915 le
.m_item
= (long) item
;
2916 le
.SetEventObject( this );
2918 le
.m_editCancelled
= FALSE
;
2920 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2923 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2925 // let owner know that the edit was cancelled
2926 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2927 le
.m_item
= (long) item
;
2928 le
.SetEventObject( this );
2929 le
.m_label
= wxEmptyString
;
2930 le
.m_editCancelled
= TRUE
;
2932 GetEventHandler()->ProcessEvent( le
);
2938 void wxGenericTreeCtrl::OnRenameTimer()
2943 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2945 if ( !m_anchor
) return;
2947 // we process left mouse up event (enables in-place edit), right down
2948 // (pass to the user code), left dbl click (activate item) and
2949 // dragging/moving events for items drag-and-drop
2950 if ( !(event
.LeftDown() ||
2952 event
.RightDown() ||
2953 event
.LeftDClick() ||
2955 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2962 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2965 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2967 if ( event
.Dragging() && !m_isDragging
)
2969 if (m_dragCount
== 0)
2974 if (m_dragCount
!= 3)
2976 // wait until user drags a bit further...
2980 wxEventType command
= event
.RightIsDown()
2981 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2982 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2984 wxTreeEvent
nevent( command
, GetId() );
2985 nevent
.m_item
= (long) m_current
;
2986 nevent
.SetEventObject(this);
2988 // by default the dragging is not supported, the user code must
2989 // explicitly allow the event for it to take place
2992 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2994 // we're going to drag this item
2995 m_isDragging
= TRUE
;
2997 // remember the old cursor because we will change it while
2999 m_oldCursor
= m_cursor
;
3001 // in a single selection control, hide the selection temporarily
3002 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3004 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3006 if ( m_oldSelection
)
3008 m_oldSelection
->SetHilight(FALSE
);
3009 RefreshLine(m_oldSelection
);
3016 else if ( event
.Moving() )
3018 if ( item
!= m_dropTarget
)
3020 // unhighlight the previous drop target
3021 DrawDropEffect(m_dropTarget
);
3023 m_dropTarget
= item
;
3025 // highlight the current drop target if any
3026 DrawDropEffect(m_dropTarget
);
3031 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3033 // erase the highlighting
3034 DrawDropEffect(m_dropTarget
);
3036 if ( m_oldSelection
)
3038 m_oldSelection
->SetHilight(TRUE
);
3039 RefreshLine(m_oldSelection
);
3040 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3043 // generate the drag end event
3044 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3046 event
.m_item
= (long) item
;
3047 event
.m_pointDrag
= pt
;
3048 event
.SetEventObject(this);
3050 (void)GetEventHandler()->ProcessEvent(event
);
3052 m_isDragging
= FALSE
;
3053 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3057 SetCursor(m_oldCursor
);
3063 // here we process only the messages which happen on tree items
3067 if (item
== NULL
) return; /* we hit the blank area */
3069 if ( event
.RightDown() )
3071 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3072 nevent
.m_item
= (long) item
;
3073 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3074 nevent
.SetEventObject(this);
3075 GetEventHandler()->ProcessEvent(nevent
);
3077 else if ( event
.LeftUp() )
3079 // this facilitates multiple-item drag-and-drop
3081 if (item
&& HasFlag(wxTR_MULTIPLE
))
3083 wxArrayTreeItemIds selections
;
3084 size_t count
= GetSelections(selections
);
3087 !event
.ControlDown() &&
3090 SelectItem(item
, true, false);
3096 if ( (item
== m_current
) &&
3097 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3098 HasFlag(wxTR_EDIT_LABELS
) )
3100 if ( m_renameTimer
)
3102 if ( m_renameTimer
->IsRunning() )
3103 m_renameTimer
->Stop();
3107 m_renameTimer
= new wxTreeRenameTimer( this );
3110 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
3113 m_lastOnSame
= FALSE
;
3116 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3118 if ( event
.LeftDown() )
3120 m_lastOnSame
= item
== m_current
;
3123 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3125 // only toggle the item for a single click, double click on
3126 // the button doesn't do anything (it toggles the item twice)
3127 if ( event
.LeftDown() )
3132 // don't select the item if the button was clicked
3137 // clear the previously selected items, if the
3138 // user clicked outside of the present selection.
3139 // otherwise, perform the deselection on mouse-up.
3140 // this allows multiple drag and drop to work.
3142 if (!IsSelected(item
))
3144 // how should the selection work for this event?
3145 bool is_multiple
, extended_select
, unselect_others
;
3146 EventFlagsToSelType(GetWindowStyleFlag(),
3148 event
.ControlDown(),
3149 is_multiple
, extended_select
, unselect_others
);
3151 SelectItem(item
, unselect_others
, extended_select
);
3155 // For some reason, Windows isn't recognizing a left double-click,
3156 // so we need to simulate it here. Allow 200 milliseconds for now.
3157 if ( event
.LeftDClick() )
3159 // double clicking should not start editing the item label
3160 if ( m_renameTimer
)
3161 m_renameTimer
->Stop();
3163 m_lastOnSame
= FALSE
;
3165 // send activate event first
3166 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3167 nevent
.m_item
= (long) item
;
3168 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3169 nevent
.SetEventObject( this );
3170 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3172 // if the user code didn't process the activate event,
3173 // handle it ourselves by toggling the item when it is
3175 if ( item
->HasPlus() )
3185 void wxGenericTreeCtrl::OnInternalIdle()
3187 wxWindow::OnInternalIdle();
3189 // Check if we need to select the root item
3190 // because nothing else has been selected.
3191 // Delaying it means that we can invoke event handlers
3192 // as required, when a first item is selected.
3193 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3196 SelectItem(m_select_me
);
3197 else if (GetRootItem().IsOk())
3198 SelectItem(GetRootItem());
3201 /* after all changes have been done to the tree control,
3202 * we actually redraw the tree when everything is over */
3204 if (!m_dirty
) return;
3208 CalculatePositions();
3210 AdjustMyScrollbars();
3213 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3218 wxTreeItemAttr
*attr
= item
->GetAttributes();
3219 if ( attr
&& attr
->HasFont() )
3220 dc
.SetFont(attr
->GetFont());
3221 else if ( item
->IsBold() )
3222 dc
.SetFont(m_boldFont
);
3224 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3227 // restore normal font
3228 dc
.SetFont( m_normalFont
);
3232 int image
= item
->GetCurrentImage();
3233 if ( image
!= NO_IMAGE
)
3235 if ( m_imageListNormal
)
3237 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3242 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3245 total_h
+= 2; // at least 2 pixels
3247 total_h
+= total_h
/10; // otherwise 10% extra spacing
3249 item
->SetHeight(total_h
);
3250 if (total_h
>m_lineHeight
)
3251 m_lineHeight
=total_h
;
3253 item
->SetWidth(image_w
+text_w
+2);
3256 // -----------------------------------------------------------------------------
3257 // for developper : y is now the top of the level
3258 // not the middle of it !
3259 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3261 int x
= level
*m_indent
;
3262 if (!HasFlag(wxTR_HIDE_ROOT
))
3266 else if (level
== 0)
3268 // a hidden root is not evaluated, but its
3269 // children are always calculated
3273 CalculateSize( item
, dc
);
3276 item
->SetX( x
+m_spacing
);
3278 y
+= GetLineHeight(item
);
3280 if ( !item
->IsExpanded() )
3282 // we don't need to calculate collapsed branches
3287 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3288 size_t n
, count
= children
.Count();
3290 for (n
= 0; n
< count
; ++n
)
3291 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3294 void wxGenericTreeCtrl::CalculatePositions()
3296 if ( !m_anchor
) return;
3298 wxClientDC
dc(this);
3301 dc
.SetFont( m_normalFont
);
3303 dc
.SetPen( m_dottedPen
);
3304 //if(GetImageList() == NULL)
3305 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3308 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3311 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3313 if (m_dirty
) return;
3315 wxSize client
= GetClientSize();
3318 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3319 rect
.width
= client
.x
;
3320 rect
.height
= client
.y
;
3322 Refresh(TRUE
, &rect
);
3324 AdjustMyScrollbars();
3327 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3329 if (m_dirty
) return;
3332 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3333 rect
.width
= GetClientSize().x
;
3334 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3336 Refresh(TRUE
, &rect
);
3339 void wxGenericTreeCtrl::RefreshSelected()
3341 // TODO: this is awfully inefficient, we should keep the list of all
3342 // selected items internally, should be much faster
3344 RefreshSelectedUnder(m_anchor
);
3347 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3349 if ( item
->IsSelected() )
3352 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3353 size_t count
= children
.GetCount();
3354 for ( size_t n
= 0; n
< count
; n
++ )
3356 RefreshSelectedUnder(children
[n
]);
3360 // ----------------------------------------------------------------------------
3361 // changing colours: we need to refresh the tree control
3362 // ----------------------------------------------------------------------------
3364 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3366 if ( !wxWindow::SetBackgroundColour(colour
) )
3374 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3376 if ( !wxWindow::SetForegroundColour(colour
) )
3384 #endif // wxUSE_TREECTRL