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 "treectrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #include "wx/generic/treectrl.h"
32 #include "wx/generic/imaglist.h"
33 #include "wx/settings.h"
36 #include "wx/dynarray.h"
37 #include "wx/dcclient.h"
38 #include "wx/msgdlg.h"
40 // -----------------------------------------------------------------------------
42 // -----------------------------------------------------------------------------
44 class WXDLLEXPORT wxGenericTreeItem
;
46 WX_DEFINE_ARRAY(wxGenericTreeItem
*, wxArrayTreeItems
);
48 // -----------------------------------------------------------------------------
50 // -----------------------------------------------------------------------------
53 class WXDLLEXPORT wxGenericTreeItem
57 wxGenericTreeItem() { m_data
= NULL
; }
58 wxGenericTreeItem( wxGenericTreeItem
*parent
,
61 int image
, int selImage
,
62 wxTreeItemData
*data
);
67 wxArrayTreeItems
& GetChildren() { return m_children
; }
69 const wxString
& GetText() const { return m_text
; }
70 int GetImage() const { return m_image
; }
71 int GetSelectedImage() const { return m_selImage
; }
72 wxTreeItemData
*GetData() const { return m_data
; }
74 void SetText( const wxString
&text
, wxDC
& dc
);
75 void SetImage(int image
) { m_image
= image
; }
76 void SetSelectedImage(int image
) { m_selImage
= image
; }
77 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
79 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
81 void SetBold(bool bold
) { m_isBold
= bold
; }
83 int GetX() const { return m_x
; }
84 int GetY() const { return m_y
; }
86 void SetHeight(int h
) { m_height
= h
; }
88 void SetX(int x
) { m_x
= x
; }
89 void SetY(int y
) { m_y
= y
; }
91 wxGenericTreeItem
*GetParent() const { return m_parent
; }
94 // deletes all children notifying the treectrl about it if !NULL pointer
96 void DeleteChildren(wxTreeCtrl
*tree
= NULL
);
97 // FIXME don't know what is it for
100 // get count of all children (and grand children if 'recursively')
101 size_t GetChildrenCount(bool recursively
= TRUE
) const;
103 void Insert(wxGenericTreeItem
*child
, size_t index
)
104 { m_children
.Insert(child
, index
); }
106 void SetCross( int x
, int y
);
107 void GetSize( int &x
, int &y
);
109 // return the item at given position (or NULL if no item), onButton is TRUE
110 // if the point belongs to the item's button, otherwise it lies on the
112 wxGenericTreeItem
*HitTest( const wxPoint
& point
, bool &onButton
);
114 void Expand() { m_isCollapsed
= FALSE
; }
115 void Collapse() { m_isCollapsed
= TRUE
; }
117 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
120 bool HasChildren() const { return !m_children
.IsEmpty(); }
121 bool HasHilight() const { return m_hasHilight
; }
122 bool IsExpanded() const { return !m_isCollapsed
; }
123 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
124 bool IsBold() const { return m_isBold
; }
132 wxTreeItemData
*m_data
;
134 // use bitfields to save size
135 int m_isCollapsed
:1;
136 int m_hasHilight
:1; // same as focused
137 int m_hasPlus
:1; // used for item which doesn't have
138 // children but still has a [+] button
139 int m_isBold
:1; // render the label in bold font
142 long m_height
, m_width
;
143 int m_xCross
, m_yCross
;
145 wxArrayTreeItems m_children
;
146 wxGenericTreeItem
*m_parent
;
149 // =============================================================================
151 // =============================================================================
153 // -----------------------------------------------------------------------------
155 // -----------------------------------------------------------------------------
157 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
159 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
160 : wxNotifyEvent( commandType
, id
)
163 m_itemOld
= (wxGenericTreeItem
*)NULL
;
166 // -----------------------------------------------------------------------------
168 // -----------------------------------------------------------------------------
170 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
171 const wxString
& text
,
173 int image
, int selImage
,
174 wxTreeItemData
*data
)
178 m_selImage
= selImage
;
181 m_xCross
= m_yCross
= 0;
185 m_isCollapsed
= TRUE
;
186 m_hasHilight
= FALSE
;
192 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
195 wxGenericTreeItem::~wxGenericTreeItem()
199 wxASSERT_MSG( m_children
.IsEmpty(),
200 "please call DeleteChildren() before deleting the item" );
203 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
205 size_t count
= m_children
.Count();
206 for ( size_t n
= 0; n
< count
; n
++ )
208 wxGenericTreeItem
*child
= m_children
[n
];
211 tree
->SendDeleteEvent(child
);
214 child
->DeleteChildren(tree
);
221 void wxGenericTreeItem::SetText( const wxString
&text
, wxDC
& dc
)
225 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
228 void wxGenericTreeItem::Reset()
235 m_height
= m_width
= 0;
242 m_isCollapsed
= TRUE
;
244 m_parent
= (wxGenericTreeItem
*)NULL
;
247 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
249 size_t count
= m_children
.Count();
253 size_t total
= count
;
254 for ( size_t n
= 0; n
< count
; n
++ )
256 total
+= m_children
[n
]->GetChildrenCount();
262 void wxGenericTreeItem::SetCross( int x
, int y
)
268 void wxGenericTreeItem::GetSize( int &x
, int &y
)
270 if ( y
< m_y
) y
= m_y
;
271 int width
= m_x
+ m_width
;
272 if (width
> x
) x
= width
;
276 size_t count
= m_children
.Count();
277 for ( size_t n
= 0; n
< count
; n
++ )
279 m_children
[n
]->GetSize( x
, y
);
284 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
287 if ((point
.y
> m_y
) && (point
.y
< m_y
+ m_height
))
290 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
291 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
292 (IsExpanded() || HasPlus()))
299 if (m_image
!= -1) w
+= 20;
301 if ((point
.x
> m_x
) && (point
.x
< m_x
+w
))
311 size_t count
= m_children
.Count();
312 for ( size_t n
= 0; n
< count
; n
++ )
314 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, onButton
);
324 // -----------------------------------------------------------------------------
325 // wxTreeCtrl implementation
326 // -----------------------------------------------------------------------------
328 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
330 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
331 EVT_PAINT (wxTreeCtrl::OnPaint
)
332 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
333 EVT_CHAR (wxTreeCtrl::OnChar
)
334 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
335 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
336 EVT_IDLE (wxTreeCtrl::OnIdle
)
339 // -----------------------------------------------------------------------------
340 // construction/destruction
341 // -----------------------------------------------------------------------------
342 void wxTreeCtrl::Init()
345 m_anchor
= (wxGenericTreeItem
*) NULL
;
354 m_hilightBrush
= new wxBrush
356 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
361 m_imageListState
= (wxImageList
*) NULL
;
364 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
365 const wxPoint
& pos
, const wxSize
& size
,
367 const wxValidator
&validator
,
368 const wxString
& name
)
372 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
374 SetValidator( validator
);
376 SetBackgroundColour( *wxWHITE
);
377 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
382 wxTreeCtrl::~wxTreeCtrl()
384 wxDELETE( m_hilightBrush
);
389 // -----------------------------------------------------------------------------
391 // -----------------------------------------------------------------------------
393 size_t wxTreeCtrl::GetCount() const
395 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
398 void wxTreeCtrl::SetIndent(unsigned int indent
)
404 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
406 wxCHECK_MSG( item
.IsOk(), 0u, "invalid tree item" );
408 return item
.m_pItem
->GetChildrenCount(recursively
);
411 // -----------------------------------------------------------------------------
412 // functions to work with tree items
413 // -----------------------------------------------------------------------------
415 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
417 wxCHECK_MSG( item
.IsOk(), "", "invalid tree item" );
419 return item
.m_pItem
->GetText();
422 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
424 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
426 return item
.m_pItem
->GetImage();
429 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
431 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
433 return item
.m_pItem
->GetSelectedImage();
436 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
438 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
440 return item
.m_pItem
->GetData();
443 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
445 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
448 item
.m_pItem
->SetText(text
, dc
);
451 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
453 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
455 item
.m_pItem
->SetImage(image
);
458 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
460 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
462 item
.m_pItem
->SetSelectedImage(image
);
465 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
467 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
469 item
.m_pItem
->SetData(data
);
472 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
474 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
476 item
.m_pItem
->SetHasPlus(has
);
479 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
481 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
483 // avoid redrawing the tree if no real change
484 wxGenericTreeItem
*pItem
= item
.m_pItem
;
485 if ( pItem
->IsBold() != bold
)
487 pItem
->SetBold(bold
);
492 // -----------------------------------------------------------------------------
493 // item status inquiries
494 // -----------------------------------------------------------------------------
496 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
498 wxFAIL_MSG("not implemented");
503 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
505 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
507 return !item
.m_pItem
->GetChildren().IsEmpty();
510 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
512 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
514 return item
.m_pItem
->IsExpanded();
517 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
519 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
521 return item
.m_pItem
->HasHilight();
524 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
526 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
528 return item
.m_pItem
->IsBold();
531 // -----------------------------------------------------------------------------
533 // -----------------------------------------------------------------------------
535 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
537 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
539 return item
.m_pItem
->GetParent();
542 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
544 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
547 return GetNextChild(item
, cookie
);
550 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
552 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
554 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
555 if ( (size_t)cookie
< children
.Count() )
557 return item
.m_pItem
->GetChildren().Item(cookie
++);
561 // there are no more of them
562 return wxTreeItemId();
566 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
568 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
570 wxGenericTreeItem
*i
= item
.m_pItem
;
571 wxGenericTreeItem
*parent
= i
->GetParent();
572 if ( parent
== NULL
)
574 // root item doesn't have any siblings
575 return wxTreeItemId();
578 wxArrayTreeItems
& siblings
= parent
->GetChildren();
579 int index
= siblings
.Index(i
);
580 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
582 size_t n
= (size_t)(index
+ 1);
583 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
586 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
588 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
590 wxGenericTreeItem
*i
= item
.m_pItem
;
591 wxGenericTreeItem
*parent
= i
->GetParent();
592 if ( parent
== NULL
)
594 // root item doesn't have any siblings
595 return wxTreeItemId();
598 wxArrayTreeItems
& siblings
= parent
->GetChildren();
599 int index
= siblings
.Index(i
);
600 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
602 return index
== 0 ? wxTreeItemId()
603 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
606 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
608 wxFAIL_MSG("not implemented");
610 return wxTreeItemId();
613 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
615 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
617 wxFAIL_MSG("not implemented");
619 return wxTreeItemId();
622 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
624 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
626 wxFAIL_MSG("not implemented");
628 return wxTreeItemId();
631 // -----------------------------------------------------------------------------
633 // -----------------------------------------------------------------------------
635 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
637 const wxString
& text
,
638 int image
, int selImage
,
639 wxTreeItemData
*data
)
641 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
644 // should we give a warning here?
645 return AddRoot(text
, image
, selImage
, data
);
649 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
656 data
->m_pItem
= item
;
659 parent
->Insert( item
, previous
);
666 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
667 int image
, int selImage
,
668 wxTreeItemData
*data
)
670 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), "tree can have only one root" );
673 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
674 image
, selImage
, data
);
677 data
->m_pItem
= m_anchor
;
680 AdjustMyScrollbars();
686 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
687 const wxString
& text
,
688 int image
, int selImage
,
689 wxTreeItemData
*data
)
691 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
694 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
695 const wxTreeItemId
& idPrevious
,
696 const wxString
& text
,
697 int image
, int selImage
,
698 wxTreeItemData
*data
)
700 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
703 // should we give a warning here?
704 return AddRoot(text
, image
, selImage
, data
);
707 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
708 wxASSERT_MSG( index
!= NOT_FOUND
,
709 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
710 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
713 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
714 const wxString
& text
,
715 int image
, int selImage
,
716 wxTreeItemData
*data
)
718 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
721 // should we give a warning here?
722 return AddRoot(text
, image
, selImage
, data
);
725 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
726 image
, selImage
, data
);
729 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
731 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
733 event
.SetEventObject( this );
734 ProcessEvent( event
);
737 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
739 wxGenericTreeItem
*item
= itemId
.m_pItem
;
740 item
->DeleteChildren(this);
745 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
747 wxGenericTreeItem
*item
= itemId
.m_pItem
;
748 wxGenericTreeItem
*parent
= item
->GetParent();
752 parent
->GetChildren().Remove(item
);
755 item
->DeleteChildren(this);
756 SendDeleteEvent(item
);
762 void wxTreeCtrl::DeleteAllItems()
766 m_anchor
->DeleteChildren(this);
775 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
777 wxGenericTreeItem
*item
= itemId
.m_pItem
;
779 if ( !item
->HasPlus() )
782 if ( item
->IsExpanded() )
785 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
787 event
.SetEventObject( this );
788 if ( ProcessEvent( event
) && event
.m_code
)
790 // cancelled by program
795 CalculatePositions();
797 RefreshSubtree(item
);
799 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
800 ProcessEvent( event
);
803 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
805 wxGenericTreeItem
*item
= itemId
.m_pItem
;
807 if ( !item
->IsExpanded() )
810 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
812 event
.SetEventObject( this );
813 if ( ProcessEvent( event
) && event
.m_code
)
815 // cancelled by program
821 wxArrayTreeItems
& children
= item
->GetChildren();
822 size_t count
= children
.Count();
823 for ( size_t n
= 0; n
< count
; n
++ )
825 Collapse(children
[n
]);
828 CalculatePositions();
830 RefreshSubtree(item
);
832 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
833 ProcessEvent( event
);
836 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
839 DeleteChildren(item
);
842 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
844 wxGenericTreeItem
*item
= itemId
.m_pItem
;
846 if ( item
->IsExpanded() )
852 void wxTreeCtrl::Unselect()
856 m_current
->SetHilight( FALSE
);
857 RefreshLine( m_current
);
861 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
863 wxGenericTreeItem
*item
= itemId
.m_pItem
;
865 if ( m_current
!= item
)
867 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
869 event
.m_itemOld
= m_current
;
870 event
.SetEventObject( this );
871 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
876 m_current
->SetHilight( FALSE
);
877 RefreshLine( m_current
);
881 m_current
->SetHilight( TRUE
);
882 RefreshLine( m_current
);
884 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
885 GetEventHandler()->ProcessEvent( event
);
889 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
891 wxGenericTreeItem
*gitem
= item
.m_pItem
;
893 int item_y
= gitem
->GetY();
897 ViewStart( &start_x
, &start_y
);
902 GetClientSize( &client_w
, &client_h
);
904 if (item_y
< start_y
+3)
908 m_anchor
->GetSize( x
, y
);
910 int x_pos
= GetScrollPos( wxHORIZONTAL
);
911 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
915 if (item_y
> start_y
+client_h
-16)
919 m_anchor
->GetSize( x
, y
);
921 int x_pos
= GetScrollPos( wxHORIZONTAL
);
922 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
927 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
929 wxFAIL_MSG("not implemented");
932 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
933 wxClassInfo
* WXUNUSED(textCtrlClass
) )
935 wxFAIL_MSG("not implemented");
937 return (wxTextCtrl
*)NULL
;
940 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
942 wxFAIL_MSG("not implemented");
944 return (wxTextCtrl
*)NULL
;
947 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
949 wxFAIL_MSG("not implemented");
952 // FIXME: tree sorting functions are not reentrant and not MT-safe!
953 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
955 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
956 wxGenericTreeItem
**item2
)
958 wxCHECK_MSG( s_treeBeingSorted
, 0, "bug in wxTreeCtrl::SortChildren()" );
960 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
963 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
964 const wxTreeItemId
& item2
)
966 return strcmp(GetItemText(item1
), GetItemText(item2
));
969 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
971 wxCHECK_RET( itemId
.IsOk(), "invalid tree item" );
973 wxGenericTreeItem
*item
= itemId
.m_pItem
;
975 wxCHECK_RET( !s_treeBeingSorted
,
976 "wxTreeCtrl::SortChildren is not reentrant" );
978 wxArrayTreeItems
& children
= item
->GetChildren();
979 if ( children
.Count() > 1 )
981 s_treeBeingSorted
= this;
982 children
.Sort(tree_ctrl_compare_func
);
983 s_treeBeingSorted
= NULL
;
987 //else: don't make the tree dirty as nothing changed
990 wxImageList
*wxTreeCtrl::GetImageList() const
992 return m_imageListNormal
;
995 wxImageList
*wxTreeCtrl::GetStateImageList() const
997 return m_imageListState
;
1000 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1002 m_imageListNormal
= imageList
;
1005 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1007 m_imageListState
= imageList
;
1010 // -----------------------------------------------------------------------------
1012 // -----------------------------------------------------------------------------
1014 void wxTreeCtrl::AdjustMyScrollbars()
1020 m_anchor
->GetSize( x
, y
);
1021 y
+= 2*m_lineHeight
;
1022 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1023 int y_pos
= GetScrollPos( wxVERTICAL
);
1024 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
1028 SetScrollbars( 0, 0, 0, 0 );
1032 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1034 // render bold items in bold
1038 if ( item
->IsBold() )
1040 fontOld
= dc
.GetFont();
1043 // @@ is there any better way to make a bold variant of old font?
1044 fontNew
= wxFont( fontOld
.GetPointSize(),
1045 fontOld
.GetFamily(),
1048 fontOld
.GetUnderlined());
1049 dc
.SetFont(fontNew
);
1053 wxFAIL_MSG("wxDC::GetFont() failed!");
1059 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1063 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1065 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1068 else if (item
->GetImage() != -1)
1070 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1074 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
1076 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1078 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1079 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1080 item
->GetX(), item
->GetY()-1,
1081 wxIMAGELIST_DRAW_TRANSPARENT
);
1082 dc
.DestroyClippingRegion();
1084 else if (item
->GetImage() != -1)
1086 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1087 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1088 item
->GetX(), item
->GetY()-1,
1089 wxIMAGELIST_DRAW_TRANSPARENT
);
1090 dc
.DestroyClippingRegion();
1093 dc
.SetBackgroundMode(wxTRANSPARENT
);
1094 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY() );
1096 // restore normal font for bold items
1099 dc
.SetFont( fontOld
);
1103 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1105 int horizX
= level
*m_indent
;
1107 item
->SetX( horizX
+33 );
1108 item
->SetY( y
-m_lineHeight
/3 );
1109 item
->SetHeight( m_lineHeight
);
1111 item
->SetCross( horizX
+15, y
);
1115 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1116 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1118 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
1120 int startX
= horizX
;
1121 int endX
= horizX
+ 10;
1123 if (!item
->HasChildren()) endX
+= 20;
1125 dc
.DrawLine( startX
, y
, endX
, y
);
1127 if (item
->HasPlus())
1129 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
1130 dc
.SetPen( *wxGREY_PEN
);
1131 dc
.SetBrush( *wxWHITE_BRUSH
);
1132 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
1133 dc
.SetPen( *wxBLACK_PEN
);
1134 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
1136 if (!item
->IsExpanded())
1137 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
1140 if (item
->HasHilight())
1142 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1144 dc
.SetBrush( *m_hilightBrush
);
1147 dc
.SetPen( *wxBLACK_PEN
);
1149 dc
.SetPen( *wxTRANSPARENT_PEN
);
1151 PaintItem(item
, dc
);
1153 dc
.SetPen( *wxBLACK_PEN
);
1154 dc
.SetTextForeground( *wxBLACK
);
1155 dc
.SetBrush( *wxWHITE_BRUSH
);
1159 dc
.SetBrush( *wxWHITE_BRUSH
);
1160 dc
.SetPen( *wxTRANSPARENT_PEN
);
1162 PaintItem(item
, dc
);
1164 dc
.SetPen( *wxBLACK_PEN
);
1168 if ( item
->IsExpanded() )
1172 wxArrayTreeItems
& children
= item
->GetChildren();
1173 size_t count
= children
.Count();
1174 for ( size_t n
= 0; n
< count
; n
++ )
1179 PaintLevel( children
[n
], dc
, level
+1, y
);
1182 // it may happen that the item is expanded but has no items (when you
1183 // delete all its children for example) - don't draw the vertical line
1186 dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1190 // -----------------------------------------------------------------------------
1191 // wxWindows callbacks
1192 // -----------------------------------------------------------------------------
1194 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1202 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1204 dc
.SetPen( m_dottedPen
);
1205 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1207 int y
= m_lineHeight
/ 2 + 2;
1208 PaintLevel( m_anchor
, dc
, 0, y
);
1211 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1215 RefreshLine( m_current
);
1218 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1222 RefreshLine( m_current
);
1225 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1227 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1228 te
.m_code
= event
.KeyCode();
1229 te
.SetEventObject( this );
1230 GetEventHandler()->ProcessEvent( te
);
1238 switch (event
.KeyCode())
1242 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1250 if (IsExpanded(m_current
))
1252 Collapse(m_current
);
1264 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1265 event
.m_item
= m_current
;
1267 event
.SetEventObject( this );
1268 GetEventHandler()->ProcessEvent( event
);
1274 wxTreeItemId prev
= GetPrevSibling( m_current
);
1277 prev
= GetParent( m_current
);
1279 wxTreeItemId current
= m_current
;
1280 if (current
== GetFirstChild( prev
, cockie
))
1282 // otherwise we return to where we came from
1284 EnsureVisible( prev
);
1290 while (IsExpanded(prev
))
1292 int c
= (int)GetChildrenCount( prev
, FALSE
);
1294 prev
= GetFirstChild( prev
, cockie
);
1295 for (int i
= 0; i
< c
-1; i
++)
1296 prev
= GetNextSibling( prev
);
1299 EnsureVisible( prev
);
1305 wxTreeItemId prev
= GetPrevSibling( m_current
);
1309 EnsureVisible( prev
);
1313 prev
= GetParent( m_current
);
1316 EnsureVisible( prev
);
1324 // this works the same as the down arrow except that we also expand the
1325 // item if it wasn't expanded yet
1331 if (IsExpanded(m_current
))
1334 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1335 SelectItem( child
);
1336 EnsureVisible( child
);
1340 wxTreeItemId next
= GetNextSibling( m_current
);
1343 wxTreeItemId current
= m_current
;
1344 while (current
&& !next
)
1346 current
= GetParent( current
);
1347 if (current
) next
= GetNextSibling( current
);
1353 EnsureVisible( next
);
1364 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& WXUNUSED(flags
))
1366 bool onButton
= FALSE
;
1367 return m_anchor
->HitTest( point
, onButton
);
1370 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1372 if ( !(event
.LeftDown() || event
.LeftDClick()) )
1378 wxClientDC
dc(this);
1380 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1381 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1383 bool onButton
= FALSE
;
1384 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1388 if (!IsSelected(item
)) SelectItem(item
);
1390 if ( event
.LeftDClick() )
1392 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1393 event
.m_item
= item
;
1395 event
.SetEventObject( this );
1396 GetEventHandler()->ProcessEvent( event
);
1405 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1407 if (!m_dirty
) return;
1411 CalculatePositions();
1413 AdjustMyScrollbars();
1416 // -----------------------------------------------------------------------------
1417 // -----------------------------------------------------------------------------
1418 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
,
1423 int horizX
= level
*m_indent
;
1425 item
->SetX( horizX
+33 );
1426 item
->SetY( y
-m_lineHeight
/3-2 );
1427 item
->SetHeight( m_lineHeight
);
1429 // if ( item->IsExpanded() )
1431 if ( !item
->IsExpanded() ) // Surely this is correct? JACS
1434 wxArrayTreeItems
& children
= item
->GetChildren();
1435 size_t count
= children
.Count();
1436 for ( size_t n
= 0; n
< count
; n
++ )
1439 CalculateLevel( children
[n
], dc
, level
+1, y
);
1443 void wxTreeCtrl::CalculatePositions()
1448 wxClientDC
dc(this);
1451 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1453 dc
.SetPen( m_dottedPen
);
1454 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1456 int y
= m_lineHeight
/ 2 + 2;
1457 CalculateLevel( m_anchor
, dc
, 0, y
);
1460 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1462 wxClientDC
dc(this);
1467 GetClientSize( &cw
, &ch
);
1470 rect
.x
= dc
.LogicalToDeviceX( 0 );
1472 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1475 Refresh( TRUE
, &rect
);
1477 AdjustMyScrollbars();
1480 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1482 wxClientDC
dc(this);
1486 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1487 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1489 rect
.height
= dc
.GetCharHeight() + 6;
1490 Refresh( TRUE
, &rect
);