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
;
366 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
367 const wxPoint
& pos
, const wxSize
& size
,
369 const wxValidator
&validator
,
370 const wxString
& name
)
374 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
376 SetValidator( validator
);
378 SetBackgroundColour( *wxWHITE
);
379 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
384 wxTreeCtrl::~wxTreeCtrl()
386 wxDELETE( m_hilightBrush
);
391 // -----------------------------------------------------------------------------
393 // -----------------------------------------------------------------------------
395 size_t wxTreeCtrl::GetCount() const
397 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
400 void wxTreeCtrl::SetIndent(unsigned int indent
)
406 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
408 wxCHECK_MSG( item
.IsOk(), 0u, "invalid tree item" );
410 return item
.m_pItem
->GetChildrenCount(recursively
);
413 // -----------------------------------------------------------------------------
414 // functions to work with tree items
415 // -----------------------------------------------------------------------------
417 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
419 wxCHECK_MSG( item
.IsOk(), "", "invalid tree item" );
421 return item
.m_pItem
->GetText();
424 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
426 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
428 return item
.m_pItem
->GetImage();
431 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
433 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
435 return item
.m_pItem
->GetSelectedImage();
438 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
440 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
442 return item
.m_pItem
->GetData();
445 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
447 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
450 item
.m_pItem
->SetText(text
, dc
);
453 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
455 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
457 item
.m_pItem
->SetImage(image
);
460 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
462 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
464 item
.m_pItem
->SetSelectedImage(image
);
467 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
469 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
471 item
.m_pItem
->SetData(data
);
474 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
476 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
478 item
.m_pItem
->SetHasPlus(has
);
481 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
483 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
485 // avoid redrawing the tree if no real change
486 wxGenericTreeItem
*pItem
= item
.m_pItem
;
487 if ( pItem
->IsBold() != bold
)
489 pItem
->SetBold(bold
);
494 // -----------------------------------------------------------------------------
495 // item status inquiries
496 // -----------------------------------------------------------------------------
498 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
500 wxFAIL_MSG("not implemented");
505 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
507 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
509 return !item
.m_pItem
->GetChildren().IsEmpty();
512 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
514 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
516 return item
.m_pItem
->IsExpanded();
519 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
521 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
523 return item
.m_pItem
->HasHilight();
526 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
528 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
530 return item
.m_pItem
->IsBold();
533 // -----------------------------------------------------------------------------
535 // -----------------------------------------------------------------------------
537 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
539 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
541 return item
.m_pItem
->GetParent();
544 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
546 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
549 return GetNextChild(item
, cookie
);
552 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
554 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
556 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
557 if ( (size_t)cookie
< children
.Count() )
559 return item
.m_pItem
->GetChildren().Item(cookie
++);
563 // there are no more of them
564 return wxTreeItemId();
568 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
570 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
572 wxGenericTreeItem
*i
= item
.m_pItem
;
573 wxGenericTreeItem
*parent
= i
->GetParent();
574 if ( parent
== NULL
)
576 // root item doesn't have any siblings
577 return wxTreeItemId();
580 wxArrayTreeItems
& siblings
= parent
->GetChildren();
581 int index
= siblings
.Index(i
);
582 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
584 size_t n
= (size_t)(index
+ 1);
585 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
588 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
590 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
592 wxGenericTreeItem
*i
= item
.m_pItem
;
593 wxGenericTreeItem
*parent
= i
->GetParent();
594 if ( parent
== NULL
)
596 // root item doesn't have any siblings
597 return wxTreeItemId();
600 wxArrayTreeItems
& siblings
= parent
->GetChildren();
601 int index
= siblings
.Index(i
);
602 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
604 return index
== 0 ? wxTreeItemId()
605 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
608 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
610 wxFAIL_MSG("not implemented");
612 return wxTreeItemId();
615 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
617 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
619 wxFAIL_MSG("not implemented");
621 return wxTreeItemId();
624 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
626 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
628 wxFAIL_MSG("not implemented");
630 return wxTreeItemId();
633 // -----------------------------------------------------------------------------
635 // -----------------------------------------------------------------------------
637 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
639 const wxString
& text
,
640 int image
, int selImage
,
641 wxTreeItemData
*data
)
643 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
646 // should we give a warning here?
647 return AddRoot(text
, image
, selImage
, data
);
651 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
658 data
->m_pItem
= item
;
661 parent
->Insert( item
, previous
);
668 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
669 int image
, int selImage
,
670 wxTreeItemData
*data
)
672 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), "tree can have only one root" );
675 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
676 image
, selImage
, data
);
679 data
->m_pItem
= m_anchor
;
682 AdjustMyScrollbars();
688 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
689 const wxString
& text
,
690 int image
, int selImage
,
691 wxTreeItemData
*data
)
693 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
696 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
697 const wxTreeItemId
& idPrevious
,
698 const wxString
& text
,
699 int image
, int selImage
,
700 wxTreeItemData
*data
)
702 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
705 // should we give a warning here?
706 return AddRoot(text
, image
, selImage
, data
);
709 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
710 wxASSERT_MSG( index
!= wxNOT_FOUND
,
711 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
712 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
715 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
716 const wxString
& text
,
717 int image
, int selImage
,
718 wxTreeItemData
*data
)
720 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
723 // should we give a warning here?
724 return AddRoot(text
, image
, selImage
, data
);
727 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
728 image
, selImage
, data
);
731 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
733 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
735 event
.SetEventObject( this );
736 ProcessEvent( event
);
739 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
741 wxGenericTreeItem
*item
= itemId
.m_pItem
;
742 item
->DeleteChildren(this);
747 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
749 wxGenericTreeItem
*item
= itemId
.m_pItem
;
750 wxGenericTreeItem
*parent
= item
->GetParent();
754 parent
->GetChildren().Remove(item
);
757 item
->DeleteChildren(this);
758 SendDeleteEvent(item
);
764 void wxTreeCtrl::DeleteAllItems()
768 m_anchor
->DeleteChildren(this);
777 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
779 wxGenericTreeItem
*item
= itemId
.m_pItem
;
781 if ( !item
->HasPlus() )
784 if ( item
->IsExpanded() )
787 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
789 event
.SetEventObject( this );
790 if ( ProcessEvent( event
) && event
.m_code
)
792 // cancelled by program
797 CalculatePositions();
799 RefreshSubtree(item
);
801 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
802 ProcessEvent( event
);
805 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
807 wxGenericTreeItem
*item
= itemId
.m_pItem
;
809 if ( !item
->IsExpanded() )
812 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
814 event
.SetEventObject( this );
815 if ( ProcessEvent( event
) && event
.m_code
)
817 // cancelled by program
823 wxArrayTreeItems
& children
= item
->GetChildren();
824 size_t count
= children
.Count();
825 for ( size_t n
= 0; n
< count
; n
++ )
827 Collapse(children
[n
]);
830 CalculatePositions();
832 RefreshSubtree(item
);
834 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
835 ProcessEvent( event
);
838 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
841 DeleteChildren(item
);
844 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
846 wxGenericTreeItem
*item
= itemId
.m_pItem
;
848 if ( item
->IsExpanded() )
854 void wxTreeCtrl::Unselect()
858 m_current
->SetHilight( FALSE
);
859 RefreshLine( m_current
);
863 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
865 wxGenericTreeItem
*item
= itemId
.m_pItem
;
867 if ( m_current
!= item
)
869 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
871 event
.m_itemOld
= m_current
;
872 event
.SetEventObject( this );
873 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
878 m_current
->SetHilight( FALSE
);
879 RefreshLine( m_current
);
883 m_current
->SetHilight( TRUE
);
884 RefreshLine( m_current
);
886 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
887 GetEventHandler()->ProcessEvent( event
);
891 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
893 wxGenericTreeItem
*gitem
= item
.m_pItem
;
895 int item_y
= gitem
->GetY();
899 ViewStart( &start_x
, &start_y
);
904 GetClientSize( &client_w
, &client_h
);
906 if (item_y
< start_y
+3)
910 m_anchor
->GetSize( x
, y
);
912 int x_pos
= GetScrollPos( wxHORIZONTAL
);
913 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
917 if (item_y
> start_y
+client_h
-16)
921 m_anchor
->GetSize( x
, y
);
923 int x_pos
= GetScrollPos( wxHORIZONTAL
);
924 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
929 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
931 wxFAIL_MSG("not implemented");
934 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
935 wxClassInfo
* WXUNUSED(textCtrlClass
) )
937 wxFAIL_MSG("not implemented");
939 return (wxTextCtrl
*)NULL
;
942 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
944 wxFAIL_MSG("not implemented");
946 return (wxTextCtrl
*)NULL
;
949 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
951 wxFAIL_MSG("not implemented");
954 // FIXME: tree sorting functions are not reentrant and not MT-safe!
955 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
957 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
958 wxGenericTreeItem
**item2
)
960 wxCHECK_MSG( s_treeBeingSorted
, 0, "bug in wxTreeCtrl::SortChildren()" );
962 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
965 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
966 const wxTreeItemId
& item2
)
968 return strcmp(GetItemText(item1
), GetItemText(item2
));
971 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
973 wxCHECK_RET( itemId
.IsOk(), "invalid tree item" );
975 wxGenericTreeItem
*item
= itemId
.m_pItem
;
977 wxCHECK_RET( !s_treeBeingSorted
,
978 "wxTreeCtrl::SortChildren is not reentrant" );
980 wxArrayTreeItems
& children
= item
->GetChildren();
981 if ( children
.Count() > 1 )
983 s_treeBeingSorted
= this;
984 children
.Sort(tree_ctrl_compare_func
);
985 s_treeBeingSorted
= NULL
;
989 //else: don't make the tree dirty as nothing changed
992 wxImageList
*wxTreeCtrl::GetImageList() const
994 return m_imageListNormal
;
997 wxImageList
*wxTreeCtrl::GetStateImageList() const
999 return m_imageListState
;
1002 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1004 m_imageListNormal
= imageList
;
1007 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1009 m_imageListState
= imageList
;
1012 // -----------------------------------------------------------------------------
1014 // -----------------------------------------------------------------------------
1016 void wxTreeCtrl::AdjustMyScrollbars()
1022 m_anchor
->GetSize( x
, y
);
1023 y
+= 2*m_lineHeight
;
1024 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1025 int y_pos
= GetScrollPos( wxVERTICAL
);
1026 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
1030 SetScrollbars( 0, 0, 0, 0 );
1034 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1036 /* render bold items in bold */
1042 fontOld
= dc
.GetFont();
1045 /* @@ is there any better way to make a bold variant of old font? */
1046 fontNew
= wxFont( fontOld
.GetPointSize(),
1047 fontOld
.GetFamily(),
1050 fontOld
.GetUnderlined());
1051 dc
.SetFont(fontNew
);
1055 wxFAIL_MSG("wxDC::GetFont() failed!");
1061 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1065 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1067 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1070 else if (item
->GetImage() != -1)
1072 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1076 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
1078 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1080 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1081 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1082 item
->GetX(), item
->GetY()-1,
1083 wxIMAGELIST_DRAW_TRANSPARENT
);
1084 dc
.DestroyClippingRegion();
1086 else if (item
->GetImage() != -1)
1088 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1089 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1090 item
->GetX(), item
->GetY()-1,
1091 wxIMAGELIST_DRAW_TRANSPARENT
);
1092 dc
.DestroyClippingRegion();
1095 dc
.SetBackgroundMode(wxTRANSPARENT
);
1096 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY() );
1098 /* restore normal font for bold items */
1101 dc
.SetFont( fontOld
);
1105 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1107 int horizX
= level
*m_indent
;
1109 item
->SetX( horizX
+33 );
1110 item
->SetY( y
-m_lineHeight
/3 );
1111 item
->SetHeight( m_lineHeight
);
1113 item
->SetCross( horizX
+15, y
);
1117 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1118 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1120 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
1122 int startX
= horizX
;
1123 int endX
= horizX
+ 10;
1125 if (!item
->HasChildren()) endX
+= 20;
1127 dc
.DrawLine( startX
, y
, endX
, y
);
1129 if (item
->HasPlus())
1131 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
1132 dc
.SetPen( *wxGREY_PEN
);
1133 dc
.SetBrush( *wxWHITE_BRUSH
);
1134 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
1135 dc
.SetPen( *wxBLACK_PEN
);
1136 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
1138 if (!item
->IsExpanded())
1140 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
1144 if (item
->HasHilight())
1146 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1148 dc
.SetBrush( *m_hilightBrush
);
1151 dc
.SetPen( *wxBLACK_PEN
);
1153 dc
.SetPen( *wxTRANSPARENT_PEN
);
1155 PaintItem(item
, dc
);
1157 dc
.SetPen( *wxBLACK_PEN
);
1158 dc
.SetTextForeground( *wxBLACK
);
1159 dc
.SetBrush( *wxWHITE_BRUSH
);
1163 dc
.SetBrush( *wxWHITE_BRUSH
);
1164 dc
.SetPen( *wxTRANSPARENT_PEN
);
1166 PaintItem(item
, dc
);
1168 dc
.SetPen( *wxBLACK_PEN
);
1172 if (item
->IsExpanded())
1176 wxArrayTreeItems
& children
= item
->GetChildren();
1177 size_t count
= children
.Count();
1178 for ( size_t n
= 0; n
< count
; n
++ )
1182 PaintLevel( children
[n
], dc
, level
+1, y
);
1185 /* it may happen that the item is expanded but has no items (when you
1186 * delete all its children for example) - don't draw the vertical line
1188 if (count
> 0) dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1192 // -----------------------------------------------------------------------------
1193 // wxWindows callbacks
1194 // -----------------------------------------------------------------------------
1196 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1204 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1206 dc
.SetPen( m_dottedPen
);
1207 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1209 int y
= m_lineHeight
/ 2 + 2;
1210 PaintLevel( m_anchor
, dc
, 0, y
);
1213 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1217 if (m_current
) RefreshLine( m_current
);
1220 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1224 if (m_current
) RefreshLine( m_current
);
1227 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1229 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1230 te
.m_code
= event
.KeyCode();
1231 te
.SetEventObject( this );
1232 GetEventHandler()->ProcessEvent( te
);
1240 switch (event
.KeyCode())
1244 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1252 if (IsExpanded(m_current
))
1254 Collapse(m_current
);
1266 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1267 event
.m_item
= m_current
;
1269 event
.SetEventObject( this );
1270 GetEventHandler()->ProcessEvent( event
);
1276 wxTreeItemId prev
= GetPrevSibling( m_current
);
1279 prev
= GetParent( m_current
);
1281 wxTreeItemId current
= m_current
;
1282 if (current
== GetFirstChild( prev
, cockie
))
1284 // otherwise we return to where we came from
1286 EnsureVisible( prev
);
1292 while (IsExpanded(prev
))
1294 int c
= (int)GetChildrenCount( prev
, FALSE
);
1296 prev
= GetFirstChild( prev
, cockie
);
1297 for (int i
= 0; i
< c
-1; i
++)
1298 prev
= GetNextSibling( prev
);
1301 EnsureVisible( prev
);
1307 wxTreeItemId prev
= GetPrevSibling( m_current
);
1311 EnsureVisible( prev
);
1315 prev
= GetParent( m_current
);
1318 EnsureVisible( prev
);
1326 // this works the same as the down arrow except that we also expand the
1327 // item if it wasn't expanded yet
1333 if (IsExpanded(m_current
))
1336 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1337 SelectItem( child
);
1338 EnsureVisible( child
);
1342 wxTreeItemId next
= GetNextSibling( m_current
);
1345 wxTreeItemId current
= m_current
;
1346 while (current
&& !next
)
1348 current
= GetParent( current
);
1349 if (current
) next
= GetNextSibling( current
);
1355 EnsureVisible( next
);
1366 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& WXUNUSED(flags
))
1368 bool onButton
= FALSE
;
1369 return m_anchor
->HitTest( point
, onButton
);
1372 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1374 if (!event
.LeftIsDown()) m_dragCount
= 0;
1376 if ( !(event
.LeftDown() || event
.LeftDClick() || event
.Dragging()) ) return;
1378 if ( !m_anchor
) return;
1380 wxClientDC
dc(this);
1382 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1383 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1385 bool onButton
= FALSE
;
1386 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1388 if (item
== NULL
) return; /* we hit the blank area */
1390 if (event
.Dragging())
1392 if (m_dragCount
== 2) /* small drag latency (3?) */
1396 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG
, GetId());
1397 nevent
.m_item
= m_current
;
1398 nevent
.SetEventObject(this);
1399 GetEventHandler()->ProcessEvent(nevent
);
1408 if (!IsSelected(item
)) SelectItem(item
); /* we dont support multiple selections, BTW */
1410 if (event
.LeftDClick())
1412 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1413 event
.m_item
= item
;
1415 event
.SetEventObject( this );
1416 GetEventHandler()->ProcessEvent( event
);
1425 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1427 /* after all changes have been done to the tree control,
1428 * we actually redraw the tree when everything is over */
1430 if (!m_dirty
) return;
1434 CalculatePositions();
1436 AdjustMyScrollbars();
1439 // -----------------------------------------------------------------------------
1441 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1443 int horizX
= level
*m_indent
;
1445 item
->SetX( horizX
+33 );
1446 item
->SetY( y
-m_lineHeight
/3-2 );
1447 item
->SetHeight( m_lineHeight
);
1449 if ( !item
->IsExpanded() )
1451 /* we dont need to calculate collapsed branches */
1455 wxArrayTreeItems
& children
= item
->GetChildren();
1456 size_t count
= children
.Count();
1457 for ( size_t n
= 0; n
< count
; n
++ )
1460 CalculateLevel( children
[n
], dc
, level
+1, y
); /* recurse */
1464 void wxTreeCtrl::CalculatePositions()
1466 if ( !m_anchor
) return;
1468 wxClientDC
dc(this);
1471 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1473 dc
.SetPen( m_dottedPen
);
1474 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1476 int y
= m_lineHeight
/ 2 + 2;
1477 CalculateLevel( m_anchor
, dc
, 0, y
); /* start recursion */
1480 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1482 wxClientDC
dc(this);
1487 GetClientSize( &cw
, &ch
);
1490 rect
.x
= dc
.LogicalToDeviceX( 0 );
1492 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1495 Refresh( TRUE
, &rect
);
1497 AdjustMyScrollbars();
1500 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1502 wxClientDC
dc(this);
1506 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1507 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1509 rect
.height
= dc
.GetCharHeight() + 6;
1511 Refresh( TRUE
, &rect
);