1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
8 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
21 #pragma implementation "treectlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/treebase.h"
34 #include "wx/generic/treectlg.h"
36 #include "wx/textctrl.h"
37 #include "wx/imaglist.h"
38 #include "wx/settings.h"
39 #include "wx/dcclient.h"
41 // -----------------------------------------------------------------------------
43 // -----------------------------------------------------------------------------
45 class WXDLLEXPORT wxGenericTreeItem
;
47 WX_DEFINE_EXPORTED_ARRAY(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 static const int NO_IMAGE
= -1;
55 static const int PIXELS_PER_UNIT
= 10;
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
62 static const char *aqua_arrow_right
[] = {
63 /* columns rows colors chars-per-pixel */
84 static const char *aqua_arrow_down
[] = {
85 /* columns rows colors chars-per-pixel */
105 // -----------------------------------------------------------------------------
107 // -----------------------------------------------------------------------------
109 // timer used for enabling in-place edit
110 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
113 // start editing the current item after half a second (if the mouse hasn't
114 // been clicked/moved)
115 enum { DELAY
= 500 };
117 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
119 virtual void Notify();
122 wxGenericTreeCtrl
*m_owner
;
125 // control used for in-place edit
126 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
129 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
132 void OnChar( wxKeyEvent
&event
);
133 void OnKeyUp( wxKeyEvent
&event
);
134 void OnKillFocus( wxFocusEvent
&event
);
136 bool AcceptChanges();
140 wxGenericTreeCtrl
*m_owner
;
141 wxGenericTreeItem
*m_itemEdited
;
142 wxString m_startValue
;
145 DECLARE_EVENT_TABLE()
148 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
149 // for a sufficiently long time
150 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
153 // reset the current prefix after half a second of inactivity
154 enum { DELAY
= 500 };
156 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
158 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
161 wxGenericTreeCtrl
*m_owner
;
165 class WXDLLEXPORT wxGenericTreeItem
169 wxGenericTreeItem() { m_data
= NULL
; }
170 wxGenericTreeItem( wxGenericTreeItem
*parent
,
171 const wxString
& text
,
174 wxTreeItemData
*data
);
176 ~wxGenericTreeItem();
179 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
181 const wxString
& GetText() const { return m_text
; }
182 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
183 { return m_images
[which
]; }
184 wxTreeItemData
*GetData() const { return m_data
; }
186 // returns the current image for the item (depending on its
187 // selected/expanded/whatever state)
188 int GetCurrentImage() const;
190 void SetText( const wxString
&text
);
191 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
192 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
194 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
196 void SetBold(bool bold
) { m_isBold
= bold
; }
198 int GetX() const { return m_x
; }
199 int GetY() const { return m_y
; }
201 void SetX(int x
) { m_x
= x
; }
202 void SetY(int y
) { m_y
= y
; }
204 int GetHeight() const { return m_height
; }
205 int GetWidth() const { return m_width
; }
207 void SetHeight(int h
) { m_height
= h
; }
208 void SetWidth(int w
) { m_width
= w
; }
210 wxGenericTreeItem
*GetParent() const { return m_parent
; }
213 // deletes all children notifying the treectrl about it if !NULL
215 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
217 // get count of all children (and grand children if 'recursively')
218 size_t GetChildrenCount(bool recursively
= TRUE
) const;
220 void Insert(wxGenericTreeItem
*child
, size_t index
)
221 { m_children
.Insert(child
, index
); }
223 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
225 // return the item at given position (or NULL if no item), onButton is
226 // TRUE if the point belongs to the item's button, otherwise it lies
227 // on the button's label
228 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
229 const wxGenericTreeCtrl
*,
233 void Expand() { m_isCollapsed
= FALSE
; }
234 void Collapse() { m_isCollapsed
= TRUE
; }
236 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
239 bool HasChildren() const { return !m_children
.IsEmpty(); }
240 bool IsSelected() const { return m_hasHilight
!= 0; }
241 bool IsExpanded() const { return !m_isCollapsed
; }
242 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
243 bool IsBold() const { return m_isBold
!= 0; }
246 // get them - may be NULL
247 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
248 // get them ensuring that the pointer is not NULL
249 wxTreeItemAttr
& Attr()
253 m_attr
= new wxTreeItemAttr
;
259 void SetAttributes(wxTreeItemAttr
*attr
)
261 if ( m_ownsAttr
) delete m_attr
;
265 // set them and delete when done
266 void AssignAttributes(wxTreeItemAttr
*attr
)
273 // since there can be very many of these, we save size by chosing
274 // the smallest representation for the elements and by ordering
275 // the members to avoid padding.
276 wxString m_text
; // label to be rendered for item
278 wxTreeItemData
*m_data
; // user-provided data
280 wxArrayGenericTreeItems m_children
; // list of children
281 wxGenericTreeItem
*m_parent
; // parent of this item
283 wxTreeItemAttr
*m_attr
; // attributes???
285 // tree ctrl images for the normal, selected, expanded and
286 // expanded+selected states
287 short m_images
[wxTreeItemIcon_Max
];
289 wxCoord m_x
; // (virtual) offset from top
290 short m_y
; // (virtual) offset from left
291 short m_width
; // width of this item
292 unsigned char m_height
; // height of this item
294 // use bitfields to save size
295 int m_isCollapsed
:1;
296 int m_hasHilight
:1; // same as focused
297 int m_hasPlus
:1; // used for item which doesn't have
298 // children but has a [+] button
299 int m_isBold
:1; // render the label in bold font
300 int m_ownsAttr
:1; // delete attribute when done
303 // =============================================================================
305 // =============================================================================
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 // translate the key or mouse event flags to the type of selection we're
313 static void EventFlagsToSelType(long style
,
317 bool &extended_select
,
318 bool &unselect_others
)
320 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
321 extended_select
= shiftDown
&& is_multiple
;
322 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
325 // check if the given item is under another one
326 static bool IsDescendantOf(wxGenericTreeItem
*parent
, wxGenericTreeItem
*item
)
330 if ( item
== parent
)
332 // item is a descendant of parent
336 item
= item
->GetParent();
342 // -----------------------------------------------------------------------------
343 // wxTreeRenameTimer (internal)
344 // -----------------------------------------------------------------------------
346 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
351 void wxTreeRenameTimer::Notify()
353 m_owner
->OnRenameTimer();
356 //-----------------------------------------------------------------------------
357 // wxTreeTextCtrl (internal)
358 //-----------------------------------------------------------------------------
360 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
361 EVT_CHAR (wxTreeTextCtrl::OnChar
)
362 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
363 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
366 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
367 wxGenericTreeItem
*item
)
368 : m_itemEdited(item
), m_startValue(item
->GetText())
373 int w
= m_itemEdited
->GetWidth(),
374 h
= m_itemEdited
->GetHeight();
377 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
382 int image
= item
->GetCurrentImage();
383 if ( image
!= NO_IMAGE
)
385 if ( m_owner
->m_imageListNormal
)
387 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
392 wxFAIL_MSG(_T("you must create an image list to use images!"));
396 // FIXME: what are all these hardcoded 4, 8 and 11s really?
400 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
401 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
404 bool wxTreeTextCtrl::AcceptChanges()
406 const wxString value
= GetValue();
408 if ( value
== m_startValue
)
410 // nothing changed, always accept
414 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
416 // vetoed by the user
420 // accepted, do rename the item
421 m_owner
->SetItemText(m_itemEdited
, value
);
426 void wxTreeTextCtrl::Finish()
430 m_owner
->ResetTextControl();
432 wxPendingDelete
.Append(this);
436 m_owner
->SetFocus(); // This doesn't work. TODO.
440 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
442 switch ( event
.m_keyCode
)
445 if ( !AcceptChanges() )
447 // vetoed by the user, don't disappear
454 m_owner
->OnRenameCancelled(m_itemEdited
);
462 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
466 // auto-grow the textctrl:
467 wxSize parentSize
= m_owner
->GetSize();
468 wxPoint myPos
= GetPosition();
469 wxSize mySize
= GetSize();
471 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
472 if (myPos
.x
+ sx
> parentSize
.x
)
473 sx
= parentSize
.x
- myPos
.x
;
482 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
490 if ( AcceptChanges() )
496 // -----------------------------------------------------------------------------
498 // -----------------------------------------------------------------------------
500 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
501 const wxString
& text
,
502 int image
, int selImage
,
503 wxTreeItemData
*data
)
506 m_images
[wxTreeItemIcon_Normal
] = image
;
507 m_images
[wxTreeItemIcon_Selected
] = selImage
;
508 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
509 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
514 m_isCollapsed
= TRUE
;
515 m_hasHilight
= FALSE
;
521 m_attr
= (wxTreeItemAttr
*)NULL
;
524 // We don't know the height here yet.
529 wxGenericTreeItem::~wxGenericTreeItem()
533 if (m_ownsAttr
) delete m_attr
;
535 wxASSERT_MSG( m_children
.IsEmpty(),
536 wxT("please call DeleteChildren() before deleting the item") );
539 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
541 size_t count
= m_children
.Count();
542 for ( size_t n
= 0; n
< count
; n
++ )
544 wxGenericTreeItem
*child
= m_children
[n
];
546 tree
->SendDeleteEvent(child
);
548 child
->DeleteChildren(tree
);
555 void wxGenericTreeItem::SetText( const wxString
&text
)
560 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
562 size_t count
= m_children
.Count();
566 size_t total
= count
;
567 for (size_t n
= 0; n
< count
; ++n
)
569 total
+= m_children
[n
]->GetChildrenCount();
575 void wxGenericTreeItem::GetSize( int &x
, int &y
,
576 const wxGenericTreeCtrl
*theButton
)
578 int bottomY
=m_y
+theButton
->GetLineHeight(this);
579 if ( y
< bottomY
) y
= bottomY
;
580 int width
= m_x
+ m_width
;
581 if ( x
< width
) x
= width
;
585 size_t count
= m_children
.Count();
586 for ( size_t n
= 0; n
< count
; ++n
)
588 m_children
[n
]->GetSize( x
, y
, theButton
);
593 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
594 const wxGenericTreeCtrl
*theCtrl
,
598 // for a hidden root node, don't evaluate it, but do evaluate children
599 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
602 int h
= theCtrl
->GetLineHeight(this);
603 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
605 int y_mid
= m_y
+ h
/2;
606 if (point
.y
< y_mid
)
607 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
609 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
611 // 5 is the size of the plus sign
612 int xCross
= m_x
- theCtrl
->GetSpacing();
613 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
614 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
615 HasPlus() && theCtrl
->HasButtons() )
617 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
621 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
626 // assuming every image (normal and selected) has the same size!
627 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
628 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
631 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
632 flags
|= wxTREE_HITTEST_ONITEMICON
;
634 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
640 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
641 if (point
.x
> m_x
+m_width
)
642 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
647 // if children are expanded, fall through to evaluate them
648 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
652 size_t count
= m_children
.Count();
653 for ( size_t n
= 0; n
< count
; n
++ )
655 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
663 return (wxGenericTreeItem
*) NULL
;
666 int wxGenericTreeItem::GetCurrentImage() const
668 int image
= NO_IMAGE
;
673 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
676 if ( image
== NO_IMAGE
)
678 // we usually fall back to the normal item, but try just the
679 // expanded one (and not selected) first in this case
680 image
= GetImage(wxTreeItemIcon_Expanded
);
686 image
= GetImage(wxTreeItemIcon_Selected
);
689 // maybe it doesn't have the specific image we want,
690 // try the default one instead
691 if ( image
== NO_IMAGE
) image
= GetImage();
696 // -----------------------------------------------------------------------------
697 // wxGenericTreeCtrl implementation
698 // -----------------------------------------------------------------------------
700 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
702 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
703 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
704 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
705 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
706 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
707 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
708 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
711 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
713 * wxTreeCtrl has to be a real class or we have problems with
714 * the run-time information.
717 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
720 // -----------------------------------------------------------------------------
721 // construction/destruction
722 // -----------------------------------------------------------------------------
724 void wxGenericTreeCtrl::Init()
726 m_current
= m_key_current
= m_anchor
= (wxGenericTreeItem
*) NULL
;
734 m_hilightBrush
= new wxBrush
736 wxSystemSettings::GetColour
738 wxSYS_COLOUR_HIGHLIGHT
743 m_hilightUnfocusedBrush
= new wxBrush
745 wxSystemSettings::GetColour
747 wxSYS_COLOUR_BTNSHADOW
752 m_imageListNormal
= m_imageListButtons
=
753 m_imageListState
= (wxImageList
*) NULL
;
754 m_ownsImageListNormal
= m_ownsImageListButtons
=
755 m_ownsImageListState
= FALSE
;
758 m_isDragging
= FALSE
;
759 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
762 m_renameTimer
= NULL
;
765 m_lastOnSame
= FALSE
;
767 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
768 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
769 m_normalFont
.GetFamily(),
770 m_normalFont
.GetStyle(),
772 m_normalFont
.GetUnderlined(),
773 m_normalFont
.GetFaceName(),
774 m_normalFont
.GetEncoding());
777 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
782 const wxValidator
&validator
,
783 const wxString
& name
)
787 wxGetOsVersion( &major
, &minor
);
789 if (style
& wxTR_HAS_BUTTONS
) style
|= wxTR_MAC_BUTTONS
;
790 if (style
& wxTR_HAS_BUTTONS
) style
&= ~wxTR_HAS_BUTTONS
;
791 style
&= ~wxTR_LINES_AT_ROOT
;
792 style
|= wxTR_NO_LINES
;
794 style
|= wxTR_ROW_LINES
;
796 style
|= wxTR_AQUA_BUTTONS
;
799 if (style
& wxTR_AQUA_BUTTONS
)
801 m_arrowRight
= new wxBitmap( aqua_arrow_right
);
802 m_arrowDown
= new wxBitmap( aqua_arrow_down
);
810 wxScrolledWindow::Create( parent
, id
, pos
, size
,
811 style
|wxHSCROLL
|wxVSCROLL
, name
);
813 // If the tree display has no buttons, but does have
814 // connecting lines, we can use a narrower layout.
815 // It may not be a good idea to force this...
816 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
823 SetValidator( validator
);
826 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
827 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
829 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
830 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
835 wxGenericTreeCtrl::~wxGenericTreeCtrl()
837 delete m_hilightBrush
;
838 delete m_hilightUnfocusedBrush
;
845 delete m_renameTimer
;
848 if (m_ownsImageListNormal
)
849 delete m_imageListNormal
;
850 if (m_ownsImageListState
)
851 delete m_imageListState
;
852 if (m_ownsImageListButtons
)
853 delete m_imageListButtons
;
856 // -----------------------------------------------------------------------------
858 // -----------------------------------------------------------------------------
860 size_t wxGenericTreeCtrl::GetCount() const
862 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
865 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
867 m_indent
= (unsigned short) indent
;
871 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
873 m_spacing
= (unsigned short) spacing
;
877 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
879 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
881 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
884 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
886 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
888 // if we will hide the root, make sure children are visible
889 m_anchor
->SetHasPlus();
891 CalculatePositions();
894 // right now, just sets the styles. Eventually, we may
895 // want to update the inherited styles, but right now
896 // none of the parents has updatable styles
897 m_windowStyle
= styles
;
901 // -----------------------------------------------------------------------------
902 // functions to work with tree items
903 // -----------------------------------------------------------------------------
905 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
907 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
909 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
912 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
913 wxTreeItemIcon which
) const
915 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
917 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
920 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
922 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
924 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
927 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
929 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
931 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
932 return pItem
->Attr().GetTextColour();
935 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
937 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
939 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
940 return pItem
->Attr().GetBackgroundColour();
943 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
945 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
947 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
948 return pItem
->Attr().GetFont();
951 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
953 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
956 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
957 pItem
->SetText(text
);
958 CalculateSize(pItem
, dc
);
962 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
964 wxTreeItemIcon which
)
966 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
968 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
969 pItem
->SetImage(image
, which
);
972 CalculateSize(pItem
, dc
);
976 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
978 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
980 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
983 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
985 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
987 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
988 pItem
->SetHasPlus(has
);
992 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
994 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
996 // avoid redrawing the tree if no real change
997 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
998 if ( pItem
->IsBold() != bold
)
1000 pItem
->SetBold(bold
);
1005 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1006 const wxColour
& col
)
1008 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1010 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1011 pItem
->Attr().SetTextColour(col
);
1015 void wxGenericTreeCtrl::SetItemBackgroundColour(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().SetBackgroundColour(col
);
1025 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1027 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1029 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1030 pItem
->Attr().SetFont(font
);
1034 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1036 wxScrolledWindow::SetFont(font
);
1038 m_normalFont
= font
;
1039 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1040 m_normalFont
.GetFamily(),
1041 m_normalFont
.GetStyle(),
1043 m_normalFont
.GetUnderlined(),
1044 m_normalFont
.GetFaceName(),
1045 m_normalFont
.GetEncoding());
1051 // -----------------------------------------------------------------------------
1052 // item status inquiries
1053 // -----------------------------------------------------------------------------
1055 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1057 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1059 // An item is only visible if it's not a descendant of a collapsed item
1060 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1061 wxGenericTreeItem
* parent
= pItem
->GetParent();
1064 if (!parent
->IsExpanded())
1066 parent
= parent
->GetParent();
1070 GetViewStart(& startX
, & startY
);
1072 wxSize clientSize
= GetClientSize();
1075 if (!GetBoundingRect(item
, rect
))
1077 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1079 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1081 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1087 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1089 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1091 // consider that the item does have children if it has the "+" button: it
1092 // might not have them (if it had never been expanded yet) but then it
1093 // could have them as well and it's better to err on this side rather than
1094 // disabling some operations which are restricted to the items with
1095 // children for an item which does have them
1096 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1099 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1101 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1103 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1106 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1108 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1110 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1113 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1115 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1117 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1120 // -----------------------------------------------------------------------------
1122 // -----------------------------------------------------------------------------
1124 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1126 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1128 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1131 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
1133 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1136 return GetNextChild(item
, cookie
);
1139 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
1141 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1143 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1144 if ( (size_t)cookie
< children
.Count() )
1146 return children
.Item((size_t)cookie
++);
1150 // there are no more of them
1151 return wxTreeItemId();
1155 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1157 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1159 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1160 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1163 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1165 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1167 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1168 wxGenericTreeItem
*parent
= i
->GetParent();
1169 if ( parent
== NULL
)
1171 // root item doesn't have any siblings
1172 return wxTreeItemId();
1175 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1176 int index
= siblings
.Index(i
);
1177 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1179 size_t n
= (size_t)(index
+ 1);
1180 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1183 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1185 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1187 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1188 wxGenericTreeItem
*parent
= i
->GetParent();
1189 if ( parent
== NULL
)
1191 // root item doesn't have any siblings
1192 return wxTreeItemId();
1195 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1196 int index
= siblings
.Index(i
);
1197 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1199 return index
== 0 ? wxTreeItemId()
1200 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1203 // Only for internal use right now, but should probably be public
1204 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1206 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1208 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1210 // First see if there are any children.
1211 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1212 if (children
.GetCount() > 0)
1214 return children
.Item(0);
1218 // Try a sibling of this or ancestor instead
1219 wxTreeItemId p
= item
;
1220 wxTreeItemId toFind
;
1223 toFind
= GetNextSibling(p
);
1224 p
= GetItemParent(p
);
1225 } while (p
.IsOk() && !toFind
.IsOk());
1230 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1232 wxTreeItemId id
= GetRootItem();
1241 } while (id
.IsOk());
1243 return wxTreeItemId();
1246 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1248 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1250 wxTreeItemId id
= item
;
1253 while (id
= GetNext(id
), id
.IsOk())
1259 return wxTreeItemId();
1262 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1264 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1266 wxFAIL_MSG(wxT("not implemented"));
1268 return wxTreeItemId();
1271 // called by wxTextTreeCtrl when it marks itself for deletion
1272 void wxGenericTreeCtrl::ResetTextControl()
1277 // find the first item starting with the given prefix after the given item
1278 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1279 const wxString
& prefixOrig
) const
1281 // match is case insensitive as this is more convenient to the user: having
1282 // to press Shift-letter to go to the item starting with a capital letter
1283 // would be too bothersome
1284 wxString prefix
= prefixOrig
.Lower();
1286 // determine the starting point: we shouldn't take the current item (this
1287 // allows to switch between two items starting with the same letter just by
1288 // pressing it) but we shouldn't jump to the next one if the user is
1289 // continuing to type as otherwise he might easily skip the item he wanted
1290 wxTreeItemId id
= idParent
;
1291 if ( prefix
.length() == 1 )
1296 // look for the item starting with the given prefix after it
1297 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1302 // if we haven't found anything...
1305 // ... wrap to the beginning
1307 if ( HasFlag(wxTR_HIDE_ROOT
) )
1309 // can't select virtual root
1313 // and try all the items (stop when we get to the one we started from)
1314 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1323 // -----------------------------------------------------------------------------
1325 // -----------------------------------------------------------------------------
1327 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1329 const wxString
& text
,
1330 int image
, int selImage
,
1331 wxTreeItemData
*data
)
1333 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1336 // should we give a warning here?
1337 return AddRoot(text
, image
, selImage
, data
);
1340 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1342 wxGenericTreeItem
*item
=
1343 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1347 data
->m_pItem
= (long) item
;
1350 parent
->Insert( item
, previous
);
1355 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1356 int image
, int selImage
,
1357 wxTreeItemData
*data
)
1359 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1361 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1363 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1364 image
, selImage
, data
);
1367 data
->m_pItem
= (long) m_anchor
;
1370 if (HasFlag(wxTR_HIDE_ROOT
))
1372 // if root is hidden, make sure we can navigate
1374 m_anchor
->SetHasPlus();
1376 CalculatePositions();
1379 if (!HasFlag(wxTR_MULTIPLE
))
1381 m_current
= m_key_current
= m_anchor
;
1382 m_current
->SetHilight( TRUE
);
1388 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1389 const wxString
& text
,
1390 int image
, int selImage
,
1391 wxTreeItemData
*data
)
1393 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1396 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1397 const wxTreeItemId
& idPrevious
,
1398 const wxString
& text
,
1399 int image
, int selImage
,
1400 wxTreeItemData
*data
)
1402 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1405 // should we give a warning here?
1406 return AddRoot(text
, image
, selImage
, data
);
1410 if (idPrevious
.IsOk())
1412 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1413 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1414 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1417 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1420 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1422 const wxString
& text
,
1423 int image
, int selImage
,
1424 wxTreeItemData
*data
)
1426 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1429 // should we give a warning here?
1430 return AddRoot(text
, image
, selImage
, data
);
1433 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1436 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1437 const wxString
& text
,
1438 int image
, int selImage
,
1439 wxTreeItemData
*data
)
1441 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1444 // should we give a warning here?
1445 return AddRoot(text
, image
, selImage
, data
);
1448 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1449 image
, selImage
, data
);
1452 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1454 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1455 event
.m_item
= (long) item
;
1456 event
.SetEventObject( this );
1457 ProcessEvent( event
);
1460 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1462 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1464 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1465 item
->DeleteChildren(this);
1468 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1470 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1472 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1474 wxGenericTreeItem
*parent
= item
->GetParent();
1476 // don't keep stale pointers around!
1477 if ( IsDescendantOf(item
, m_key_current
) )
1479 m_key_current
= parent
;
1482 if ( IsDescendantOf(item
, m_current
) )
1487 // remove the item from the tree
1490 parent
->GetChildren().Remove( item
); // remove by value
1492 else // deleting the root
1494 // nothing will be left in the tree
1498 // and delete all of its children and the item itself now
1499 item
->DeleteChildren(this);
1500 SendDeleteEvent(item
);
1504 void wxGenericTreeCtrl::DeleteAllItems()
1512 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1514 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1516 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1517 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1518 _T("can't expand hidden root") );
1520 if ( !item
->HasPlus() )
1523 if ( item
->IsExpanded() )
1526 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1527 event
.m_item
= (long) item
;
1528 event
.SetEventObject( this );
1530 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1532 // cancelled by program
1537 CalculatePositions();
1539 RefreshSubtree(item
);
1541 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1542 ProcessEvent( event
);
1545 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1547 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1550 if ( !IsExpanded(item
) )
1555 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1556 while ( child
.IsOk() )
1560 child
= GetNextChild(item
, cookie
);
1564 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1566 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1567 _T("can't collapse hidden root") );
1569 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1571 if ( !item
->IsExpanded() )
1574 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1575 event
.m_item
= (long) item
;
1576 event
.SetEventObject( this );
1577 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1579 // cancelled by program
1585 #if 0 // TODO why should items be collapsed recursively?
1586 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1587 size_t count
= children
.Count();
1588 for ( size_t n
= 0; n
< count
; n
++ )
1590 Collapse(children
[n
]);
1594 CalculatePositions();
1596 RefreshSubtree(item
);
1598 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1599 ProcessEvent( event
);
1602 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1605 DeleteChildren(item
);
1608 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1610 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1612 if (item
->IsExpanded())
1618 void wxGenericTreeCtrl::Unselect()
1622 m_current
->SetHilight( FALSE
);
1623 RefreshLine( m_current
);
1629 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1631 if (item
->IsSelected())
1633 item
->SetHilight(FALSE
);
1637 if (item
->HasChildren())
1639 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1640 size_t count
= children
.Count();
1641 for ( size_t n
= 0; n
< count
; ++n
)
1643 UnselectAllChildren(children
[n
]);
1648 void wxGenericTreeCtrl::UnselectAll()
1650 wxTreeItemId rootItem
= GetRootItem();
1652 // the tree might not have the root item at all
1655 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1659 // Recursive function !
1660 // To stop we must have crt_item<last_item
1662 // Tag all next children, when no more children,
1663 // Move to parent (not to tag)
1664 // Keep going... if we found last_item, we stop.
1665 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1667 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1669 if (parent
== NULL
) // This is root item
1670 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1672 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1673 int index
= children
.Index(crt_item
);
1674 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1676 size_t count
= children
.Count();
1677 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1679 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1682 return TagNextChildren(parent
, last_item
, select
);
1685 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1687 crt_item
->SetHilight(select
);
1688 RefreshLine(crt_item
);
1690 if (crt_item
==last_item
)
1693 if (crt_item
->HasChildren())
1695 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1696 size_t count
= children
.Count();
1697 for ( size_t n
= 0; n
< count
; ++n
)
1699 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1707 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1709 // item2 is not necessary after item1
1710 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1712 // choice first' and 'last' between item1 and item2
1713 if (item1
->GetY()<item2
->GetY())
1724 bool select
= m_current
->IsSelected();
1726 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1729 TagNextChildren(first
,last
,select
);
1732 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1733 bool unselect_others
,
1734 bool extended_select
)
1736 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1738 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1739 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1741 //wxCHECK_RET( ( (!unselect_others) && is_single),
1742 // wxT("this is a single selection tree") );
1744 // to keep going anyhow !!!
1747 if (item
->IsSelected())
1748 return; // nothing to do
1749 unselect_others
= TRUE
;
1750 extended_select
= FALSE
;
1752 else if ( unselect_others
&& item
->IsSelected() )
1754 // selection change if there is more than one item currently selected
1755 wxArrayTreeItemIds selected_items
;
1756 if ( GetSelections(selected_items
) == 1 )
1760 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1761 event
.m_item
= (long) item
;
1762 event
.m_itemOld
= (long) m_current
;
1763 event
.SetEventObject( this );
1764 // TODO : Here we don't send any selection mode yet !
1766 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1769 wxTreeItemId parent
= GetItemParent( itemId
);
1770 while (parent
.IsOk())
1772 if (!IsExpanded(parent
))
1775 parent
= GetItemParent( parent
);
1778 EnsureVisible( itemId
);
1781 if (unselect_others
)
1783 if (is_single
) Unselect(); // to speed up thing
1788 if (extended_select
)
1792 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1795 // don't change the mark (m_current)
1796 SelectItemRange(m_current
, item
);
1800 bool select
=TRUE
; // the default
1802 // Check if we need to toggle hilight (ctrl mode)
1803 if (!unselect_others
)
1804 select
=!item
->IsSelected();
1806 m_current
= m_key_current
= item
;
1807 m_current
->SetHilight(select
);
1808 RefreshLine( m_current
);
1811 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1812 GetEventHandler()->ProcessEvent( event
);
1815 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1816 wxArrayTreeItemIds
&array
) const
1818 if ( item
->IsSelected() )
1819 array
.Add(wxTreeItemId(item
));
1821 if ( item
->HasChildren() )
1823 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1824 size_t count
= children
.GetCount();
1825 for ( size_t n
= 0; n
< count
; ++n
)
1826 FillArray(children
[n
], array
);
1830 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1833 wxTreeItemId idRoot
= GetRootItem();
1834 if ( idRoot
.IsOk() )
1836 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1838 //else: the tree is empty, so no selections
1840 return array
.Count();
1843 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1845 if (!item
.IsOk()) return;
1847 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1849 // first expand all parent branches
1850 wxGenericTreeItem
*parent
= gitem
->GetParent();
1852 if ( HasFlag(wxTR_HIDE_ROOT
) )
1854 while ( parent
!= m_anchor
)
1857 parent
= parent
->GetParent();
1865 parent
= parent
->GetParent();
1869 //if (parent) CalculatePositions();
1874 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1876 if (!item
.IsOk()) return;
1878 // We have to call this here because the label in
1879 // question might just have been added and no screen
1880 // update taken place.
1881 if (m_dirty
) wxYieldIfNeeded();
1883 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1885 // now scroll to the item
1886 int item_y
= gitem
->GetY();
1890 GetViewStart( &start_x
, &start_y
);
1891 start_y
*= PIXELS_PER_UNIT
;
1895 GetClientSize( &client_w
, &client_h
);
1897 if (item_y
< start_y
+3)
1902 m_anchor
->GetSize( x
, y
, this );
1903 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1904 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1905 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1906 // Item should appear at top
1907 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1909 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1914 m_anchor
->GetSize( x
, y
, this );
1915 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1916 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1917 item_y
+= PIXELS_PER_UNIT
+2;
1918 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1919 // Item should appear at bottom
1920 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
);
1924 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1925 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1927 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1928 wxGenericTreeItem
**item2
)
1930 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1932 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1935 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1936 const wxTreeItemId
& item2
)
1938 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1941 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1943 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1945 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1947 wxCHECK_RET( !s_treeBeingSorted
,
1948 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1950 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1951 if ( children
.Count() > 1 )
1955 s_treeBeingSorted
= this;
1956 children
.Sort(tree_ctrl_compare_func
);
1957 s_treeBeingSorted
= NULL
;
1959 //else: don't make the tree dirty as nothing changed
1962 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1964 return m_imageListNormal
;
1967 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1969 return m_imageListButtons
;
1972 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1974 return m_imageListState
;
1977 void wxGenericTreeCtrl::CalculateLineHeight()
1979 wxClientDC
dc(this);
1980 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1982 if ( m_imageListNormal
)
1984 // Calculate a m_lineHeight value from the normal Image sizes.
1985 // May be toggle off. Then wxGenericTreeCtrl will spread when
1986 // necessary (which might look ugly).
1987 int n
= m_imageListNormal
->GetImageCount();
1988 for (int i
= 0; i
< n
; i
++)
1990 int width
= 0, height
= 0;
1991 m_imageListNormal
->GetSize(i
, width
, height
);
1992 if (height
> m_lineHeight
) m_lineHeight
= height
;
1996 if (m_imageListButtons
)
1998 // Calculate a m_lineHeight value from the Button image sizes.
1999 // May be toggle off. Then wxGenericTreeCtrl will spread when
2000 // necessary (which might look ugly).
2001 int n
= m_imageListButtons
->GetImageCount();
2002 for (int i
= 0; i
< n
; i
++)
2004 int width
= 0, height
= 0;
2005 m_imageListButtons
->GetSize(i
, width
, height
);
2006 if (height
> m_lineHeight
) m_lineHeight
= height
;
2010 if (m_lineHeight
< 30)
2011 m_lineHeight
+= 2; // at least 2 pixels
2013 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2016 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2018 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2019 m_imageListNormal
= imageList
;
2020 m_ownsImageListNormal
= FALSE
;
2022 // Don't do any drawing if we're setting the list to NULL,
2023 // since we may be in the process of deleting the tree control.
2025 CalculateLineHeight();
2028 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2030 if (m_ownsImageListState
) delete m_imageListState
;
2031 m_imageListState
= imageList
;
2032 m_ownsImageListState
= FALSE
;
2035 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2037 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2038 m_imageListButtons
= imageList
;
2039 m_ownsImageListButtons
= FALSE
;
2041 CalculateLineHeight();
2044 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2046 SetImageList(imageList
);
2047 m_ownsImageListNormal
= TRUE
;
2050 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2052 SetStateImageList(imageList
);
2053 m_ownsImageListState
= TRUE
;
2056 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2058 SetButtonsImageList(imageList
);
2059 m_ownsImageListButtons
= TRUE
;
2062 // -----------------------------------------------------------------------------
2064 // -----------------------------------------------------------------------------
2066 void wxGenericTreeCtrl::AdjustMyScrollbars()
2071 m_anchor
->GetSize( x
, y
, this );
2072 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2073 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2074 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2075 int y_pos
= GetScrollPos( wxVERTICAL
);
2076 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2080 SetScrollbars( 0, 0, 0, 0 );
2084 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2086 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2087 return item
->GetHeight();
2089 return m_lineHeight
;
2092 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2094 // TODO implement "state" icon on items
2096 wxTreeItemAttr
*attr
= item
->GetAttributes();
2097 if ( attr
&& attr
->HasFont() )
2098 dc
.SetFont(attr
->GetFont());
2099 else if (item
->IsBold())
2100 dc
.SetFont(m_boldFont
);
2102 long text_w
= 0, text_h
= 0;
2103 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2105 int image_h
= 0, image_w
= 0;
2106 int image
= item
->GetCurrentImage();
2107 if ( image
!= NO_IMAGE
)
2109 if ( m_imageListNormal
)
2111 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2120 int total_h
= GetLineHeight(item
);
2122 if ( item
->IsSelected() )
2124 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2129 if ( attr
&& attr
->HasBackgroundColour() )
2130 colBg
= attr
->GetBackgroundColour();
2132 colBg
= m_backgroundColour
;
2133 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2136 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2138 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2142 DoGetPosition(&x
, &y
);
2144 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2148 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2150 // If it's selected, and there's an image, then we should
2151 // take care to leave the area under the image painted in the
2152 // background colour.
2153 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2154 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2158 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2159 item
->GetWidth()+2, total_h
-offset
);
2163 if ( image
!= NO_IMAGE
)
2165 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2166 m_imageListNormal
->Draw( image
, dc
,
2168 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2169 wxIMAGELIST_DRAW_TRANSPARENT
);
2170 dc
.DestroyClippingRegion();
2173 dc
.SetBackgroundMode(wxTRANSPARENT
);
2174 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2175 dc
.DrawText( item
->GetText(),
2176 (wxCoord
)(image_w
+ item
->GetX()),
2177 (wxCoord
)(item
->GetY() + extraH
));
2179 // restore normal font
2180 dc
.SetFont( m_normalFont
);
2183 // Now y stands for the top of the item, whereas it used to stand for middle !
2184 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2186 int x
= level
*m_indent
;
2187 if (!HasFlag(wxTR_HIDE_ROOT
))
2191 else if (level
== 0)
2193 // always expand hidden root
2195 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2196 int count
= children
.Count();
2202 PaintLevel(children
[n
], dc
, 1, y
);
2203 } while (++n
< count
);
2205 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2207 // draw line down to last child
2208 origY
+= GetLineHeight(children
[0])>>1;
2209 oldY
+= GetLineHeight(children
[n
-1])>>1;
2210 dc
.DrawLine(3, origY
, 3, oldY
);
2216 item
->SetX(x
+m_spacing
);
2219 int h
= GetLineHeight(item
);
2221 int y_mid
= y_top
+ (h
>>1);
2224 int exposed_x
= dc
.LogicalToDeviceX(0);
2225 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2227 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2231 // don't draw rect outline if we already have the
2232 // background color under Mac
2233 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2234 #endif // !__WXMAC__
2238 if ( item
->IsSelected() )
2240 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2244 wxTreeItemAttr
*attr
= item
->GetAttributes();
2245 if (attr
&& attr
->HasTextColour())
2246 colText
= attr
->GetTextColour();
2248 colText
= GetForegroundColour();
2252 dc
.SetTextForeground(colText
);
2256 PaintItem(item
, dc
);
2258 if (HasFlag(wxTR_ROW_LINES
))
2260 // if the background colour is white, choose a
2261 // contrasting color for the lines
2262 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2263 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2264 dc
.DrawLine(0, y_top
, 10000, y_top
);
2265 dc
.DrawLine(0, y
, 10000, y
);
2268 // restore DC objects
2269 dc
.SetBrush(*wxWHITE_BRUSH
);
2270 dc
.SetPen(m_dottedPen
);
2271 dc
.SetTextForeground(*wxBLACK
);
2273 if (item
->HasPlus() && HasButtons()) // should the item show a button?
2275 if (!HasFlag(wxTR_NO_LINES
))
2277 if (x
> (signed)m_indent
)
2278 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
2279 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2280 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
2281 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
2284 if (m_imageListButtons
!= NULL
)
2286 // draw the image button here
2287 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
2288 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
2289 if (item
->IsSelected())
2290 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2291 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2292 int xx
= x
- (image_w
>>1);
2293 int yy
= y_mid
- (image_h
>>1);
2294 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
2295 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2296 wxIMAGELIST_DRAW_TRANSPARENT
);
2297 dc
.DestroyClippingRegion();
2299 else if (HasFlag(wxTR_TWIST_BUTTONS
))
2301 // draw the twisty button here
2303 if (HasFlag(wxTR_AQUA_BUTTONS
))
2305 if (item
->IsExpanded())
2306 dc
.DrawBitmap( *m_arrowDown
, x
-5, y_mid
-6, TRUE
);
2308 dc
.DrawBitmap( *m_arrowRight
, x
-5, y_mid
-6, TRUE
);
2312 dc
.SetBrush(*m_hilightBrush
);
2313 dc
.SetPen(*wxBLACK_PEN
);
2316 if (item
->IsExpanded())
2319 button
[0].y
= y_mid
-2;
2321 button
[1].y
= y_mid
-2;
2323 button
[2].y
= y_mid
+3;
2327 button
[0].y
= y_mid
-5;
2329 button
[1].y
= y_mid
+5;
2331 button
[2].y
= y_mid
;
2334 dc
.DrawPolygon(3, button
);
2335 dc
.SetPen(m_dottedPen
);
2338 else // if (HasFlag(wxTR_HAS_BUTTONS))
2340 // draw the plus sign here
2341 dc
.SetPen(*wxGREY_PEN
);
2342 dc
.SetBrush(*wxWHITE_BRUSH
);
2343 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2344 dc
.SetPen(*wxBLACK_PEN
);
2345 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2346 if (!item
->IsExpanded())
2347 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2348 dc
.SetPen(m_dottedPen
);
2351 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2353 // draw the horizontal line here
2355 if (x
> (signed)m_indent
)
2356 x_start
-= m_indent
;
2357 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2359 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2363 if (item
->IsExpanded())
2365 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2366 int count
= children
.Count();
2373 PaintLevel(children
[n
], dc
, level
, y
);
2374 } while (++n
< count
);
2376 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2378 // draw line down to last child
2379 oldY
+= GetLineHeight(children
[n
-1])>>1;
2380 if (HasButtons()) y_mid
+= 5;
2381 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2387 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2391 if ( item
->HasPlus() )
2393 // it's a folder, indicate it by a border
2398 // draw a line under the drop target because the item will be
2400 DrawLine(item
, TRUE
/* below */);
2403 SetCursor(wxCURSOR_BULLSEYE
);
2408 SetCursor(wxCURSOR_NO_ENTRY
);
2412 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2414 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2416 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2418 wxClientDC
dc(this);
2420 dc
.SetLogicalFunction(wxINVERT
);
2421 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2423 int w
= i
->GetWidth() + 2;
2424 int h
= GetLineHeight(i
) + 2;
2426 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2429 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2431 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2433 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2435 wxClientDC
dc(this);
2437 dc
.SetLogicalFunction(wxINVERT
);
2443 y
+= GetLineHeight(i
) - 1;
2446 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2449 // -----------------------------------------------------------------------------
2450 // wxWindows callbacks
2451 // -----------------------------------------------------------------------------
2453 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2461 dc
.SetFont( m_normalFont
);
2462 dc
.SetPen( m_dottedPen
);
2464 // this is now done dynamically
2465 //if(GetImageList() == NULL)
2466 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2469 PaintLevel( m_anchor
, dc
, 0, y
);
2472 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2481 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2490 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2492 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2493 te
.m_evtKey
= event
;
2494 te
.SetEventObject( this );
2495 if ( GetEventHandler()->ProcessEvent( te
) )
2497 // intercepted by the user code
2501 if ( (m_current
== 0) || (m_key_current
== 0) )
2507 // how should the selection work for this event?
2508 bool is_multiple
, extended_select
, unselect_others
;
2509 EventFlagsToSelType(GetWindowStyleFlag(),
2511 event
.ControlDown(),
2512 is_multiple
, extended_select
, unselect_others
);
2516 // * : Expand all/Collapse all
2517 // ' ' | return : activate
2518 // up : go up (not last children!)
2520 // left : go to parent
2521 // right : open if parent and go next
2522 // home : go to root
2523 // end : go to last item without opening parents
2524 // alnum : start or continue searching for the item with this prefix
2525 int keyCode
= event
.KeyCode();
2530 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2538 if ( !IsExpanded(m_current
) )
2541 ExpandAll(m_current
);
2544 //else: fall through to Collapse() it
2548 if (IsExpanded(m_current
))
2550 Collapse(m_current
);
2556 if ( !event
.HasModifiers() )
2558 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2559 event
.m_item
= (long) m_current
;
2560 event
.SetEventObject( this );
2561 GetEventHandler()->ProcessEvent( event
);
2564 // in any case, also generate the normal key event for this key,
2565 // even if we generated the ACTIVATED event above: this is what
2566 // wxMSW does and it makes sense because you might not want to
2567 // process ACTIVATED event at all and handle Space and Return
2568 // directly (and differently) which would be impossible otherwise
2572 // up goes to the previous sibling or to the last
2573 // of its children if it's expanded
2576 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2579 prev
= GetItemParent( m_key_current
);
2580 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2582 break; // don't go to root if it is hidden
2587 wxTreeItemId current
= m_key_current
;
2588 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2589 if (current
== GetFirstChild( prev
, cookie
))
2591 // otherwise we return to where we came from
2592 SelectItem( prev
, unselect_others
, extended_select
);
2593 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2600 while ( IsExpanded(prev
) && HasChildren(prev
) )
2602 wxTreeItemId child
= GetLastChild(prev
);
2609 SelectItem( prev
, unselect_others
, extended_select
);
2610 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2615 // left arrow goes to the parent
2618 wxTreeItemId prev
= GetItemParent( m_current
);
2619 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2621 // don't go to root if it is hidden
2622 prev
= GetPrevSibling( m_current
);
2626 SelectItem( prev
, unselect_others
, extended_select
);
2632 // this works the same as the down arrow except that we
2633 // also expand the item if it wasn't expanded yet
2639 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2642 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2643 SelectItem( child
, unselect_others
, extended_select
);
2644 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2648 wxTreeItemId next
= GetNextSibling( m_key_current
);
2651 wxTreeItemId current
= m_key_current
;
2652 while (current
&& !next
)
2654 current
= GetItemParent( current
);
2655 if (current
) next
= GetNextSibling( current
);
2660 SelectItem( next
, unselect_others
, extended_select
);
2661 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2667 // <End> selects the last visible tree item
2670 wxTreeItemId last
= GetRootItem();
2672 while ( last
.IsOk() && IsExpanded(last
) )
2674 wxTreeItemId lastChild
= GetLastChild(last
);
2676 // it may happen if the item was expanded but then all of
2677 // its children have been deleted - so IsExpanded() returned
2678 // TRUE, but GetLastChild() returned invalid item
2687 SelectItem( last
, unselect_others
, extended_select
);
2692 // <Home> selects the root item
2695 wxTreeItemId prev
= GetRootItem();
2699 if ( HasFlag(wxTR_HIDE_ROOT
) )
2702 prev
= GetFirstChild(prev
, dummy
);
2707 SelectItem( prev
, unselect_others
, extended_select
);
2712 // do not use wxIsalnum() here
2713 if ( !event
.HasModifiers() &&
2714 ((keyCode
>= '0' && keyCode
<= '9') ||
2715 (keyCode
>= 'a' && keyCode
<= 'z') ||
2716 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2718 // find the next item starting with the given prefix
2719 char ch
= (char)keyCode
;
2721 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2732 // also start the timer to reset the current prefix if the user
2733 // doesn't press any more alnum keys soon -- we wouldn't want
2734 // to use this prefix for a new item search
2737 m_findTimer
= new wxTreeFindTimer(this);
2740 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2749 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2751 // JACS: removed wxYieldIfNeeded() because it can cause the window
2752 // to be deleted from under us if a close window event is pending
2757 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2758 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2759 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2760 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2761 if (flags
) return wxTreeItemId();
2763 if (m_anchor
== NULL
)
2765 flags
= wxTREE_HITTEST_NOWHERE
;
2766 return wxTreeItemId();
2769 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2773 flags
= wxTREE_HITTEST_NOWHERE
;
2774 return wxTreeItemId();
2779 // get the bounding rectangle of the item (or of its label only)
2780 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2782 bool WXUNUSED(textOnly
)) const
2784 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2786 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2789 GetViewStart(& startX
, & startY
);
2791 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2792 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2793 rect
.width
= i
->GetWidth();
2794 //rect.height = i->GetHeight();
2795 rect
.height
= GetLineHeight(i
);
2800 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2802 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2804 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2806 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2807 te
.m_item
= (long) itemEdit
;
2808 te
.SetEventObject( this );
2809 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2815 // We have to call this here because the label in
2816 // question might just have been added and no screen
2817 // update taken place.
2821 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2823 m_textCtrl
->SetFocus();
2826 // returns a pointer to the text edit control if the item is being
2827 // edited, NULL otherwise (it's assumed that no more than one item may
2828 // be edited simultaneously)
2829 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2834 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2835 const wxString
& value
)
2837 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2838 le
.m_item
= (long) item
;
2839 le
.SetEventObject( this );
2841 le
.m_editCancelled
= FALSE
;
2843 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2846 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2848 // let owner know that the edit was cancelled
2849 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2850 le
.m_item
= (long) item
;
2851 le
.SetEventObject( this );
2852 le
.m_label
= wxEmptyString
;
2853 le
.m_editCancelled
= FALSE
;
2855 GetEventHandler()->ProcessEvent( le
);
2861 void wxGenericTreeCtrl::OnRenameTimer()
2866 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2868 if ( !m_anchor
) return;
2870 // we process left mouse up event (enables in-place edit), right down
2871 // (pass to the user code), left dbl click (activate item) and
2872 // dragging/moving events for items drag-and-drop
2873 if ( !(event
.LeftDown() ||
2875 event
.RightDown() ||
2876 event
.LeftDClick() ||
2878 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2885 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2888 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2890 if ( event
.Dragging() && !m_isDragging
)
2892 if (m_dragCount
== 0)
2897 if (m_dragCount
!= 3)
2899 // wait until user drags a bit further...
2903 wxEventType command
= event
.RightIsDown()
2904 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2905 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2907 wxTreeEvent
nevent( command
, GetId() );
2908 nevent
.m_item
= (long) m_current
;
2909 nevent
.SetEventObject(this);
2911 // by default the dragging is not supported, the user code must
2912 // explicitly allow the event for it to take place
2915 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2917 // we're going to drag this item
2918 m_isDragging
= TRUE
;
2920 // remember the old cursor because we will change it while
2922 m_oldCursor
= m_cursor
;
2924 // in a single selection control, hide the selection temporarily
2925 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2927 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2929 if ( m_oldSelection
)
2931 m_oldSelection
->SetHilight(FALSE
);
2932 RefreshLine(m_oldSelection
);
2939 else if ( event
.Moving() )
2941 if ( item
!= m_dropTarget
)
2943 // unhighlight the previous drop target
2944 DrawDropEffect(m_dropTarget
);
2946 m_dropTarget
= item
;
2948 // highlight the current drop target if any
2949 DrawDropEffect(m_dropTarget
);
2954 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2956 // erase the highlighting
2957 DrawDropEffect(m_dropTarget
);
2959 if ( m_oldSelection
)
2961 m_oldSelection
->SetHilight(TRUE
);
2962 RefreshLine(m_oldSelection
);
2963 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2966 // generate the drag end event
2967 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2969 event
.m_item
= (long) item
;
2970 event
.m_pointDrag
= pt
;
2971 event
.SetEventObject(this);
2973 (void)GetEventHandler()->ProcessEvent(event
);
2975 m_isDragging
= FALSE
;
2976 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2980 SetCursor(m_oldCursor
);
2986 // here we process only the messages which happen on tree items
2990 if (item
== NULL
) return; /* we hit the blank area */
2992 if ( event
.RightDown() )
2994 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2995 nevent
.m_item
= (long) item
;
2996 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
2997 nevent
.SetEventObject(this);
2998 GetEventHandler()->ProcessEvent(nevent
);
3000 else if ( event
.LeftUp() )
3004 if ( (item
== m_current
) &&
3005 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3006 HasFlag(wxTR_EDIT_LABELS
) )
3008 if ( m_renameTimer
)
3010 if ( m_renameTimer
->IsRunning() )
3011 m_renameTimer
->Stop();
3015 m_renameTimer
= new wxTreeRenameTimer( this );
3018 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
3021 m_lastOnSame
= FALSE
;
3024 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3026 if ( event
.LeftDown() )
3028 m_lastOnSame
= item
== m_current
;
3031 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3033 // only toggle the item for a single click, double click on
3034 // the button doesn't do anything (it toggles the item twice)
3035 if ( event
.LeftDown() )
3040 // don't select the item if the button was clicked
3044 // how should the selection work for this event?
3045 bool is_multiple
, extended_select
, unselect_others
;
3046 EventFlagsToSelType(GetWindowStyleFlag(),
3048 event
.ControlDown(),
3049 is_multiple
, extended_select
, unselect_others
);
3051 SelectItem(item
, unselect_others
, extended_select
);
3053 // For some reason, Windows isn't recognizing a left double-click,
3054 // so we need to simulate it here. Allow 200 milliseconds for now.
3055 if ( event
.LeftDClick() )
3057 // double clicking should not start editing the item label
3058 if ( m_renameTimer
)
3059 m_renameTimer
->Stop();
3061 m_lastOnSame
= FALSE
;
3063 // send activate event first
3064 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3065 nevent
.m_item
= (long) item
;
3066 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3067 nevent
.SetEventObject( this );
3068 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3070 // if the user code didn't process the activate event,
3071 // handle it ourselves by toggling the item when it is
3073 if ( item
->HasPlus() )
3083 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
3085 /* after all changes have been done to the tree control,
3086 * we actually redraw the tree when everything is over */
3088 if (!m_dirty
) return;
3092 CalculatePositions();
3094 AdjustMyScrollbars();
3097 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3102 wxTreeItemAttr
*attr
= item
->GetAttributes();
3103 if ( attr
&& attr
->HasFont() )
3104 dc
.SetFont(attr
->GetFont());
3105 else if ( item
->IsBold() )
3106 dc
.SetFont(m_boldFont
);
3108 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3111 // restore normal font
3112 dc
.SetFont( m_normalFont
);
3116 int image
= item
->GetCurrentImage();
3117 if ( image
!= NO_IMAGE
)
3119 if ( m_imageListNormal
)
3121 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3126 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3129 total_h
+= 2; // at least 2 pixels
3131 total_h
+= total_h
/10; // otherwise 10% extra spacing
3133 item
->SetHeight(total_h
);
3134 if (total_h
>m_lineHeight
)
3135 m_lineHeight
=total_h
;
3137 item
->SetWidth(image_w
+text_w
+2);
3140 // -----------------------------------------------------------------------------
3141 // for developper : y is now the top of the level
3142 // not the middle of it !
3143 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3145 int x
= level
*m_indent
;
3146 if (!HasFlag(wxTR_HIDE_ROOT
))
3150 else if (level
== 0)
3152 // a hidden root is not evaluated, but its
3153 // children are always calculated
3157 CalculateSize( item
, dc
);
3160 item
->SetX( x
+m_spacing
);
3162 y
+= GetLineHeight(item
);
3164 if ( !item
->IsExpanded() )
3166 // we don't need to calculate collapsed branches
3171 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3172 size_t n
, count
= children
.Count();
3174 for (n
= 0; n
< count
; ++n
)
3175 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3178 void wxGenericTreeCtrl::CalculatePositions()
3180 if ( !m_anchor
) return;
3182 wxClientDC
dc(this);
3185 dc
.SetFont( m_normalFont
);
3187 dc
.SetPen( m_dottedPen
);
3188 //if(GetImageList() == NULL)
3189 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3192 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3195 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3197 if (m_dirty
) return;
3199 wxSize client
= GetClientSize();
3202 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3203 rect
.width
= client
.x
;
3204 rect
.height
= client
.y
;
3206 Refresh(TRUE
, &rect
);
3208 AdjustMyScrollbars();
3211 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3213 if (m_dirty
) return;
3216 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3217 rect
.width
= GetClientSize().x
;
3218 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3220 Refresh(TRUE
, &rect
);
3223 void wxGenericTreeCtrl::RefreshSelected()
3225 // TODO: this is awfully inefficient, we should keep the list of all
3226 // selected items internally, should be much faster
3228 RefreshSelectedUnder(m_anchor
);
3231 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3233 if ( item
->IsSelected() )
3236 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3237 size_t count
= children
.GetCount();
3238 for ( size_t n
= 0; n
< count
; n
++ )
3240 RefreshSelectedUnder(children
[n
]);
3244 // ----------------------------------------------------------------------------
3245 // changing colours: we need to refresh the tree control
3246 // ----------------------------------------------------------------------------
3248 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3250 if ( !wxWindow::SetBackgroundColour(colour
) )
3258 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3260 if ( !wxWindow::SetForegroundColour(colour
) )
3268 #endif // wxUSE_TREECTRL