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 wxTreeItemCmpFunc tree_ctrl_compare_func_2
;
940 int tree_ctrl_compare_func_1( wxGenericTreeItem
**line1
, wxGenericTreeItem
**line2
)
942 if (tree_ctrl_compare_func_2
== NULL
)
944 return strcmp( (*line1
)->GetText(), (*line2
)->GetText() );
948 wxTreeItemData
*data1
= (*line1
)->GetData();
949 wxTreeItemData
*data2
= (*line2
)->GetData();
950 return tree_ctrl_compare_func_2( data1
, data2
);
954 void wxTreeCtrl::SortChildren( const wxTreeItemId
& item
,
955 wxTreeItemCmpFunc
*cmpFunction
)
957 wxGenericTreeItem
*gitem
= item
.m_pItem
;
961 if (cmpFunction
== NULL
)
962 tree_ctrl_compare_func_2
= NULL
;
964 tree_ctrl_compare_func_2
= *cmpFunction
;
966 gitem
->GetChildren().Sort( *tree_ctrl_compare_func_1
);
971 wxImageList
*wxTreeCtrl::GetImageList() const
973 return m_imageListNormal
;
976 wxImageList
*wxTreeCtrl::GetStateImageList() const
978 return m_imageListState
;
981 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
983 m_imageListNormal
= imageList
;
986 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
988 m_imageListState
= imageList
;
991 // -----------------------------------------------------------------------------
993 // -----------------------------------------------------------------------------
995 void wxTreeCtrl::AdjustMyScrollbars()
1001 m_anchor
->GetSize( x
, y
);
1002 y
+= 2*m_lineHeight
;
1003 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1004 int y_pos
= GetScrollPos( wxVERTICAL
);
1005 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
1009 SetScrollbars( 0, 0, 0, 0 );
1013 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1015 // render bold items in bold
1019 if ( item
->IsBold() )
1021 fontOld
= dc
.GetFont();
1024 // @@ is there any better way to make a bold variant of old font?
1025 fontNew
= wxFont( fontOld
.GetPointSize(),
1026 fontOld
.GetFamily(),
1029 fontOld
.GetUnderlined());
1030 dc
.SetFont(fontNew
);
1034 wxFAIL_MSG("wxDC::GetFont() failed!");
1040 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1044 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1046 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1049 else if (item
->GetImage() != -1)
1051 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1055 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
1057 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1059 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1060 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1061 item
->GetX(), item
->GetY()-1,
1062 wxIMAGELIST_DRAW_TRANSPARENT
);
1063 dc
.DestroyClippingRegion();
1065 else if (item
->GetImage() != -1)
1067 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1068 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1069 item
->GetX(), item
->GetY()-1,
1070 wxIMAGELIST_DRAW_TRANSPARENT
);
1071 dc
.DestroyClippingRegion();
1074 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY() );
1076 // restore normal font for bold items
1079 dc
.SetFont( fontOld
);
1083 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1085 int horizX
= level
*m_indent
;
1087 item
->SetX( horizX
+33 );
1088 item
->SetY( y
-m_lineHeight
/3 );
1089 item
->SetHeight( m_lineHeight
);
1091 item
->SetCross( horizX
+15, y
);
1095 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1096 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1098 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
1100 int startX
= horizX
;
1101 int endX
= horizX
+ 10;
1103 if (!item
->HasChildren()) endX
+= 20;
1105 dc
.DrawLine( startX
, y
, endX
, y
);
1107 if (item
->HasPlus())
1109 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
1110 dc
.SetPen( *wxGREY_PEN
);
1111 dc
.SetBrush( *wxWHITE_BRUSH
);
1112 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
1113 dc
.SetPen( *wxBLACK_PEN
);
1114 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
1116 if (!item
->IsExpanded())
1117 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
1120 if (item
->HasHilight())
1122 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1124 dc
.SetBrush( *m_hilightBrush
);
1127 dc
.SetPen( *wxBLACK_PEN
);
1129 dc
.SetPen( *wxTRANSPARENT_PEN
);
1131 PaintItem(item
, dc
);
1133 dc
.SetPen( *wxBLACK_PEN
);
1134 dc
.SetTextForeground( *wxBLACK
);
1135 dc
.SetBrush( *wxWHITE_BRUSH
);
1139 dc
.SetBrush( *wxWHITE_BRUSH
);
1140 dc
.SetPen( *wxTRANSPARENT_PEN
);
1142 PaintItem(item
, dc
);
1144 dc
.SetPen( *wxBLACK_PEN
);
1148 if ( item
->IsExpanded() )
1152 wxArrayTreeItems
& children
= item
->GetChildren();
1153 size_t count
= children
.Count();
1154 for ( size_t n
= 0; n
< count
; n
++ )
1159 PaintLevel( children
[n
], dc
, level
+1, y
);
1162 // it may happen that the item is expanded but has no items (when you
1163 // delete all its children for example) - don't draw the vertical line
1166 dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1170 // -----------------------------------------------------------------------------
1171 // wxWindows callbacks
1172 // -----------------------------------------------------------------------------
1174 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1182 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1184 dc
.SetPen( m_dottedPen
);
1185 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1187 int y
= m_lineHeight
/ 2 + 2;
1188 PaintLevel( m_anchor
, dc
, 0, y
);
1191 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1195 RefreshLine( m_current
);
1198 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1202 RefreshLine( m_current
);
1205 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1207 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1208 te
.m_code
= event
.KeyCode();
1209 te
.SetEventObject( this );
1210 GetEventHandler()->ProcessEvent( te
);
1218 switch (event
.KeyCode())
1222 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1230 if (IsExpanded(m_current
))
1232 Collapse(m_current
);
1244 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1245 event
.m_item
= m_current
;
1247 event
.SetEventObject( this );
1248 GetEventHandler()->ProcessEvent( event
);
1255 wxTreeItemId prev
= GetPrevSibling( m_current
);
1259 EnsureVisible( prev
);
1263 prev
= GetParent( m_current
);
1266 EnsureVisible( prev
);
1274 // this works the same as the down arrow except that we also expand the
1275 // item if it wasn't expanded yet
1281 if (IsExpanded(m_current
))
1284 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1285 SelectItem( child
);
1286 EnsureVisible( child
);
1290 wxTreeItemId next
= GetNextSibling( m_current
);
1293 wxTreeItemId current
= m_current
;
1294 while (current
&& !next
)
1296 current
= GetParent( current
);
1297 if (current
) next
= GetNextSibling( current
);
1303 EnsureVisible( next
);
1314 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& WXUNUSED(flags
))
1316 bool onButton
= FALSE
;
1317 return m_anchor
->HitTest( point
, onButton
);
1320 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1322 if ( !(event
.LeftDown() || event
.LeftDClick()) )
1328 wxClientDC
dc(this);
1330 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1331 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1333 bool onButton
= FALSE
;
1334 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1338 if (!IsSelected(item
)) SelectItem(item
);
1340 if ( event
.LeftDClick() )
1342 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1343 event
.m_item
= item
;
1345 event
.SetEventObject( this );
1346 GetEventHandler()->ProcessEvent( event
);
1355 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1357 if (!m_dirty
) return;
1361 CalculatePositions();
1363 AdjustMyScrollbars();
1366 // -----------------------------------------------------------------------------
1367 // -----------------------------------------------------------------------------
1368 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
,
1373 int horizX
= level
*m_indent
;
1375 item
->SetX( horizX
+33 );
1376 item
->SetY( y
-m_lineHeight
/3-2 );
1377 item
->SetHeight( m_lineHeight
);
1379 if ( item
->IsExpanded() )
1382 wxArrayTreeItems
& children
= item
->GetChildren();
1383 size_t count
= children
.Count();
1384 for ( size_t n
= 0; n
< count
; n
++ )
1387 CalculateLevel( children
[n
], dc
, level
+1, y
);
1391 void wxTreeCtrl::CalculatePositions()
1396 wxClientDC
dc(this);
1399 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1401 dc
.SetPen( m_dottedPen
);
1402 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1404 int y
= m_lineHeight
/ 2 + 2;
1405 CalculateLevel( m_anchor
, dc
, 0, y
);
1408 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1410 wxClientDC
dc(this);
1415 GetClientSize( &cw
, &ch
);
1418 rect
.x
= dc
.LogicalToDeviceX( 0 );
1420 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1423 Refresh( TRUE
, &rect
);
1425 AdjustMyScrollbars();
1428 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1430 wxClientDC
dc(this);
1434 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1435 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1437 rect
.height
= dc
.GetCharHeight() + 6;
1438 Refresh( TRUE
, &rect
);