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 #include "wx/treectrl.h"
25 #include "wx/settings.h"
28 #include "wx/dynarray.h"
29 #include "wx/dcclient.h"
30 #include "wx/imaglist.h"
31 #include "wx/msgdlg.h"
33 // -----------------------------------------------------------------------------
35 // -----------------------------------------------------------------------------
37 WX_DEFINE_ARRAY(wxGenericTreeItem
*, wxArrayTreeItems
);
39 // -----------------------------------------------------------------------------
41 // -----------------------------------------------------------------------------
44 class WXDLLEXPORT wxGenericTreeItem
48 wxGenericTreeItem() { m_data
= NULL
; }
49 wxGenericTreeItem( wxGenericTreeItem
*parent
,
52 int image
, int selImage
,
53 wxTreeItemData
*data
);
58 wxArrayTreeItems
& GetChildren() { return m_children
; }
60 const wxString
& GetText() const { return m_text
; }
61 int GetImage() const { return m_image
; }
62 int GetSelectedImage() const { return m_selImage
; }
63 wxTreeItemData
*GetData() const { return m_data
; }
65 void SetText( const wxString
&text
, wxDC
& dc
);
66 void SetImage(int image
) { m_image
= image
; }
67 void SetSelectedImage(int image
) { m_selImage
= image
; }
68 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
70 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
72 void SetBold(bool bold
) { m_isBold
= bold
; }
74 int GetX() const { return m_x
; }
75 int GetY() const { return m_y
; }
77 void SetHeight(int h
) { m_height
= h
; }
79 void SetX(int x
) { m_x
= x
; }
80 void SetY(int y
) { m_y
= y
; }
82 wxGenericTreeItem
*GetParent() const { return m_parent
; }
85 // deletes all children notifying the treectrl about it if !NULL pointer
87 void DeleteChildren(wxTreeCtrl
*tree
= NULL
);
88 // FIXME don't know what is it for
91 // get count of all children (and grand children if 'recursively')
92 size_t GetChildrenCount(bool recursively
= TRUE
) const;
94 void Insert(wxGenericTreeItem
*child
, size_t index
)
95 { m_children
.Insert(child
, index
); }
97 void SetCross( int x
, int y
);
98 void GetSize( int &x
, int &y
);
100 // return the item at given position (or NULL if no item), onButton is TRUE
101 // if the point belongs to the item's button, otherwise it lies on the
103 wxGenericTreeItem
*HitTest( const wxPoint
& point
, bool &onButton
);
105 void Expand() { m_isCollapsed
= FALSE
; }
106 void Collapse() { m_isCollapsed
= TRUE
; }
108 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
111 bool HasChildren() const { return !m_children
.IsEmpty(); }
112 bool HasHilight() const { return m_hasHilight
; }
113 bool IsExpanded() const { return !m_isCollapsed
; }
114 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
115 bool IsBold() const { return m_isBold
; }
123 wxTreeItemData
*m_data
;
125 // use bitfields to save size
126 int m_isCollapsed
:1;
127 int m_hasHilight
:1; // same as focused
128 int m_hasPlus
:1; // used for item which doesn't have
129 // children but still has a [+] button
130 int m_isBold
:1; // render the label in bold font
133 long m_height
, m_width
;
134 int m_xCross
, m_yCross
;
136 wxArrayTreeItems m_children
;
137 wxGenericTreeItem
*m_parent
;
140 // =============================================================================
142 // =============================================================================
144 // -----------------------------------------------------------------------------
146 // -----------------------------------------------------------------------------
148 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
150 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
151 : wxNotifyEvent( commandType
, id
)
154 m_itemOld
= (wxGenericTreeItem
*)NULL
;
157 // -----------------------------------------------------------------------------
159 // -----------------------------------------------------------------------------
161 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
162 const wxString
& text
,
164 int image
, int selImage
,
165 wxTreeItemData
*data
)
169 m_selImage
= selImage
;
172 m_xCross
= m_yCross
= 0;
176 m_isCollapsed
= TRUE
;
177 m_hasHilight
= FALSE
;
183 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
186 wxGenericTreeItem::~wxGenericTreeItem()
190 wxASSERT_MSG( m_children
.IsEmpty(),
191 "please call DeleteChildren() before deleting the item" );
194 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
196 size_t count
= m_children
.Count();
197 for ( size_t n
= 0; n
< count
; n
++ )
199 wxGenericTreeItem
*child
= m_children
[n
];
202 tree
->SendDeleteEvent(child
);
205 child
->DeleteChildren(tree
);
212 void wxGenericTreeItem::SetText( const wxString
&text
, wxDC
& dc
)
216 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
219 void wxGenericTreeItem::Reset()
226 m_height
= m_width
= 0;
233 m_isCollapsed
= TRUE
;
235 m_parent
= (wxGenericTreeItem
*)NULL
;
238 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
240 size_t count
= m_children
.Count();
244 size_t total
= count
;
245 for ( size_t n
= 0; n
< count
; n
++ )
247 total
+= m_children
[n
]->GetChildrenCount();
253 void wxGenericTreeItem::SetCross( int x
, int y
)
259 void wxGenericTreeItem::GetSize( int &x
, int &y
)
261 if ( y
< m_y
) y
= m_y
;
262 int width
= m_x
+ m_width
;
263 if (width
> x
) x
= width
;
267 size_t count
= m_children
.Count();
268 for ( size_t n
= 0; n
< count
; n
++ )
270 m_children
[n
]->GetSize( x
, y
);
275 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
278 if ((point
.y
> m_y
) && (point
.y
< m_y
+ m_height
))
281 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
282 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
283 (IsExpanded() || HasPlus()))
289 if ((point
.x
> m_x
) && (point
.x
< m_x
+m_width
))
299 size_t count
= m_children
.Count();
300 for ( size_t n
= 0; n
< count
; n
++ )
302 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, onButton
);
312 // -----------------------------------------------------------------------------
313 // wxTreeCtrl implementation
314 // -----------------------------------------------------------------------------
316 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
318 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
319 EVT_PAINT (wxTreeCtrl::OnPaint
)
320 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
321 EVT_CHAR (wxTreeCtrl::OnChar
)
322 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
323 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
324 EVT_IDLE (wxTreeCtrl::OnIdle
)
327 // -----------------------------------------------------------------------------
328 // construction/destruction
329 // -----------------------------------------------------------------------------
330 void wxTreeCtrl::Init()
333 m_anchor
= (wxGenericTreeItem
*) NULL
;
342 m_hilightBrush
= new wxBrush
344 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
349 m_imageListState
= (wxImageList
*) NULL
;
352 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
353 const wxPoint
& pos
, const wxSize
& size
,
355 const wxValidator
&validator
,
356 const wxString
& name
)
360 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
362 SetValidator( validator
);
364 SetBackgroundColour( *wxWHITE
);
365 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
370 wxTreeCtrl::~wxTreeCtrl()
372 wxDELETE( m_hilightBrush
);
377 // -----------------------------------------------------------------------------
379 // -----------------------------------------------------------------------------
381 size_t wxTreeCtrl::GetCount() const
383 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
386 void wxTreeCtrl::SetIndent(unsigned int indent
)
392 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
394 wxCHECK_MSG( item
.IsOk(), 0u, "invalid tree item" );
396 return item
.m_pItem
->GetChildrenCount(recursively
);
399 // -----------------------------------------------------------------------------
400 // functions to work with tree items
401 // -----------------------------------------------------------------------------
403 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
405 wxCHECK_MSG( item
.IsOk(), "", "invalid tree item" );
407 return item
.m_pItem
->GetText();
410 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
412 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
414 return item
.m_pItem
->GetImage();
417 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
419 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
421 return item
.m_pItem
->GetSelectedImage();
424 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
426 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
428 return item
.m_pItem
->GetData();
431 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
433 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
436 item
.m_pItem
->SetText(text
, dc
);
439 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
441 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
443 item
.m_pItem
->SetImage(image
);
446 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
448 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
450 item
.m_pItem
->SetSelectedImage(image
);
453 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
455 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
457 item
.m_pItem
->SetData(data
);
460 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
462 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
464 item
.m_pItem
->SetHasPlus(has
);
467 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
469 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
471 // avoid redrawing the tree if no real change
472 wxGenericTreeItem
*pItem
= item
.m_pItem
;
473 if ( pItem
->IsBold() != bold
)
475 pItem
->SetBold(bold
);
480 // -----------------------------------------------------------------------------
481 // item status inquiries
482 // -----------------------------------------------------------------------------
484 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
486 wxFAIL_MSG("not implemented");
491 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
493 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
495 return !item
.m_pItem
->GetChildren().IsEmpty();
498 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
500 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
502 return item
.m_pItem
->IsExpanded();
505 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
507 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
509 return item
.m_pItem
->HasHilight();
512 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
514 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
516 return item
.m_pItem
->IsBold();
519 // -----------------------------------------------------------------------------
521 // -----------------------------------------------------------------------------
523 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
525 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
527 return item
.m_pItem
->GetParent();
530 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
532 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
535 return GetNextChild(item
, cookie
);
538 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
540 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
542 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
543 if ( (size_t)cookie
< children
.Count() )
545 return item
.m_pItem
->GetChildren().Item(cookie
++);
549 // there are no more of them
554 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
556 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
558 wxGenericTreeItem
*i
= item
.m_pItem
;
559 wxGenericTreeItem
*parent
= i
->GetParent();
560 if ( parent
== NULL
)
562 // root item doesn't have any siblings
566 wxArrayTreeItems
& siblings
= parent
->GetChildren();
567 int index
= siblings
.Index(i
);
568 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
570 size_t n
= (size_t)(index
+ 1);
571 return n
== siblings
.Count() ? (wxGenericTreeItem
*)NULL
: siblings
[n
];
574 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
576 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
578 wxGenericTreeItem
*i
= item
.m_pItem
;
579 wxGenericTreeItem
*parent
= i
->GetParent();
580 if ( parent
== NULL
)
582 // root item doesn't have any siblings
586 wxArrayTreeItems
& siblings
= parent
->GetChildren();
587 int index
= siblings
.Index(i
);
588 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
590 return index
== 0 ? (wxGenericTreeItem
*)NULL
: siblings
[(size_t)(index
- 1)];
593 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
595 wxFAIL_MSG("not implemented");
600 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
602 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
604 wxFAIL_MSG("not implemented");
609 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
611 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
613 wxFAIL_MSG("not implemented");
618 // -----------------------------------------------------------------------------
620 // -----------------------------------------------------------------------------
622 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
624 const wxString
& text
,
625 int image
, int selImage
,
626 wxTreeItemData
*data
)
628 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
631 // should we give a warning here?
632 return AddRoot(text
, image
, selImage
, data
);
636 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
643 data
->m_pItem
= item
;
646 parent
->Insert( item
, previous
);
653 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
654 int image
, int selImage
,
655 wxTreeItemData
*data
)
657 wxCHECK_MSG( !m_anchor
, NULL
, "tree can have only one root" );
660 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
661 image
, selImage
, data
);
664 data
->m_pItem
= m_anchor
;
667 AdjustMyScrollbars();
673 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
674 const wxString
& text
,
675 int image
, int selImage
,
676 wxTreeItemData
*data
)
678 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
681 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
682 const wxTreeItemId
& idPrevious
,
683 const wxString
& text
,
684 int image
, int selImage
,
685 wxTreeItemData
*data
)
687 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
690 // should we give a warning here?
691 return AddRoot(text
, image
, selImage
, data
);
694 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
695 wxASSERT_MSG( index
!= NOT_FOUND
,
696 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
697 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
700 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
701 const wxString
& text
,
702 int image
, int selImage
,
703 wxTreeItemData
*data
)
705 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
708 // should we give a warning here?
709 return AddRoot(text
, image
, selImage
, data
);
712 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
713 image
, selImage
, data
);
716 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
718 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
720 event
.SetEventObject( this );
721 ProcessEvent( event
);
724 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
726 wxGenericTreeItem
*item
= itemId
.m_pItem
;
727 item
->DeleteChildren(this);
732 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
734 wxGenericTreeItem
*item
= itemId
.m_pItem
;
735 wxGenericTreeItem
*parent
= item
->GetParent();
739 parent
->GetChildren().Remove(item
);
742 item
->DeleteChildren(this);
743 SendDeleteEvent(item
);
749 void wxTreeCtrl::DeleteAllItems()
753 m_anchor
->DeleteChildren(this);
762 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
764 wxGenericTreeItem
*item
= itemId
.m_pItem
;
766 if ( !item
->HasPlus() )
769 if ( item
->IsExpanded() )
772 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
774 event
.SetEventObject( this );
775 if ( ProcessEvent( event
) && event
.m_code
)
777 // cancelled by program
783 RefreshSubtree(item
);
785 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
786 ProcessEvent( event
);
789 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
791 wxGenericTreeItem
*item
= itemId
.m_pItem
;
793 if ( !item
->IsExpanded() )
796 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
798 event
.SetEventObject( this );
799 if ( ProcessEvent( event
) && event
.m_code
)
801 // cancelled by program
807 wxArrayTreeItems
& children
= item
->GetChildren();
808 size_t count
= children
.Count();
809 for ( size_t n
= 0; n
< count
; n
++ )
811 Collapse(children
[n
]);
814 CalculatePositions();
816 RefreshSubtree(item
);
818 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
819 ProcessEvent( event
);
822 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
825 DeleteChildren(item
);
828 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
830 wxGenericTreeItem
*item
= itemId
.m_pItem
;
832 if ( item
->IsExpanded() )
838 void wxTreeCtrl::Unselect()
842 m_current
->SetHilight( FALSE
);
843 RefreshLine( m_current
);
847 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
849 wxGenericTreeItem
*item
= itemId
.m_pItem
;
851 if ( m_current
!= item
)
853 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
855 event
.m_itemOld
= m_current
;
856 event
.SetEventObject( this );
857 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
862 m_current
->SetHilight( FALSE
);
863 RefreshLine( m_current
);
867 m_current
->SetHilight( TRUE
);
868 RefreshLine( m_current
);
870 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
871 GetEventHandler()->ProcessEvent( event
);
875 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
877 wxGenericTreeItem
*gitem
= item
.m_pItem
;
879 int item_y
= gitem
->GetY();
883 ViewStart( &start_x
, &start_y
);
886 if (item_y
< start_y
+3)
890 m_anchor
->GetSize( x
, y
);
892 int x_pos
= GetScrollPos( wxHORIZONTAL
);
893 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, item_y
/10 );
899 GetClientSize( &w
, &h
);
901 if (item_y
> start_y
+h
-26)
905 m_anchor
->GetSize( x
, y
);
907 int x_pos
= GetScrollPos( wxHORIZONTAL
);
908 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-h
+30)/10 );
913 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
915 wxFAIL_MSG("not implemented");
918 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
919 wxClassInfo
* WXUNUSED(textCtrlClass
) )
921 wxFAIL_MSG("not implemented");
923 return (wxTextCtrl
*)NULL
;
926 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
928 wxFAIL_MSG("not implemented");
930 return (wxTextCtrl
*)NULL
;
933 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
935 wxFAIL_MSG("not implemented");
938 // FIXME: tree sorting functions are not reentrant and not MT-safe!
939 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
941 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
942 wxGenericTreeItem
**item2
)
944 wxCHECK_MSG( s_treeBeingSorted
, 0, "bug in wxTreeCtrl::SortChildren()" );
946 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
949 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
950 const wxTreeItemId
& item2
)
952 return strcmp(GetItemText(item1
), GetItemText(item2
));
955 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
957 wxCHECK_RET( itemId
.IsOk(), "invalid tree item" );
959 wxGenericTreeItem
*item
= itemId
.m_pItem
;
961 wxCHECK_RET( !s_treeBeingSorted
,
962 "wxTreeCtrl::SortChildren is not reentrant" );
964 wxArrayTreeItems
& children
= item
->GetChildren();
965 if ( children
.Count() > 1 )
967 s_treeBeingSorted
= this;
968 children
.Sort(tree_ctrl_compare_func
);
969 s_treeBeingSorted
= NULL
;
973 //else: don't make the tree dirty as nothing changed
976 wxImageList
*wxTreeCtrl::GetImageList() const
978 return m_imageListNormal
;
981 wxImageList
*wxTreeCtrl::GetStateImageList() const
983 return m_imageListState
;
986 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
988 m_imageListNormal
= imageList
;
991 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
993 m_imageListState
= imageList
;
996 // -----------------------------------------------------------------------------
998 // -----------------------------------------------------------------------------
1000 void wxTreeCtrl::AdjustMyScrollbars()
1006 m_anchor
->GetSize( x
, y
);
1007 y
+= 2*m_lineHeight
;
1008 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1009 int y_pos
= GetScrollPos( wxVERTICAL
);
1010 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
1014 SetScrollbars( 0, 0, 0, 0 );
1018 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1020 // render bold items in bold
1024 if ( item
->IsBold() )
1026 fontOld
= dc
.GetFont();
1029 // @@ is there any better way to make a bold variant of old font?
1030 fontNew
= wxFont( fontOld
.GetPointSize(),
1031 fontOld
.GetFamily(),
1034 fontOld
.GetUnderlined());
1035 dc
.SetFont(fontNew
);
1039 wxFAIL_MSG("wxDC::GetFont() failed!");
1045 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1049 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1051 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1054 else if (item
->GetImage() != -1)
1056 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1060 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
1062 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1064 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1065 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1066 item
->GetX(), item
->GetY()-1,
1067 wxIMAGELIST_DRAW_TRANSPARENT
);
1068 dc
.DestroyClippingRegion();
1070 else if (item
->GetImage() != -1)
1072 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1073 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1074 item
->GetX(), item
->GetY()-1,
1075 wxIMAGELIST_DRAW_TRANSPARENT
);
1076 dc
.DestroyClippingRegion();
1079 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY() );
1081 // restore normal font for bold items
1084 dc
.SetFont( fontOld
);
1088 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1090 int horizX
= level
*m_indent
;
1092 item
->SetX( horizX
+33 );
1093 item
->SetY( y
-m_lineHeight
/3 );
1094 item
->SetHeight( m_lineHeight
);
1096 item
->SetCross( horizX
+15, y
);
1100 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1101 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1103 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
1105 int startX
= horizX
;
1106 int endX
= horizX
+ 10;
1108 if (!item
->HasChildren()) endX
+= 20;
1110 dc
.DrawLine( startX
, y
, endX
, y
);
1112 if (item
->HasPlus())
1114 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
1115 dc
.SetPen( *wxGREY_PEN
);
1116 dc
.SetBrush( *wxWHITE_BRUSH
);
1117 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
1118 dc
.SetPen( *wxBLACK_PEN
);
1119 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
1121 if (!item
->IsExpanded())
1122 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
1125 if (item
->HasHilight())
1127 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1129 dc
.SetBrush( *m_hilightBrush
);
1132 dc
.SetPen( *wxBLACK_PEN
);
1134 dc
.SetPen( *wxTRANSPARENT_PEN
);
1136 PaintItem(item
, dc
);
1138 dc
.SetPen( *wxBLACK_PEN
);
1139 dc
.SetTextForeground( *wxBLACK
);
1140 dc
.SetBrush( *wxWHITE_BRUSH
);
1144 dc
.SetBrush( *wxWHITE_BRUSH
);
1145 dc
.SetPen( *wxTRANSPARENT_PEN
);
1147 PaintItem(item
, dc
);
1149 dc
.SetPen( *wxBLACK_PEN
);
1153 if ( item
->IsExpanded() )
1157 wxArrayTreeItems
& children
= item
->GetChildren();
1158 size_t count
= children
.Count();
1159 for ( size_t n
= 0; n
< count
; n
++ )
1164 PaintLevel( children
[n
], dc
, level
+1, y
);
1167 // it may happen that the item is expanded but has no items (when you
1168 // delete all its children for example) - don't draw the vertical line
1171 dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1175 // -----------------------------------------------------------------------------
1176 // wxWindows callbacks
1177 // -----------------------------------------------------------------------------
1179 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1187 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1189 dc
.SetPen( m_dottedPen
);
1190 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1192 int y
= m_lineHeight
/ 2 + 2;
1193 PaintLevel( m_anchor
, dc
, 0, y
);
1196 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1200 RefreshLine( m_current
);
1203 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1207 RefreshLine( m_current
);
1210 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1212 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1213 te
.m_code
= event
.KeyCode();
1214 te
.SetEventObject( this );
1215 GetEventHandler()->ProcessEvent( te
);
1223 switch (event
.KeyCode())
1227 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1235 if (IsExpanded(m_current
))
1237 Collapse(m_current
);
1249 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1250 event
.m_item
= m_current
;
1252 event
.SetEventObject( this );
1253 GetEventHandler()->ProcessEvent( event
);
1260 wxTreeItemId prev
= GetPrevSibling( m_current
);
1264 EnsureVisible( prev
);
1268 prev
= GetParent( m_current
);
1271 EnsureVisible( prev
);
1279 // this works the same as the down arrow except that we also expand the
1280 // item if it wasn't expanded yet
1286 if (IsExpanded(m_current
))
1289 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1290 SelectItem( child
);
1291 EnsureVisible( child
);
1295 wxTreeItemId next
= GetNextSibling( m_current
);
1298 wxTreeItemId current
= m_current
;
1299 while (current
&& !next
)
1301 current
= GetParent( current
);
1302 if (current
) next
= GetNextSibling( current
);
1308 EnsureVisible( next
);
1319 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& WXUNUSED(flags
))
1321 bool onButton
= FALSE
;
1322 return m_anchor
->HitTest( point
, onButton
);
1325 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1327 if ( !(event
.LeftDown() || event
.LeftDClick()) )
1333 wxClientDC
dc(this);
1335 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1336 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1338 bool onButton
= FALSE
;
1339 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1343 if (!IsSelected(item
)) SelectItem(item
);
1345 if ( event
.LeftDClick() )
1347 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1348 event
.m_item
= item
;
1350 event
.SetEventObject( this );
1351 GetEventHandler()->ProcessEvent( event
);
1360 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1362 if (!m_dirty
) return;
1366 CalculatePositions();
1368 AdjustMyScrollbars();
1371 // -----------------------------------------------------------------------------
1372 // -----------------------------------------------------------------------------
1373 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
,
1378 int horizX
= level
*m_indent
;
1380 item
->SetX( horizX
+33 );
1381 item
->SetY( y
-m_lineHeight
/3-2 );
1382 item
->SetHeight( m_lineHeight
);
1384 if ( item
->IsExpanded() )
1387 wxArrayTreeItems
& children
= item
->GetChildren();
1388 size_t count
= children
.Count();
1389 for ( size_t n
= 0; n
< count
; n
++ )
1392 CalculateLevel( children
[n
], dc
, level
+1, y
);
1396 void wxTreeCtrl::CalculatePositions()
1401 wxClientDC
dc(this);
1404 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1406 dc
.SetPen( m_dottedPen
);
1407 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1409 int y
= m_lineHeight
/ 2 + 2;
1410 CalculateLevel( m_anchor
, dc
, 0, y
);
1413 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1415 wxClientDC
dc(this);
1420 GetClientSize( &cw
, &ch
);
1423 rect
.x
= dc
.LogicalToDeviceX( 0 );
1425 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1428 Refresh( TRUE
, &rect
);
1430 AdjustMyScrollbars();
1433 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1435 wxClientDC
dc(this);
1439 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1440 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1442 rect
.height
= dc
.GetCharHeight() + 6;
1443 Refresh( TRUE
, &rect
);