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/settings.h"
35 #include "wx/dynarray.h"
36 #include "wx/dcclient.h"
37 #include "wx/imaglist.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() : 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() : siblings
[(size_t)(index
- 1)];
605 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
607 wxFAIL_MSG("not implemented");
609 return wxTreeItemId();
612 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
614 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
616 wxFAIL_MSG("not implemented");
618 return wxTreeItemId();
621 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
623 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
625 wxFAIL_MSG("not implemented");
627 return wxTreeItemId();
630 // -----------------------------------------------------------------------------
632 // -----------------------------------------------------------------------------
634 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
636 const wxString
& text
,
637 int image
, int selImage
,
638 wxTreeItemData
*data
)
640 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
643 // should we give a warning here?
644 return AddRoot(text
, image
, selImage
, data
);
648 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
655 data
->m_pItem
= item
;
658 parent
->Insert( item
, previous
);
665 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
666 int image
, int selImage
,
667 wxTreeItemData
*data
)
669 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), "tree can have only one root" );
672 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
673 image
, selImage
, data
);
676 data
->m_pItem
= m_anchor
;
679 AdjustMyScrollbars();
685 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
686 const wxString
& text
,
687 int image
, int selImage
,
688 wxTreeItemData
*data
)
690 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
693 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
694 const wxTreeItemId
& idPrevious
,
695 const wxString
& text
,
696 int image
, int selImage
,
697 wxTreeItemData
*data
)
699 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
702 // should we give a warning here?
703 return AddRoot(text
, image
, selImage
, data
);
706 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
707 wxASSERT_MSG( index
!= NOT_FOUND
,
708 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
709 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
712 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
713 const wxString
& text
,
714 int image
, int selImage
,
715 wxTreeItemData
*data
)
717 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
720 // should we give a warning here?
721 return AddRoot(text
, image
, selImage
, data
);
724 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
725 image
, selImage
, data
);
728 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
730 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
732 event
.SetEventObject( this );
733 ProcessEvent( event
);
736 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
738 wxGenericTreeItem
*item
= itemId
.m_pItem
;
739 item
->DeleteChildren(this);
744 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
746 wxGenericTreeItem
*item
= itemId
.m_pItem
;
747 wxGenericTreeItem
*parent
= item
->GetParent();
751 parent
->GetChildren().Remove(item
);
754 item
->DeleteChildren(this);
755 SendDeleteEvent(item
);
761 void wxTreeCtrl::DeleteAllItems()
765 m_anchor
->DeleteChildren(this);
774 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
776 wxGenericTreeItem
*item
= itemId
.m_pItem
;
778 if ( !item
->HasPlus() )
781 if ( item
->IsExpanded() )
784 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
786 event
.SetEventObject( this );
787 if ( ProcessEvent( event
) && event
.m_code
)
789 // cancelled by program
794 CalculatePositions();
796 RefreshSubtree(item
);
798 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
799 ProcessEvent( event
);
802 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
804 wxGenericTreeItem
*item
= itemId
.m_pItem
;
806 if ( !item
->IsExpanded() )
809 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
811 event
.SetEventObject( this );
812 if ( ProcessEvent( event
) && event
.m_code
)
814 // cancelled by program
820 wxArrayTreeItems
& children
= item
->GetChildren();
821 size_t count
= children
.Count();
822 for ( size_t n
= 0; n
< count
; n
++ )
824 Collapse(children
[n
]);
827 CalculatePositions();
829 RefreshSubtree(item
);
831 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
832 ProcessEvent( event
);
835 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
838 DeleteChildren(item
);
841 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
843 wxGenericTreeItem
*item
= itemId
.m_pItem
;
845 if ( item
->IsExpanded() )
851 void wxTreeCtrl::Unselect()
855 m_current
->SetHilight( FALSE
);
856 RefreshLine( m_current
);
860 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
862 wxGenericTreeItem
*item
= itemId
.m_pItem
;
864 if ( m_current
!= item
)
866 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
868 event
.m_itemOld
= m_current
;
869 event
.SetEventObject( this );
870 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
875 m_current
->SetHilight( FALSE
);
876 RefreshLine( m_current
);
880 m_current
->SetHilight( TRUE
);
881 RefreshLine( m_current
);
883 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
884 GetEventHandler()->ProcessEvent( event
);
888 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
890 wxGenericTreeItem
*gitem
= item
.m_pItem
;
892 int item_y
= gitem
->GetY();
896 ViewStart( &start_x
, &start_y
);
901 GetClientSize( &client_w
, &client_h
);
903 if (item_y
< start_y
+3)
907 m_anchor
->GetSize( x
, y
);
909 int x_pos
= GetScrollPos( wxHORIZONTAL
);
910 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
914 if (item_y
> start_y
+client_h
-16)
918 m_anchor
->GetSize( x
, y
);
920 int x_pos
= GetScrollPos( wxHORIZONTAL
);
921 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
926 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
928 wxFAIL_MSG("not implemented");
931 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
932 wxClassInfo
* WXUNUSED(textCtrlClass
) )
934 wxFAIL_MSG("not implemented");
936 return (wxTextCtrl
*)NULL
;
939 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
941 wxFAIL_MSG("not implemented");
943 return (wxTextCtrl
*)NULL
;
946 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
948 wxFAIL_MSG("not implemented");
951 // FIXME: tree sorting functions are not reentrant and not MT-safe!
952 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
954 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
955 wxGenericTreeItem
**item2
)
957 wxCHECK_MSG( s_treeBeingSorted
, 0, "bug in wxTreeCtrl::SortChildren()" );
959 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
962 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
963 const wxTreeItemId
& item2
)
965 return strcmp(GetItemText(item1
), GetItemText(item2
));
968 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
970 wxCHECK_RET( itemId
.IsOk(), "invalid tree item" );
972 wxGenericTreeItem
*item
= itemId
.m_pItem
;
974 wxCHECK_RET( !s_treeBeingSorted
,
975 "wxTreeCtrl::SortChildren is not reentrant" );
977 wxArrayTreeItems
& children
= item
->GetChildren();
978 if ( children
.Count() > 1 )
980 s_treeBeingSorted
= this;
981 children
.Sort(tree_ctrl_compare_func
);
982 s_treeBeingSorted
= NULL
;
986 //else: don't make the tree dirty as nothing changed
989 wxImageList
*wxTreeCtrl::GetImageList() const
991 return m_imageListNormal
;
994 wxImageList
*wxTreeCtrl::GetStateImageList() const
996 return m_imageListState
;
999 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1001 m_imageListNormal
= imageList
;
1004 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1006 m_imageListState
= imageList
;
1009 // -----------------------------------------------------------------------------
1011 // -----------------------------------------------------------------------------
1013 void wxTreeCtrl::AdjustMyScrollbars()
1019 m_anchor
->GetSize( x
, y
);
1020 y
+= 2*m_lineHeight
;
1021 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1022 int y_pos
= GetScrollPos( wxVERTICAL
);
1023 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
1027 SetScrollbars( 0, 0, 0, 0 );
1031 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1033 // render bold items in bold
1037 if ( item
->IsBold() )
1039 fontOld
= dc
.GetFont();
1042 // @@ is there any better way to make a bold variant of old font?
1043 fontNew
= wxFont( fontOld
.GetPointSize(),
1044 fontOld
.GetFamily(),
1047 fontOld
.GetUnderlined());
1048 dc
.SetFont(fontNew
);
1052 wxFAIL_MSG("wxDC::GetFont() failed!");
1058 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1062 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1064 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1067 else if (item
->GetImage() != -1)
1069 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1073 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
1075 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1077 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1078 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1079 item
->GetX(), item
->GetY()-1,
1080 wxIMAGELIST_DRAW_TRANSPARENT
);
1081 dc
.DestroyClippingRegion();
1083 else if (item
->GetImage() != -1)
1085 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1086 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1087 item
->GetX(), item
->GetY()-1,
1088 wxIMAGELIST_DRAW_TRANSPARENT
);
1089 dc
.DestroyClippingRegion();
1092 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY() );
1094 // restore normal font for bold items
1097 dc
.SetFont( fontOld
);
1101 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1103 int horizX
= level
*m_indent
;
1105 item
->SetX( horizX
+33 );
1106 item
->SetY( y
-m_lineHeight
/3 );
1107 item
->SetHeight( m_lineHeight
);
1109 item
->SetCross( horizX
+15, y
);
1113 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1114 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1116 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
1118 int startX
= horizX
;
1119 int endX
= horizX
+ 10;
1121 if (!item
->HasChildren()) endX
+= 20;
1123 dc
.DrawLine( startX
, y
, endX
, y
);
1125 if (item
->HasPlus())
1127 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
1128 dc
.SetPen( *wxGREY_PEN
);
1129 dc
.SetBrush( *wxWHITE_BRUSH
);
1130 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
1131 dc
.SetPen( *wxBLACK_PEN
);
1132 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
1134 if (!item
->IsExpanded())
1135 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
1138 if (item
->HasHilight())
1140 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1142 dc
.SetBrush( *m_hilightBrush
);
1145 dc
.SetPen( *wxBLACK_PEN
);
1147 dc
.SetPen( *wxTRANSPARENT_PEN
);
1149 PaintItem(item
, dc
);
1151 dc
.SetPen( *wxBLACK_PEN
);
1152 dc
.SetTextForeground( *wxBLACK
);
1153 dc
.SetBrush( *wxWHITE_BRUSH
);
1157 dc
.SetBrush( *wxWHITE_BRUSH
);
1158 dc
.SetPen( *wxTRANSPARENT_PEN
);
1160 PaintItem(item
, dc
);
1162 dc
.SetPen( *wxBLACK_PEN
);
1166 if ( item
->IsExpanded() )
1170 wxArrayTreeItems
& children
= item
->GetChildren();
1171 size_t count
= children
.Count();
1172 for ( size_t n
= 0; n
< count
; n
++ )
1177 PaintLevel( children
[n
], dc
, level
+1, y
);
1180 // it may happen that the item is expanded but has no items (when you
1181 // delete all its children for example) - don't draw the vertical line
1184 dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1188 // -----------------------------------------------------------------------------
1189 // wxWindows callbacks
1190 // -----------------------------------------------------------------------------
1192 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1200 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1202 dc
.SetPen( m_dottedPen
);
1203 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1205 int y
= m_lineHeight
/ 2 + 2;
1206 PaintLevel( m_anchor
, dc
, 0, y
);
1209 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1213 RefreshLine( m_current
);
1216 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1220 RefreshLine( m_current
);
1223 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1225 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1226 te
.m_code
= event
.KeyCode();
1227 te
.SetEventObject( this );
1228 GetEventHandler()->ProcessEvent( te
);
1236 switch (event
.KeyCode())
1240 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1248 if (IsExpanded(m_current
))
1250 Collapse(m_current
);
1262 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1263 event
.m_item
= m_current
;
1265 event
.SetEventObject( this );
1266 GetEventHandler()->ProcessEvent( event
);
1272 wxTreeItemId prev
= GetPrevSibling( m_current
);
1275 prev
= GetParent( m_current
);
1277 wxTreeItemId current
= m_current
;
1278 if (current
== GetFirstChild( prev
, cockie
))
1280 // otherwise we return to where we came from
1282 EnsureVisible( prev
);
1288 while (IsExpanded(prev
))
1290 int c
= (int)GetChildrenCount( prev
, FALSE
);
1292 prev
= GetFirstChild( prev
, cockie
);
1293 for (int i
= 0; i
< c
-1; i
++)
1294 prev
= GetNextSibling( prev
);
1297 EnsureVisible( prev
);
1303 wxTreeItemId prev
= GetPrevSibling( m_current
);
1307 EnsureVisible( prev
);
1311 prev
= GetParent( m_current
);
1314 EnsureVisible( prev
);
1322 // this works the same as the down arrow except that we also expand the
1323 // item if it wasn't expanded yet
1329 if (IsExpanded(m_current
))
1332 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1333 SelectItem( child
);
1334 EnsureVisible( child
);
1338 wxTreeItemId next
= GetNextSibling( m_current
);
1341 wxTreeItemId current
= m_current
;
1342 while (current
&& !next
)
1344 current
= GetParent( current
);
1345 if (current
) next
= GetNextSibling( current
);
1351 EnsureVisible( next
);
1362 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& WXUNUSED(flags
))
1364 bool onButton
= FALSE
;
1365 return m_anchor
->HitTest( point
, onButton
);
1368 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1370 if ( !(event
.LeftDown() || event
.LeftDClick()) )
1376 wxClientDC
dc(this);
1378 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1379 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1381 bool onButton
= FALSE
;
1382 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1386 if (!IsSelected(item
)) SelectItem(item
);
1388 if ( event
.LeftDClick() )
1390 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1391 event
.m_item
= item
;
1393 event
.SetEventObject( this );
1394 GetEventHandler()->ProcessEvent( event
);
1403 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1405 if (!m_dirty
) return;
1409 CalculatePositions();
1411 AdjustMyScrollbars();
1414 // -----------------------------------------------------------------------------
1415 // -----------------------------------------------------------------------------
1416 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
,
1421 int horizX
= level
*m_indent
;
1423 item
->SetX( horizX
+33 );
1424 item
->SetY( y
-m_lineHeight
/3-2 );
1425 item
->SetHeight( m_lineHeight
);
1427 // if ( item->IsExpanded() )
1429 if ( !item
->IsExpanded() ) // Surely this is correct? JACS
1432 wxArrayTreeItems
& children
= item
->GetChildren();
1433 size_t count
= children
.Count();
1434 for ( size_t n
= 0; n
< count
; n
++ )
1437 CalculateLevel( children
[n
], dc
, level
+1, y
);
1441 void wxTreeCtrl::CalculatePositions()
1446 wxClientDC
dc(this);
1449 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1451 dc
.SetPen( m_dottedPen
);
1452 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1454 int y
= m_lineHeight
/ 2 + 2;
1455 CalculateLevel( m_anchor
, dc
, 0, y
);
1458 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1460 wxClientDC
dc(this);
1465 GetClientSize( &cw
, &ch
);
1468 rect
.x
= dc
.LogicalToDeviceX( 0 );
1470 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1473 Refresh( TRUE
, &rect
);
1475 AdjustMyScrollbars();
1478 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1480 wxClientDC
dc(this);
1484 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1485 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1487 rect
.height
= dc
.GetCharHeight() + 6;
1488 Refresh( TRUE
, &rect
);