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"
32 // -----------------------------------------------------------------------------
34 // -----------------------------------------------------------------------------
36 WX_DEFINE_ARRAY(wxGenericTreeItem
*, wxArrayTreeItems
);
38 // -----------------------------------------------------------------------------
40 // -----------------------------------------------------------------------------
43 class WXDLLEXPORT wxGenericTreeItem
47 wxGenericTreeItem() { m_data
= NULL
; }
48 wxGenericTreeItem( wxGenericTreeItem
*parent
,
51 int image
, int selImage
,
52 wxTreeItemData
*data
);
57 wxArrayTreeItems
& GetChildren() { return m_children
; }
59 const wxString
& GetText() const { return m_text
; }
60 int GetImage() const { return m_image
; }
61 int GetSelectedImage() const { return m_selImage
; }
62 wxTreeItemData
*GetData() const { return m_data
; }
64 void SetText( const wxString
&text
, wxDC
& dc
);
65 void SetImage(int image
) { m_image
= image
; }
66 void SetSelectedImage(int image
) { m_selImage
= image
; }
67 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
69 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
71 void SetBold(bool bold
) { m_isBold
= bold
; }
73 int GetX() const { return m_x
; }
74 int GetY() const { return m_y
; }
76 void SetHeight(int h
) { m_height
= h
; }
78 void SetX(int x
) { m_x
= x
; }
79 void SetY(int y
) { m_y
= y
; }
81 wxGenericTreeItem
*GetParent() const { return m_parent
; }
84 // deletes all children notifying the treectrl about it if !NULL pointer
86 void DeleteChildren(wxTreeCtrl
*tree
= NULL
);
87 // FIXME don't know what is it for
90 // get count of all children (and grand children if 'recursively')
91 size_t GetChildrenCount(bool recursively
= TRUE
) const;
93 void Insert(wxGenericTreeItem
*child
, size_t index
)
94 { m_children
.Insert(child
, index
); }
96 void SetCross( int x
, int y
);
97 void GetSize( int &x
, int &y
);
99 // return the item at given position (or NULL if no item), onButton is TRUE
100 // if the point belongs to the item's button, otherwise it lies on the
102 wxGenericTreeItem
*HitTest( const wxPoint
& point
, bool &onButton
);
104 void Expand() { m_isCollapsed
= FALSE
; }
105 void Collapse() { m_isCollapsed
= TRUE
; }
107 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
110 bool HasChildren() const { return !m_children
.IsEmpty(); }
111 bool HasHilight() const { return m_hasHilight
; }
112 bool IsExpanded() const { return !m_isCollapsed
; }
113 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
114 bool IsBold() const { return m_isBold
; }
122 wxTreeItemData
*m_data
;
124 // use bitfields to save size
125 int m_isCollapsed
:1;
126 int m_hasHilight
:1; // same as focused
127 int m_hasPlus
:1; // used for item which doesn't have
128 // children but still has a [+] button
129 int m_isBold
:1; // render the label in bold font
132 long m_height
, m_width
;
133 int m_xCross
, m_yCross
;
135 wxArrayTreeItems m_children
;
136 wxGenericTreeItem
*m_parent
;
139 // =============================================================================
141 // =============================================================================
143 // -----------------------------------------------------------------------------
145 // -----------------------------------------------------------------------------
147 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
149 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
150 : wxNotifyEvent( commandType
, id
)
153 m_itemOld
= (wxGenericTreeItem
*)NULL
;
156 // -----------------------------------------------------------------------------
158 // -----------------------------------------------------------------------------
160 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
161 const wxString
& text
,
163 int image
, int selImage
,
164 wxTreeItemData
*data
)
168 m_selImage
= selImage
;
171 m_xCross
= m_yCross
= 0;
175 m_isCollapsed
= TRUE
;
176 m_hasHilight
= FALSE
;
182 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
185 wxGenericTreeItem::~wxGenericTreeItem()
189 wxASSERT_MSG( m_children
.IsEmpty(),
190 "please call DeleteChildren() before deleting the item" );
193 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
195 size_t count
= m_children
.Count();
196 for ( size_t n
= 0; n
< count
; n
++ )
198 wxGenericTreeItem
*child
= m_children
[n
];
201 tree
->SendDeleteEvent(child
);
204 child
->DeleteChildren(tree
);
211 void wxGenericTreeItem::SetText( const wxString
&text
, wxDC
& dc
)
215 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
218 void wxGenericTreeItem::Reset()
225 m_height
= m_width
= 0;
232 m_isCollapsed
= TRUE
;
234 m_parent
= (wxGenericTreeItem
*)NULL
;
237 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
239 size_t count
= m_children
.Count();
243 size_t total
= count
;
244 for ( size_t n
= 0; n
< count
; n
++ )
246 total
+= m_children
[n
]->GetChildrenCount();
252 void wxGenericTreeItem::SetCross( int x
, int y
)
258 void wxGenericTreeItem::GetSize( int &x
, int &y
)
260 if ( y
< m_y
) y
= m_y
;
261 int width
= m_x
+ m_width
;
262 if (width
> x
) x
= width
;
266 size_t count
= m_children
.Count();
267 for ( size_t n
= 0; n
< count
; n
++ )
269 m_children
[n
]->GetSize( x
, y
);
274 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
277 if ((point
.y
> m_y
) && (point
.y
< m_y
+ m_height
))
280 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
281 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
282 (IsExpanded() || HasPlus()))
288 if ((point
.x
> m_x
) && (point
.x
< m_x
+m_width
))
298 size_t count
= m_children
.Count();
299 for ( size_t n
= 0; n
< count
; n
++ )
301 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, onButton
);
311 // -----------------------------------------------------------------------------
312 // wxTreeCtrl implementation
313 // -----------------------------------------------------------------------------
315 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
317 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
318 EVT_PAINT (wxTreeCtrl::OnPaint
)
319 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
320 EVT_CHAR (wxTreeCtrl::OnChar
)
321 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
322 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
323 EVT_IDLE (wxTreeCtrl::OnIdle
)
326 // -----------------------------------------------------------------------------
327 // construction/destruction
328 // -----------------------------------------------------------------------------
329 void wxTreeCtrl::Init()
332 m_anchor
= (wxGenericTreeItem
*) NULL
;
341 m_hilightBrush
= new wxBrush
343 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
348 m_imageListState
= (wxImageList
*) NULL
;
351 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
352 const wxPoint
& pos
, const wxSize
& size
,
354 const wxValidator
&validator
,
355 const wxString
& name
)
359 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
361 SetValidator( validator
);
363 SetBackgroundColour( *wxWHITE
);
364 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
369 wxTreeCtrl::~wxTreeCtrl()
371 wxDELETE( m_hilightBrush
);
376 // -----------------------------------------------------------------------------
378 // -----------------------------------------------------------------------------
380 size_t wxTreeCtrl::GetCount() const
382 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
385 void wxTreeCtrl::SetIndent(unsigned int indent
)
391 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
393 wxCHECK_MSG( item
.IsOk(), 0u, "invalid tree item" );
395 return item
.m_pItem
->GetChildrenCount(recursively
);
398 // -----------------------------------------------------------------------------
399 // functions to work with tree items
400 // -----------------------------------------------------------------------------
402 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
404 wxCHECK_MSG( item
.IsOk(), "", "invalid tree item" );
406 return item
.m_pItem
->GetText();
409 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
411 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
413 return item
.m_pItem
->GetImage();
416 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
418 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
420 return item
.m_pItem
->GetSelectedImage();
423 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
425 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
427 return item
.m_pItem
->GetData();
430 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
432 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
435 item
.m_pItem
->SetText(text
, dc
);
438 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
440 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
442 item
.m_pItem
->SetImage(image
);
445 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
447 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
449 item
.m_pItem
->SetSelectedImage(image
);
452 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
454 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
456 item
.m_pItem
->SetData(data
);
459 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
461 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
463 item
.m_pItem
->SetHasPlus(has
);
466 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
468 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
470 // avoid redrawing the tree if no real change
471 wxGenericTreeItem
*pItem
= item
.m_pItem
;
472 if ( pItem
->IsBold() != bold
)
474 pItem
->SetBold(bold
);
479 // -----------------------------------------------------------------------------
480 // item status inquiries
481 // -----------------------------------------------------------------------------
483 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
485 wxFAIL_MSG("not implemented");
490 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
492 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
494 return !item
.m_pItem
->GetChildren().IsEmpty();
497 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
499 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
501 return item
.m_pItem
->IsExpanded();
504 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
506 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
508 return item
.m_pItem
->HasHilight();
511 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
513 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
515 return item
.m_pItem
->IsBold();
518 // -----------------------------------------------------------------------------
520 // -----------------------------------------------------------------------------
522 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
524 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
526 return item
.m_pItem
->GetParent();
529 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
531 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
534 return GetNextChild(item
, cookie
);
537 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
539 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
541 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
542 if ( (size_t)cookie
< children
.Count() )
544 return item
.m_pItem
->GetChildren().Item(cookie
++);
548 // there are no more of them
553 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
555 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
557 wxGenericTreeItem
*i
= item
.m_pItem
;
558 wxGenericTreeItem
*parent
= i
->GetParent();
559 if ( parent
== NULL
)
561 // root item doesn't have any siblings
565 wxArrayTreeItems
& siblings
= parent
->GetChildren();
566 int index
= siblings
.Index(i
);
567 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
569 size_t n
= (size_t)(index
+ 1);
570 return n
== siblings
.Count() ? (wxGenericTreeItem
*)NULL
: siblings
[n
];
573 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
575 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
577 wxGenericTreeItem
*i
= item
.m_pItem
;
578 wxGenericTreeItem
*parent
= i
->GetParent();
579 if ( parent
== NULL
)
581 // root item doesn't have any siblings
585 wxArrayTreeItems
& siblings
= parent
->GetChildren();
586 int index
= siblings
.Index(i
);
587 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
589 return index
== 0 ? (wxGenericTreeItem
*)NULL
: siblings
[(size_t)(index
- 1)];
592 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
594 wxFAIL_MSG("not implemented");
599 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
601 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
603 wxFAIL_MSG("not implemented");
608 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
610 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
612 wxFAIL_MSG("not implemented");
617 // -----------------------------------------------------------------------------
619 // -----------------------------------------------------------------------------
621 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
623 const wxString
& text
,
624 int image
, int selImage
,
625 wxTreeItemData
*data
)
627 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
630 // should we give a warning here?
631 return AddRoot(text
, image
, selImage
, data
);
635 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
642 data
->m_pItem
= item
;
645 parent
->Insert( item
, previous
);
652 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
653 int image
, int selImage
,
654 wxTreeItemData
*data
)
656 wxCHECK_MSG( !m_anchor
, NULL
, "tree can have only one root" );
659 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
660 image
, selImage
, data
);
663 data
->m_pItem
= m_anchor
;
666 AdjustMyScrollbars();
672 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
673 const wxString
& text
,
674 int image
, int selImage
,
675 wxTreeItemData
*data
)
677 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
680 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
681 const wxTreeItemId
& idPrevious
,
682 const wxString
& text
,
683 int image
, int selImage
,
684 wxTreeItemData
*data
)
686 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
689 // should we give a warning here?
690 return AddRoot(text
, image
, selImage
, data
);
693 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
694 wxASSERT_MSG( index
!= NOT_FOUND
,
695 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
696 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
699 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
700 const wxString
& text
,
701 int image
, int selImage
,
702 wxTreeItemData
*data
)
704 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
707 // should we give a warning here?
708 return AddRoot(text
, image
, selImage
, data
);
711 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
712 image
, selImage
, data
);
715 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
717 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
719 event
.SetEventObject( this );
720 ProcessEvent( event
);
723 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
725 wxGenericTreeItem
*item
= itemId
.m_pItem
;
726 item
->DeleteChildren(this);
731 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
733 wxGenericTreeItem
*item
= itemId
.m_pItem
;
734 wxGenericTreeItem
*parent
= item
->GetParent();
738 parent
->GetChildren().Remove(item
);
741 item
->DeleteChildren(this);
742 SendDeleteEvent(item
);
748 void wxTreeCtrl::DeleteAllItems()
752 m_anchor
->DeleteChildren(this);
761 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
763 wxGenericTreeItem
*item
= itemId
.m_pItem
;
765 if ( !item
->HasPlus() )
768 if ( item
->IsExpanded() )
771 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
773 event
.SetEventObject( this );
774 if ( ProcessEvent( event
) && event
.m_code
)
776 // cancelled by program
782 RefreshSubtree(item
);
784 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
785 ProcessEvent( event
);
788 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
790 wxGenericTreeItem
*item
= itemId
.m_pItem
;
792 if ( !item
->IsExpanded() )
795 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
797 event
.SetEventObject( this );
798 if ( ProcessEvent( event
) && event
.m_code
)
800 // cancelled by program
806 wxArrayTreeItems
& children
= item
->GetChildren();
807 size_t count
= children
.Count();
808 for ( size_t n
= 0; n
< count
; n
++ )
810 Collapse(children
[n
]);
813 CalculatePositions();
815 RefreshSubtree(item
);
817 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
818 ProcessEvent( event
);
821 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
824 DeleteChildren(item
);
827 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
829 wxGenericTreeItem
*item
= itemId
.m_pItem
;
831 if ( item
->IsExpanded() )
837 void wxTreeCtrl::Unselect()
841 m_current
->SetHilight( FALSE
);
842 RefreshLine( m_current
);
846 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
848 wxGenericTreeItem
*item
= itemId
.m_pItem
;
850 if ( m_current
!= item
)
852 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
854 event
.m_itemOld
= m_current
;
855 event
.SetEventObject( this );
856 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
861 m_current
->SetHilight( FALSE
);
862 RefreshLine( m_current
);
866 m_current
->SetHilight( TRUE
);
867 RefreshLine( m_current
);
869 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
870 GetEventHandler()->ProcessEvent( event
);
874 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
876 wxGenericTreeItem
*gitem
= item
.m_pItem
;
878 int item_y
= gitem
->GetY();
882 ViewStart( &start_x
, &start_y
);
885 if (item_y
< start_y
+3)
889 m_anchor
->GetSize( x
, y
);
891 int x_pos
= GetScrollPos( wxHORIZONTAL
);
892 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, item_y
/10 );
898 GetClientSize( &w
, &h
);
900 if (item_y
> start_y
+h
-26)
904 m_anchor
->GetSize( x
, y
);
906 int x_pos
= GetScrollPos( wxHORIZONTAL
);
907 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-h
+30)/10 );
912 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
914 wxFAIL_MSG("not implemented");
917 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
918 wxClassInfo
* WXUNUSED(textCtrlClass
) )
920 wxFAIL_MSG("not implemented");
925 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
927 wxFAIL_MSG("not implemented");
932 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
934 wxFAIL_MSG("not implemented");
937 void wxTreeCtrl::SortChildren( const wxTreeItemId
& WXUNUSED(item
),
938 wxTreeItemCmpFunc
*WXUNUSED(cmpFunction
))
940 wxFAIL_MSG("not implemented");
943 wxImageList
*wxTreeCtrl::GetImageList() const
945 return m_imageListNormal
;
948 wxImageList
*wxTreeCtrl::GetStateImageList() const
950 return m_imageListState
;
953 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
955 m_imageListNormal
= imageList
;
958 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
960 m_imageListState
= imageList
;
963 // -----------------------------------------------------------------------------
965 // -----------------------------------------------------------------------------
966 void wxTreeCtrl::AdjustMyScrollbars()
972 m_anchor
->GetSize( x
, y
);
974 int x_pos
= GetScrollPos( wxHORIZONTAL
);
975 int y_pos
= GetScrollPos( wxVERTICAL
);
976 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
980 SetScrollbars( 0, 0, 0, 0 );
984 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
986 // render bold items in bold
990 if ( item
->IsBold() )
992 fontOld
= dc
.GetFont();
995 // @@ is there any better way to make a bold variant of old font?
996 fontNew
= wxFont( fontOld
.GetPointSize(),
1000 fontOld
.GetUnderlined());
1001 dc
.SetFont(fontNew
);
1005 wxFAIL_MSG("wxDC::GetFont() failed!");
1011 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1015 if (item
->GetImage() != -1)
1017 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1021 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
1023 if (item
->GetImage() != -1)
1025 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1026 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1027 item
->GetX(), item
->GetY()-1,
1028 wxIMAGELIST_DRAW_TRANSPARENT
);
1029 dc
.DestroyClippingRegion();
1032 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY() );
1034 // restore normal font for bold items
1037 dc
.SetFont( fontOld
);
1041 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1043 int horizX
= level
*m_indent
;
1045 item
->SetX( horizX
+33 );
1046 item
->SetY( y
-m_lineHeight
/3 );
1047 item
->SetHeight( m_lineHeight
);
1049 item
->SetCross( horizX
+15, y
);
1053 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1054 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1056 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
1058 int startX
= horizX
;
1059 int endX
= horizX
+ 10;
1061 if (!item
->HasChildren()) endX
+= 20;
1063 dc
.DrawLine( startX
, y
, endX
, y
);
1065 if (item
->HasPlus())
1067 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
1068 dc
.SetPen( *wxGREY_PEN
);
1069 dc
.SetBrush( *wxWHITE_BRUSH
);
1070 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
1071 dc
.SetPen( *wxBLACK_PEN
);
1072 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
1074 if (!item
->IsExpanded())
1075 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
1078 if (item
->HasHilight())
1080 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1082 dc
.SetBrush( *m_hilightBrush
);
1085 dc
.SetPen( *wxBLACK_PEN
);
1087 dc
.SetPen( *wxTRANSPARENT_PEN
);
1089 PaintItem(item
, dc
);
1091 dc
.SetPen( *wxBLACK_PEN
);
1092 dc
.SetTextForeground( *wxBLACK
);
1093 dc
.SetBrush( *wxWHITE_BRUSH
);
1097 dc
.SetBrush( *wxWHITE_BRUSH
);
1098 dc
.SetPen( *wxTRANSPARENT_PEN
);
1100 PaintItem(item
, dc
);
1102 dc
.SetPen( *wxBLACK_PEN
);
1106 if ( item
->IsExpanded() )
1110 wxArrayTreeItems
& children
= item
->GetChildren();
1111 size_t count
= children
.Count();
1112 for ( size_t n
= 0; n
< count
; n
++ )
1117 PaintLevel( children
[n
], dc
, level
+1, y
);
1120 // it may happen that the item is expanded but has no items (when you
1121 // delete all its children for example) - don't draw the vertical line
1124 dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1128 // -----------------------------------------------------------------------------
1129 // wxWindows callbacks
1130 // -----------------------------------------------------------------------------
1132 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1140 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1142 dc
.SetPen( m_dottedPen
);
1143 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1145 int y
= m_lineHeight
/ 2 + 2;
1146 PaintLevel( m_anchor
, dc
, 0, y
);
1149 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1153 RefreshLine( m_current
);
1156 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1160 RefreshLine( m_current
);
1163 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1165 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1166 te
.m_code
= event
.KeyCode();
1167 te
.SetEventObject( this );
1168 GetEventHandler()->ProcessEvent( te
);
1176 switch (event
.KeyCode())
1180 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1188 if (IsExpanded(m_current
))
1190 Collapse(m_current
);
1202 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1203 event
.m_item
= m_current
;
1205 event
.SetEventObject( this );
1206 GetEventHandler()->ProcessEvent( event
);
1213 wxTreeItemId prev
= GetPrevSibling( m_current
);
1217 EnsureVisible( prev
);
1221 prev
= GetParent( m_current
);
1224 EnsureVisible( prev
);
1232 // this works the same as the down arrow except that we also expand the
1233 // item if it wasn't expanded yet
1239 if (IsExpanded(m_current
))
1242 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1243 SelectItem( child
);
1244 EnsureVisible( child
);
1248 wxTreeItemId next
= GetNextSibling( m_current
);
1251 wxTreeItemId current
= m_current
;
1252 while (current
&& !next
)
1254 current
= GetParent( current
);
1255 if (current
) next
= GetNextSibling( current
);
1261 EnsureVisible( next
);
1272 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& WXUNUSED(flags
))
1274 bool onButton
= FALSE
;
1275 return m_anchor
->HitTest( point
, onButton
);
1278 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1280 if ( !(event
.LeftDown() || event
.LeftDClick()) )
1286 wxClientDC
dc(this);
1288 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1289 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1291 bool onButton
= FALSE
;
1292 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1296 if (!IsSelected(item
)) SelectItem(item
);
1298 if ( event
.LeftDClick() )
1300 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1301 event
.m_item
= item
;
1303 event
.SetEventObject( this );
1304 GetEventHandler()->ProcessEvent( event
);
1313 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1315 if (!m_dirty
) return;
1319 CalculatePositions();
1321 AdjustMyScrollbars();
1324 // -----------------------------------------------------------------------------
1325 // -----------------------------------------------------------------------------
1326 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
,
1331 int horizX
= level
*m_indent
;
1333 item
->SetX( horizX
+33 );
1334 item
->SetY( y
-m_lineHeight
/3-2 );
1335 item
->SetHeight( m_lineHeight
);
1337 if ( item
->IsExpanded() )
1340 wxArrayTreeItems
& children
= item
->GetChildren();
1341 size_t count
= children
.Count();
1342 for ( size_t n
= 0; n
< count
; n
++ )
1345 CalculateLevel( children
[n
], dc
, level
+1, y
);
1349 void wxTreeCtrl::CalculatePositions()
1354 wxClientDC
dc(this);
1357 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1359 dc
.SetPen( m_dottedPen
);
1360 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1362 int y
= m_lineHeight
/ 2 + 2;
1363 CalculateLevel( m_anchor
, dc
, 0, y
);
1366 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1368 wxClientDC
dc(this);
1373 GetClientSize( &cw
, &ch
);
1376 rect
.x
= dc
.LogicalToDeviceX( 0 );
1378 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1381 Refresh( TRUE
, &rect
);
1383 AdjustMyScrollbars();
1386 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1388 wxClientDC
dc(this);
1392 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1393 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1395 rect
.height
= dc
.GetCharHeight() + 6;
1396 Refresh( TRUE
, &rect
);