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()))
290 if (m_image
!= -1) w
+= 20;
292 if ((point
.x
> m_x
) && (point
.x
< m_x
+w
))
302 size_t count
= m_children
.Count();
303 for ( size_t n
= 0; n
< count
; n
++ )
305 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, onButton
);
315 // -----------------------------------------------------------------------------
316 // wxTreeCtrl implementation
317 // -----------------------------------------------------------------------------
319 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
321 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
322 EVT_PAINT (wxTreeCtrl::OnPaint
)
323 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
324 EVT_CHAR (wxTreeCtrl::OnChar
)
325 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
326 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
327 EVT_IDLE (wxTreeCtrl::OnIdle
)
330 // -----------------------------------------------------------------------------
331 // construction/destruction
332 // -----------------------------------------------------------------------------
333 void wxTreeCtrl::Init()
336 m_anchor
= (wxGenericTreeItem
*) NULL
;
345 m_hilightBrush
= new wxBrush
347 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
352 m_imageListState
= (wxImageList
*) NULL
;
355 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
356 const wxPoint
& pos
, const wxSize
& size
,
358 const wxValidator
&validator
,
359 const wxString
& name
)
363 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
365 SetValidator( validator
);
367 SetBackgroundColour( *wxWHITE
);
368 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
373 wxTreeCtrl::~wxTreeCtrl()
375 wxDELETE( m_hilightBrush
);
380 // -----------------------------------------------------------------------------
382 // -----------------------------------------------------------------------------
384 size_t wxTreeCtrl::GetCount() const
386 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
389 void wxTreeCtrl::SetIndent(unsigned int indent
)
395 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
397 wxCHECK_MSG( item
.IsOk(), 0u, "invalid tree item" );
399 return item
.m_pItem
->GetChildrenCount(recursively
);
402 // -----------------------------------------------------------------------------
403 // functions to work with tree items
404 // -----------------------------------------------------------------------------
406 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
408 wxCHECK_MSG( item
.IsOk(), "", "invalid tree item" );
410 return item
.m_pItem
->GetText();
413 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
415 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
417 return item
.m_pItem
->GetImage();
420 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
422 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
424 return item
.m_pItem
->GetSelectedImage();
427 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
429 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
431 return item
.m_pItem
->GetData();
434 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
436 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
439 item
.m_pItem
->SetText(text
, dc
);
442 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
444 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
446 item
.m_pItem
->SetImage(image
);
449 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
451 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
453 item
.m_pItem
->SetSelectedImage(image
);
456 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
458 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
460 item
.m_pItem
->SetData(data
);
463 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
465 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
467 item
.m_pItem
->SetHasPlus(has
);
470 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
472 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
474 // avoid redrawing the tree if no real change
475 wxGenericTreeItem
*pItem
= item
.m_pItem
;
476 if ( pItem
->IsBold() != bold
)
478 pItem
->SetBold(bold
);
483 // -----------------------------------------------------------------------------
484 // item status inquiries
485 // -----------------------------------------------------------------------------
487 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
489 wxFAIL_MSG("not implemented");
494 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
496 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
498 return !item
.m_pItem
->GetChildren().IsEmpty();
501 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
503 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
505 return item
.m_pItem
->IsExpanded();
508 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
510 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
512 return item
.m_pItem
->HasHilight();
515 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
517 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
519 return item
.m_pItem
->IsBold();
522 // -----------------------------------------------------------------------------
524 // -----------------------------------------------------------------------------
526 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
528 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
530 return item
.m_pItem
->GetParent();
533 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
535 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
538 return GetNextChild(item
, cookie
);
541 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
543 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
545 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
546 if ( (size_t)cookie
< children
.Count() )
548 return item
.m_pItem
->GetChildren().Item(cookie
++);
552 // there are no more of them
557 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
559 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
561 wxGenericTreeItem
*i
= item
.m_pItem
;
562 wxGenericTreeItem
*parent
= i
->GetParent();
563 if ( parent
== NULL
)
565 // root item doesn't have any siblings
569 wxArrayTreeItems
& siblings
= parent
->GetChildren();
570 int index
= siblings
.Index(i
);
571 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
573 size_t n
= (size_t)(index
+ 1);
574 return n
== siblings
.Count() ? (wxGenericTreeItem
*)NULL
: siblings
[n
];
577 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
579 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
581 wxGenericTreeItem
*i
= item
.m_pItem
;
582 wxGenericTreeItem
*parent
= i
->GetParent();
583 if ( parent
== NULL
)
585 // root item doesn't have any siblings
589 wxArrayTreeItems
& siblings
= parent
->GetChildren();
590 int index
= siblings
.Index(i
);
591 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
593 return index
== 0 ? (wxGenericTreeItem
*)NULL
: siblings
[(size_t)(index
- 1)];
596 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
598 wxFAIL_MSG("not implemented");
603 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
605 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
607 wxFAIL_MSG("not implemented");
612 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
614 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
616 wxFAIL_MSG("not implemented");
621 // -----------------------------------------------------------------------------
623 // -----------------------------------------------------------------------------
625 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
627 const wxString
& text
,
628 int image
, int selImage
,
629 wxTreeItemData
*data
)
631 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
634 // should we give a warning here?
635 return AddRoot(text
, image
, selImage
, data
);
639 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
646 data
->m_pItem
= item
;
649 parent
->Insert( item
, previous
);
656 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
657 int image
, int selImage
,
658 wxTreeItemData
*data
)
660 wxCHECK_MSG( !m_anchor
, NULL
, "tree can have only one root" );
663 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
664 image
, selImage
, data
);
667 data
->m_pItem
= m_anchor
;
670 AdjustMyScrollbars();
676 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
677 const wxString
& text
,
678 int image
, int selImage
,
679 wxTreeItemData
*data
)
681 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
684 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
685 const wxTreeItemId
& idPrevious
,
686 const wxString
& text
,
687 int image
, int selImage
,
688 wxTreeItemData
*data
)
690 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
693 // should we give a warning here?
694 return AddRoot(text
, image
, selImage
, data
);
697 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
698 wxASSERT_MSG( index
!= NOT_FOUND
,
699 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
700 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
703 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
704 const wxString
& text
,
705 int image
, int selImage
,
706 wxTreeItemData
*data
)
708 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
711 // should we give a warning here?
712 return AddRoot(text
, image
, selImage
, data
);
715 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
716 image
, selImage
, data
);
719 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
721 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
723 event
.SetEventObject( this );
724 ProcessEvent( event
);
727 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
729 wxGenericTreeItem
*item
= itemId
.m_pItem
;
730 item
->DeleteChildren(this);
735 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
737 wxGenericTreeItem
*item
= itemId
.m_pItem
;
738 wxGenericTreeItem
*parent
= item
->GetParent();
742 parent
->GetChildren().Remove(item
);
745 item
->DeleteChildren(this);
746 SendDeleteEvent(item
);
752 void wxTreeCtrl::DeleteAllItems()
756 m_anchor
->DeleteChildren(this);
765 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
767 wxGenericTreeItem
*item
= itemId
.m_pItem
;
769 if ( !item
->HasPlus() )
772 if ( item
->IsExpanded() )
775 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
777 event
.SetEventObject( this );
778 if ( ProcessEvent( event
) && event
.m_code
)
780 // cancelled by program
785 CalculatePositions();
787 RefreshSubtree(item
);
789 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
790 ProcessEvent( event
);
793 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
795 wxGenericTreeItem
*item
= itemId
.m_pItem
;
797 if ( !item
->IsExpanded() )
800 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
802 event
.SetEventObject( this );
803 if ( ProcessEvent( event
) && event
.m_code
)
805 // cancelled by program
811 wxArrayTreeItems
& children
= item
->GetChildren();
812 size_t count
= children
.Count();
813 for ( size_t n
= 0; n
< count
; n
++ )
815 Collapse(children
[n
]);
818 CalculatePositions();
820 RefreshSubtree(item
);
822 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
823 ProcessEvent( event
);
826 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
829 DeleteChildren(item
);
832 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
834 wxGenericTreeItem
*item
= itemId
.m_pItem
;
836 if ( item
->IsExpanded() )
842 void wxTreeCtrl::Unselect()
846 m_current
->SetHilight( FALSE
);
847 RefreshLine( m_current
);
851 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
853 wxGenericTreeItem
*item
= itemId
.m_pItem
;
855 if ( m_current
!= item
)
857 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
859 event
.m_itemOld
= m_current
;
860 event
.SetEventObject( this );
861 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
866 m_current
->SetHilight( FALSE
);
867 RefreshLine( m_current
);
871 m_current
->SetHilight( TRUE
);
872 RefreshLine( m_current
);
874 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
875 GetEventHandler()->ProcessEvent( event
);
879 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
881 wxGenericTreeItem
*gitem
= item
.m_pItem
;
883 int item_y
= gitem
->GetY();
887 ViewStart( &start_x
, &start_y
);
892 GetClientSize( &client_w
, &client_h
);
894 if (item_y
< start_y
+3)
898 m_anchor
->GetSize( x
, y
);
900 int x_pos
= GetScrollPos( wxHORIZONTAL
);
901 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
905 if (item_y
> start_y
+client_h
-16)
909 m_anchor
->GetSize( x
, y
);
911 int x_pos
= GetScrollPos( wxHORIZONTAL
);
912 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
917 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
919 wxFAIL_MSG("not implemented");
922 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
923 wxClassInfo
* WXUNUSED(textCtrlClass
) )
925 wxFAIL_MSG("not implemented");
927 return (wxTextCtrl
*)NULL
;
930 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
932 wxFAIL_MSG("not implemented");
934 return (wxTextCtrl
*)NULL
;
937 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
939 wxFAIL_MSG("not implemented");
942 // FIXME: tree sorting functions are not reentrant and not MT-safe!
943 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
945 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
946 wxGenericTreeItem
**item2
)
948 wxCHECK_MSG( s_treeBeingSorted
, 0, "bug in wxTreeCtrl::SortChildren()" );
950 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
953 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
954 const wxTreeItemId
& item2
)
956 return strcmp(GetItemText(item1
), GetItemText(item2
));
959 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
961 wxCHECK_RET( itemId
.IsOk(), "invalid tree item" );
963 wxGenericTreeItem
*item
= itemId
.m_pItem
;
965 wxCHECK_RET( !s_treeBeingSorted
,
966 "wxTreeCtrl::SortChildren is not reentrant" );
968 wxArrayTreeItems
& children
= item
->GetChildren();
969 if ( children
.Count() > 1 )
971 s_treeBeingSorted
= this;
972 children
.Sort(tree_ctrl_compare_func
);
973 s_treeBeingSorted
= NULL
;
977 //else: don't make the tree dirty as nothing changed
980 wxImageList
*wxTreeCtrl::GetImageList() const
982 return m_imageListNormal
;
985 wxImageList
*wxTreeCtrl::GetStateImageList() const
987 return m_imageListState
;
990 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
992 m_imageListNormal
= imageList
;
995 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
997 m_imageListState
= imageList
;
1000 // -----------------------------------------------------------------------------
1002 // -----------------------------------------------------------------------------
1004 void wxTreeCtrl::AdjustMyScrollbars()
1010 m_anchor
->GetSize( x
, y
);
1011 y
+= 2*m_lineHeight
;
1012 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1013 int y_pos
= GetScrollPos( wxVERTICAL
);
1014 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
1018 SetScrollbars( 0, 0, 0, 0 );
1022 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1024 // render bold items in bold
1028 if ( item
->IsBold() )
1030 fontOld
= dc
.GetFont();
1033 // @@ is there any better way to make a bold variant of old font?
1034 fontNew
= wxFont( fontOld
.GetPointSize(),
1035 fontOld
.GetFamily(),
1038 fontOld
.GetUnderlined());
1039 dc
.SetFont(fontNew
);
1043 wxFAIL_MSG("wxDC::GetFont() failed!");
1049 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1053 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1055 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1058 else if (item
->GetImage() != -1)
1060 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1064 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
1066 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1068 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1069 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1070 item
->GetX(), item
->GetY()-1,
1071 wxIMAGELIST_DRAW_TRANSPARENT
);
1072 dc
.DestroyClippingRegion();
1074 else if (item
->GetImage() != -1)
1076 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1077 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1078 item
->GetX(), item
->GetY()-1,
1079 wxIMAGELIST_DRAW_TRANSPARENT
);
1080 dc
.DestroyClippingRegion();
1083 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY() );
1085 // restore normal font for bold items
1088 dc
.SetFont( fontOld
);
1092 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1094 int horizX
= level
*m_indent
;
1096 item
->SetX( horizX
+33 );
1097 item
->SetY( y
-m_lineHeight
/3 );
1098 item
->SetHeight( m_lineHeight
);
1100 item
->SetCross( horizX
+15, y
);
1104 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1105 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1107 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
1109 int startX
= horizX
;
1110 int endX
= horizX
+ 10;
1112 if (!item
->HasChildren()) endX
+= 20;
1114 dc
.DrawLine( startX
, y
, endX
, y
);
1116 if (item
->HasPlus())
1118 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
1119 dc
.SetPen( *wxGREY_PEN
);
1120 dc
.SetBrush( *wxWHITE_BRUSH
);
1121 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
1122 dc
.SetPen( *wxBLACK_PEN
);
1123 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
1125 if (!item
->IsExpanded())
1126 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
1129 if (item
->HasHilight())
1131 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1133 dc
.SetBrush( *m_hilightBrush
);
1136 dc
.SetPen( *wxBLACK_PEN
);
1138 dc
.SetPen( *wxTRANSPARENT_PEN
);
1140 PaintItem(item
, dc
);
1142 dc
.SetPen( *wxBLACK_PEN
);
1143 dc
.SetTextForeground( *wxBLACK
);
1144 dc
.SetBrush( *wxWHITE_BRUSH
);
1148 dc
.SetBrush( *wxWHITE_BRUSH
);
1149 dc
.SetPen( *wxTRANSPARENT_PEN
);
1151 PaintItem(item
, dc
);
1153 dc
.SetPen( *wxBLACK_PEN
);
1157 if ( item
->IsExpanded() )
1161 wxArrayTreeItems
& children
= item
->GetChildren();
1162 size_t count
= children
.Count();
1163 for ( size_t n
= 0; n
< count
; n
++ )
1168 PaintLevel( children
[n
], dc
, level
+1, y
);
1171 // it may happen that the item is expanded but has no items (when you
1172 // delete all its children for example) - don't draw the vertical line
1175 dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1179 // -----------------------------------------------------------------------------
1180 // wxWindows callbacks
1181 // -----------------------------------------------------------------------------
1183 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1191 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1193 dc
.SetPen( m_dottedPen
);
1194 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1196 int y
= m_lineHeight
/ 2 + 2;
1197 PaintLevel( m_anchor
, dc
, 0, y
);
1200 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1204 RefreshLine( m_current
);
1207 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1211 RefreshLine( m_current
);
1214 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1216 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1217 te
.m_code
= event
.KeyCode();
1218 te
.SetEventObject( this );
1219 GetEventHandler()->ProcessEvent( te
);
1227 switch (event
.KeyCode())
1231 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1239 if (IsExpanded(m_current
))
1241 Collapse(m_current
);
1253 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1254 event
.m_item
= m_current
;
1256 event
.SetEventObject( this );
1257 GetEventHandler()->ProcessEvent( event
);
1263 wxTreeItemId prev
= GetPrevSibling( m_current
);
1266 prev
= GetParent( m_current
);
1268 wxTreeItemId current
= m_current
;
1269 if (current
== GetFirstChild( prev
, cockie
))
1271 // otherwise we return to where we came from
1273 EnsureVisible( prev
);
1279 while (IsExpanded(prev
))
1281 int c
= (int)GetChildrenCount( prev
, FALSE
);
1283 prev
= GetFirstChild( prev
, cockie
);
1284 for (int i
= 0; i
< c
-1; i
++)
1285 prev
= GetNextSibling( prev
);
1288 EnsureVisible( prev
);
1294 wxTreeItemId prev
= GetPrevSibling( m_current
);
1298 EnsureVisible( prev
);
1302 prev
= GetParent( m_current
);
1305 EnsureVisible( prev
);
1313 // this works the same as the down arrow except that we also expand the
1314 // item if it wasn't expanded yet
1320 if (IsExpanded(m_current
))
1323 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1324 SelectItem( child
);
1325 EnsureVisible( child
);
1329 wxTreeItemId next
= GetNextSibling( m_current
);
1332 wxTreeItemId current
= m_current
;
1333 while (current
&& !next
)
1335 current
= GetParent( current
);
1336 if (current
) next
= GetNextSibling( current
);
1342 EnsureVisible( next
);
1353 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& WXUNUSED(flags
))
1355 bool onButton
= FALSE
;
1356 return m_anchor
->HitTest( point
, onButton
);
1359 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1361 if ( !(event
.LeftDown() || event
.LeftDClick()) )
1367 wxClientDC
dc(this);
1369 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1370 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1372 bool onButton
= FALSE
;
1373 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1377 if (!IsSelected(item
)) SelectItem(item
);
1379 if ( event
.LeftDClick() )
1381 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1382 event
.m_item
= item
;
1384 event
.SetEventObject( this );
1385 GetEventHandler()->ProcessEvent( event
);
1394 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1396 if (!m_dirty
) return;
1400 CalculatePositions();
1402 AdjustMyScrollbars();
1405 // -----------------------------------------------------------------------------
1406 // -----------------------------------------------------------------------------
1407 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
,
1412 int horizX
= level
*m_indent
;
1414 item
->SetX( horizX
+33 );
1415 item
->SetY( y
-m_lineHeight
/3-2 );
1416 item
->SetHeight( m_lineHeight
);
1418 // if ( item->IsExpanded() )
1420 if ( !item
->IsExpanded() ) // Surely this is correct? JACS
1423 wxArrayTreeItems
& children
= item
->GetChildren();
1424 size_t count
= children
.Count();
1425 for ( size_t n
= 0; n
< count
; n
++ )
1428 CalculateLevel( children
[n
], dc
, level
+1, y
);
1432 void wxTreeCtrl::CalculatePositions()
1437 wxClientDC
dc(this);
1440 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1442 dc
.SetPen( m_dottedPen
);
1443 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1445 int y
= m_lineHeight
/ 2 + 2;
1446 CalculateLevel( m_anchor
, dc
, 0, y
);
1449 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1451 wxClientDC
dc(this);
1456 GetClientSize( &cw
, &ch
);
1459 rect
.x
= dc
.LogicalToDeviceX( 0 );
1461 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1464 Refresh( TRUE
, &rect
);
1466 AdjustMyScrollbars();
1469 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1471 wxClientDC
dc(this);
1475 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1476 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1478 rect
.height
= dc
.GetCharHeight() + 6;
1479 Refresh( TRUE
, &rect
);