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 children
.Item(cookie
++);
563 // there are no more of them
564 return wxTreeItemId();
568 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
570 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
572 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
573 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
576 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
578 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
580 wxGenericTreeItem
*i
= item
.m_pItem
;
581 wxGenericTreeItem
*parent
= i
->GetParent();
582 if ( parent
== NULL
)
584 // root item doesn't have any siblings
585 return wxTreeItemId();
588 wxArrayTreeItems
& siblings
= parent
->GetChildren();
589 int index
= siblings
.Index(i
);
590 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
592 size_t n
= (size_t)(index
+ 1);
593 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
596 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
598 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
600 wxGenericTreeItem
*i
= item
.m_pItem
;
601 wxGenericTreeItem
*parent
= i
->GetParent();
602 if ( parent
== NULL
)
604 // root item doesn't have any siblings
605 return wxTreeItemId();
608 wxArrayTreeItems
& siblings
= parent
->GetChildren();
609 int index
= siblings
.Index(i
);
610 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
612 return index
== 0 ? wxTreeItemId()
613 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
616 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
618 wxFAIL_MSG("not implemented");
620 return wxTreeItemId();
623 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
625 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
627 wxFAIL_MSG("not implemented");
629 return wxTreeItemId();
632 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
634 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
636 wxFAIL_MSG("not implemented");
638 return wxTreeItemId();
641 // -----------------------------------------------------------------------------
643 // -----------------------------------------------------------------------------
645 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
647 const wxString
& text
,
648 int image
, int selImage
,
649 wxTreeItemData
*data
)
651 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
654 // should we give a warning here?
655 return AddRoot(text
, image
, selImage
, data
);
659 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
666 data
->m_pItem
= item
;
669 parent
->Insert( item
, previous
);
676 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
677 int image
, int selImage
,
678 wxTreeItemData
*data
)
680 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), "tree can have only one root" );
683 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
684 image
, selImage
, data
);
687 data
->m_pItem
= m_anchor
;
690 AdjustMyScrollbars();
696 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
697 const wxString
& text
,
698 int image
, int selImage
,
699 wxTreeItemData
*data
)
701 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
704 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
705 const wxTreeItemId
& idPrevious
,
706 const wxString
& text
,
707 int image
, int selImage
,
708 wxTreeItemData
*data
)
710 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
713 // should we give a warning here?
714 return AddRoot(text
, image
, selImage
, data
);
717 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
718 wxASSERT_MSG( index
!= wxNOT_FOUND
,
719 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
720 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
723 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
724 const wxString
& text
,
725 int image
, int selImage
,
726 wxTreeItemData
*data
)
728 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
731 // should we give a warning here?
732 return AddRoot(text
, image
, selImage
, data
);
735 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
736 image
, selImage
, data
);
739 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
741 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
743 event
.SetEventObject( this );
744 ProcessEvent( event
);
747 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
749 wxGenericTreeItem
*item
= itemId
.m_pItem
;
750 item
->DeleteChildren(this);
755 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
757 wxGenericTreeItem
*item
= itemId
.m_pItem
;
758 wxGenericTreeItem
*parent
= item
->GetParent();
762 parent
->GetChildren().Remove(item
);
765 item
->DeleteChildren(this);
766 SendDeleteEvent(item
);
772 void wxTreeCtrl::DeleteAllItems()
776 m_anchor
->DeleteChildren(this);
785 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
787 wxGenericTreeItem
*item
= itemId
.m_pItem
;
789 if ( !item
->HasPlus() )
792 if ( item
->IsExpanded() )
795 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
797 event
.SetEventObject( this );
798 if ( ProcessEvent( event
) && event
.m_code
)
800 // cancelled by program
805 CalculatePositions();
807 RefreshSubtree(item
);
809 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
810 ProcessEvent( event
);
813 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
815 wxGenericTreeItem
*item
= itemId
.m_pItem
;
817 if ( !item
->IsExpanded() )
820 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
822 event
.SetEventObject( this );
823 if ( ProcessEvent( event
) && event
.m_code
)
825 // cancelled by program
831 wxArrayTreeItems
& children
= item
->GetChildren();
832 size_t count
= children
.Count();
833 for ( size_t n
= 0; n
< count
; n
++ )
835 Collapse(children
[n
]);
838 CalculatePositions();
840 RefreshSubtree(item
);
842 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
843 ProcessEvent( event
);
846 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
849 DeleteChildren(item
);
852 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
854 wxGenericTreeItem
*item
= itemId
.m_pItem
;
856 if ( item
->IsExpanded() )
862 void wxTreeCtrl::Unselect()
866 m_current
->SetHilight( FALSE
);
867 RefreshLine( m_current
);
871 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
873 wxGenericTreeItem
*item
= itemId
.m_pItem
;
875 if ( m_current
!= item
)
877 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
879 event
.m_itemOld
= m_current
;
880 event
.SetEventObject( this );
881 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
886 m_current
->SetHilight( FALSE
);
887 RefreshLine( m_current
);
891 m_current
->SetHilight( TRUE
);
892 RefreshLine( m_current
);
894 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
895 GetEventHandler()->ProcessEvent( event
);
899 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
901 wxGenericTreeItem
*gitem
= item
.m_pItem
;
903 int item_y
= gitem
->GetY();
907 ViewStart( &start_x
, &start_y
);
912 GetClientSize( &client_w
, &client_h
);
914 if (item_y
< start_y
+3)
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 );
925 if (item_y
> start_y
+client_h
-16)
929 m_anchor
->GetSize( x
, y
);
931 int x_pos
= GetScrollPos( wxHORIZONTAL
);
932 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
937 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
939 wxFAIL_MSG("not implemented");
942 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
943 wxClassInfo
* WXUNUSED(textCtrlClass
) )
945 wxFAIL_MSG("not implemented");
947 return (wxTextCtrl
*)NULL
;
950 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
952 wxFAIL_MSG("not implemented");
954 return (wxTextCtrl
*)NULL
;
957 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
959 wxFAIL_MSG("not implemented");
962 // FIXME: tree sorting functions are not reentrant and not MT-safe!
963 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
965 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
966 wxGenericTreeItem
**item2
)
968 wxCHECK_MSG( s_treeBeingSorted
, 0, "bug in wxTreeCtrl::SortChildren()" );
970 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
973 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
974 const wxTreeItemId
& item2
)
976 return strcmp(GetItemText(item1
), GetItemText(item2
));
979 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
981 wxCHECK_RET( itemId
.IsOk(), "invalid tree item" );
983 wxGenericTreeItem
*item
= itemId
.m_pItem
;
985 wxCHECK_RET( !s_treeBeingSorted
,
986 "wxTreeCtrl::SortChildren is not reentrant" );
988 wxArrayTreeItems
& children
= item
->GetChildren();
989 if ( children
.Count() > 1 )
991 s_treeBeingSorted
= this;
992 children
.Sort(tree_ctrl_compare_func
);
993 s_treeBeingSorted
= NULL
;
997 //else: don't make the tree dirty as nothing changed
1000 wxImageList
*wxTreeCtrl::GetImageList() const
1002 return m_imageListNormal
;
1005 wxImageList
*wxTreeCtrl::GetStateImageList() const
1007 return m_imageListState
;
1010 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1012 m_imageListNormal
= imageList
;
1015 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1017 m_imageListState
= imageList
;
1020 // -----------------------------------------------------------------------------
1022 // -----------------------------------------------------------------------------
1024 void wxTreeCtrl::AdjustMyScrollbars()
1030 m_anchor
->GetSize( x
, y
);
1031 y
+= 2*m_lineHeight
;
1032 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1033 int y_pos
= GetScrollPos( wxVERTICAL
);
1034 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
1038 SetScrollbars( 0, 0, 0, 0 );
1042 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1044 /* render bold items in bold */
1050 fontOld
= dc
.GetFont();
1053 /* @@ is there any better way to make a bold variant of old font? */
1054 fontNew
= wxFont( fontOld
.GetPointSize(),
1055 fontOld
.GetFamily(),
1058 fontOld
.GetUnderlined());
1059 dc
.SetFont(fontNew
);
1063 wxFAIL_MSG("wxDC::GetFont() failed!");
1069 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1073 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1075 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1078 else if (item
->GetImage() != -1)
1080 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1084 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
1086 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1088 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1089 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1090 item
->GetX(), item
->GetY()-1,
1091 wxIMAGELIST_DRAW_TRANSPARENT
);
1092 dc
.DestroyClippingRegion();
1094 else if (item
->GetImage() != -1)
1096 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1097 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1098 item
->GetX(), item
->GetY()-1,
1099 wxIMAGELIST_DRAW_TRANSPARENT
);
1100 dc
.DestroyClippingRegion();
1103 dc
.SetBackgroundMode(wxTRANSPARENT
);
1104 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY() );
1106 /* restore normal font for bold items */
1109 dc
.SetFont( fontOld
);
1113 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1115 int horizX
= level
*m_indent
;
1117 item
->SetX( horizX
+33 );
1118 item
->SetY( y
-m_lineHeight
/3 );
1119 item
->SetHeight( m_lineHeight
);
1121 item
->SetCross( horizX
+15, y
);
1125 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1126 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1128 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
1130 int startX
= horizX
;
1131 int endX
= horizX
+ 10;
1133 if (!item
->HasChildren()) endX
+= 20;
1135 dc
.DrawLine( startX
, y
, endX
, y
);
1137 if (item
->HasPlus())
1139 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
1140 dc
.SetPen( *wxGREY_PEN
);
1141 dc
.SetBrush( *wxWHITE_BRUSH
);
1142 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
1143 dc
.SetPen( *wxBLACK_PEN
);
1144 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
1146 if (!item
->IsExpanded())
1148 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
1152 if (item
->HasHilight())
1154 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1156 dc
.SetBrush( *m_hilightBrush
);
1159 dc
.SetPen( *wxBLACK_PEN
);
1161 dc
.SetPen( *wxTRANSPARENT_PEN
);
1163 PaintItem(item
, dc
);
1165 dc
.SetPen( *wxBLACK_PEN
);
1166 dc
.SetTextForeground( *wxBLACK
);
1167 dc
.SetBrush( *wxWHITE_BRUSH
);
1171 dc
.SetBrush( *wxWHITE_BRUSH
);
1172 dc
.SetPen( *wxTRANSPARENT_PEN
);
1174 PaintItem(item
, dc
);
1176 dc
.SetPen( *wxBLACK_PEN
);
1180 if (item
->IsExpanded())
1184 wxArrayTreeItems
& children
= item
->GetChildren();
1185 size_t count
= children
.Count();
1186 for ( size_t n
= 0; n
< count
; n
++ )
1190 PaintLevel( children
[n
], dc
, level
+1, y
);
1193 /* it may happen that the item is expanded but has no items (when you
1194 * delete all its children for example) - don't draw the vertical line
1196 if (count
> 0) dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1200 // -----------------------------------------------------------------------------
1201 // wxWindows callbacks
1202 // -----------------------------------------------------------------------------
1204 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1212 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1214 dc
.SetPen( m_dottedPen
);
1215 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1217 int y
= m_lineHeight
/ 2 + 2;
1218 PaintLevel( m_anchor
, dc
, 0, y
);
1221 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1225 if (m_current
) RefreshLine( m_current
);
1228 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1232 if (m_current
) RefreshLine( m_current
);
1235 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1237 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1238 te
.m_code
= event
.KeyCode();
1239 te
.SetEventObject( this );
1240 GetEventHandler()->ProcessEvent( te
);
1248 switch (event
.KeyCode())
1252 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1260 if (IsExpanded(m_current
))
1262 Collapse(m_current
);
1274 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1275 event
.m_item
= m_current
;
1277 event
.SetEventObject( this );
1278 GetEventHandler()->ProcessEvent( event
);
1282 // up goes to the previous sibling or to the last of its children if
1286 wxTreeItemId prev
= GetPrevSibling( m_current
);
1289 prev
= GetParent( m_current
);
1291 wxTreeItemId current
= m_current
;
1292 if (current
== GetFirstChild( prev
, cockie
))
1294 // otherwise we return to where we came from
1296 EnsureVisible( prev
);
1302 while (IsExpanded(prev
))
1304 int c
= (int)GetChildrenCount( prev
, FALSE
);
1306 prev
= GetFirstChild( prev
, cockie
);
1307 for (int i
= 0; i
< c
-1; i
++)
1308 prev
= GetNextSibling( prev
);
1311 EnsureVisible( prev
);
1316 // left arrow goes to the parent
1319 wxTreeItemId prev
= GetParent( m_current
);
1322 EnsureVisible( prev
);
1329 // this works the same as the down arrow except that we also expand the
1330 // item if it wasn't expanded yet
1336 if (IsExpanded(m_current
))
1339 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1340 SelectItem( child
);
1341 EnsureVisible( child
);
1345 wxTreeItemId next
= GetNextSibling( m_current
);
1348 wxTreeItemId current
= m_current
;
1349 while (current
&& !next
)
1351 current
= GetParent( current
);
1352 if (current
) next
= GetNextSibling( current
);
1358 EnsureVisible( next
);
1364 // <End> selects the last visible tree item
1367 wxTreeItemId last
= GetRootItem();
1369 while ( last
.IsOk() && IsExpanded(last
) )
1371 wxTreeItemId lastChild
= GetLastChild(last
);
1373 // it may happen if the item was expanded but then all of
1374 // its children have been deleted - so IsExpanded() returned
1375 // TRUE, but GetLastChild() returned invalid item
1384 EnsureVisible( last
);
1390 // <Home> selects the root item
1393 wxTreeItemId prev
= GetRootItem();
1396 EnsureVisible( prev
);
1407 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& WXUNUSED(flags
))
1409 bool onButton
= FALSE
;
1410 return m_anchor
->HitTest( point
, onButton
);
1413 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1415 if (!event
.LeftIsDown()) m_dragCount
= 0;
1417 if ( !(event
.LeftDown() || event
.LeftDClick() || event
.Dragging()) ) return;
1419 if ( !m_anchor
) return;
1421 wxClientDC
dc(this);
1423 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1424 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1426 bool onButton
= FALSE
;
1427 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1429 if (item
== NULL
) return; /* we hit the blank area */
1431 if (event
.Dragging())
1433 if (m_dragCount
== 2) /* small drag latency (3?) */
1437 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG
, GetId());
1438 nevent
.m_item
= m_current
;
1439 nevent
.SetEventObject(this);
1440 GetEventHandler()->ProcessEvent(nevent
);
1449 if (!IsSelected(item
)) SelectItem(item
); /* we dont support multiple selections, BTW */
1451 if (event
.LeftDClick())
1453 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1454 event
.m_item
= item
;
1456 event
.SetEventObject( this );
1457 GetEventHandler()->ProcessEvent( event
);
1466 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1468 /* after all changes have been done to the tree control,
1469 * we actually redraw the tree when everything is over */
1471 if (!m_dirty
) return;
1475 CalculatePositions();
1477 AdjustMyScrollbars();
1480 // -----------------------------------------------------------------------------
1482 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1484 int horizX
= level
*m_indent
;
1486 item
->SetX( horizX
+33 );
1487 item
->SetY( y
-m_lineHeight
/3-2 );
1488 item
->SetHeight( m_lineHeight
);
1490 if ( !item
->IsExpanded() )
1492 /* we dont need to calculate collapsed branches */
1496 wxArrayTreeItems
& children
= item
->GetChildren();
1497 size_t count
= children
.Count();
1498 for ( size_t n
= 0; n
< count
; n
++ )
1501 CalculateLevel( children
[n
], dc
, level
+1, y
); /* recurse */
1505 void wxTreeCtrl::CalculatePositions()
1507 if ( !m_anchor
) return;
1509 wxClientDC
dc(this);
1512 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1514 dc
.SetPen( m_dottedPen
);
1515 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1517 int y
= m_lineHeight
/ 2 + 2;
1518 CalculateLevel( m_anchor
, dc
, 0, y
); /* start recursion */
1521 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1523 wxClientDC
dc(this);
1528 GetClientSize( &cw
, &ch
);
1531 rect
.x
= dc
.LogicalToDeviceX( 0 );
1533 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1536 Refresh( TRUE
, &rect
);
1538 AdjustMyScrollbars();
1541 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1543 wxClientDC
dc(this);
1547 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1548 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1550 rect
.height
= dc
.GetCharHeight() + 6;
1552 Refresh( TRUE
, &rect
);