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 int GetX() const { return m_x
; }
72 int GetY() const { return m_y
; }
74 void SetHeight(int h
) { m_height
= h
; }
76 void SetX(int x
) { m_x
= x
; }
77 void SetY(int y
) { m_y
= y
; }
79 wxGenericTreeItem
*GetParent() const { return m_parent
; }
84 // get count of all children (and grand children if 'recursively')
85 size_t GetChildrenCount(bool recursively
= TRUE
) const;
87 void Insert(wxGenericTreeItem
*child
, size_t index
)
88 { m_children
.Insert(child
, index
); }
90 void SetCross( int x
, int y
);
91 void GetSize( int &x
, int &y
);
93 // return the item at given position (or NULL if no item), onButton is TRUE
94 // if the point belongs to the item's button, otherwise it lies on the
96 wxGenericTreeItem
*HitTest( const wxPoint
& point
, bool &onButton
);
98 void Expand() { m_isCollapsed
= FALSE
; }
99 void Collapse() { m_isCollapsed
= TRUE
; }
101 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
104 bool HasChildren() const { return !m_children
.IsEmpty(); }
105 bool HasHilight() const { return m_hasHilight
; }
106 bool IsExpanded() const { return !m_isCollapsed
; }
107 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
115 wxTreeItemData
*m_data
;
117 // @@ probably should use bitfields to save size
119 m_hasHilight
, // same as focused
120 m_hasPlus
; // used for item which doesn't have
121 // children but still has a [+] button
124 long m_height
, m_width
;
125 int m_xCross
, m_yCross
;
127 wxArrayTreeItems m_children
;
128 wxGenericTreeItem
*m_parent
;
131 // =============================================================================
133 // =============================================================================
135 // -----------------------------------------------------------------------------
137 // -----------------------------------------------------------------------------
139 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxCommandEvent
)
141 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
142 : wxCommandEvent( commandType
, id
)
145 m_itemOld
= (wxGenericTreeItem
*)NULL
;
148 // -----------------------------------------------------------------------------
150 // -----------------------------------------------------------------------------
152 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
153 const wxString
& text
,
155 int image
, int selImage
,
156 wxTreeItemData
*data
)
160 m_selImage
= selImage
;
163 m_xCross
= m_yCross
= 0;
167 m_isCollapsed
= TRUE
;
168 m_hasHilight
= FALSE
;
173 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
176 wxGenericTreeItem::~wxGenericTreeItem()
180 size_t count
= m_children
.Count();
181 for ( size_t n
= 0; n
< count
; n
++ )
182 delete m_children
[n
];
185 void wxGenericTreeItem::SetText( const wxString
&text
, wxDC
& dc
)
189 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
192 void wxGenericTreeItem::Reset()
199 m_height
= m_width
= 0;
206 m_isCollapsed
= TRUE
;
208 m_parent
= (wxGenericTreeItem
*)NULL
;
211 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
213 size_t count
= m_children
.Count();
217 size_t total
= count
;
218 for ( size_t n
= 0; n
< count
; n
++ )
220 total
+= m_children
[n
]->GetChildrenCount();
226 void wxGenericTreeItem::SetCross( int x
, int y
)
232 void wxGenericTreeItem::GetSize( int &x
, int &y
)
234 if ( y
< m_y
) y
= m_y
;
235 int width
= m_x
+ m_width
;
236 if (width
> x
) x
= width
;
240 size_t count
= m_children
.Count();
241 for ( size_t n
= 0; n
< count
; n
++ )
243 m_children
[n
]->GetSize( x
, y
);
248 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
251 if ((point
.y
> m_y
) && (point
.y
< m_y
+ m_height
))
254 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
255 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
256 (IsExpanded() || HasPlus()))
262 if ((point
.x
> m_x
) && (point
.x
< m_x
+m_width
))
272 size_t count
= m_children
.Count();
273 for ( size_t n
= 0; n
< count
; n
++ )
275 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, onButton
);
285 // -----------------------------------------------------------------------------
286 // wxTreeCtrl implementation
287 // -----------------------------------------------------------------------------
289 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
291 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
292 EVT_PAINT (wxTreeCtrl::OnPaint
)
293 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
294 EVT_CHAR (wxTreeCtrl::OnChar
)
295 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
296 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
297 EVT_IDLE (wxTreeCtrl::OnIdle
)
300 // -----------------------------------------------------------------------------
301 // construction/destruction
302 // -----------------------------------------------------------------------------
303 void wxTreeCtrl::Init()
306 m_anchor
= (wxGenericTreeItem
*) NULL
;
315 m_hilightBrush
= new wxBrush
317 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
322 m_imageListState
= (wxImageList
*) NULL
;
325 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
326 const wxPoint
& pos
, const wxSize
& size
,
327 long style
, const wxString
& name
)
331 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
333 SetBackgroundColour( *wxWHITE
);
334 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
339 wxTreeCtrl::~wxTreeCtrl()
341 wxDELETE( m_hilightBrush
);
342 wxDELETE( m_anchor
);
345 // -----------------------------------------------------------------------------
347 // -----------------------------------------------------------------------------
349 size_t wxTreeCtrl::GetCount() const
351 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
354 void wxTreeCtrl::SetIndent(unsigned int indent
)
360 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
362 wxCHECK_MSG( item
.IsOk(), 0u, "invalid tree item" );
364 return item
.m_pItem
->GetChildrenCount(recursively
);
367 // -----------------------------------------------------------------------------
368 // functions to work with tree items
369 // -----------------------------------------------------------------------------
371 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
373 wxCHECK_MSG( item
.IsOk(), "", "invalid tree item" );
375 return item
.m_pItem
->GetText();
378 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
380 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
382 return item
.m_pItem
->GetImage();
385 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
387 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
389 return item
.m_pItem
->GetSelectedImage();
392 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
394 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
396 return item
.m_pItem
->GetData();
399 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
401 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
404 item
.m_pItem
->SetText(text
, dc
);
407 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
409 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
411 item
.m_pItem
->SetImage(image
);
414 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
416 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
418 item
.m_pItem
->SetSelectedImage(image
);
421 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
423 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
425 item
.m_pItem
->SetData(data
);
428 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
430 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
432 item
.m_pItem
->SetHasPlus(has
);
435 // -----------------------------------------------------------------------------
436 // item status inquiries
437 // -----------------------------------------------------------------------------
439 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
441 wxFAIL_MSG("not implemented");
446 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
448 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
450 return !item
.m_pItem
->GetChildren().IsEmpty();
453 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
455 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
457 return item
.m_pItem
->IsExpanded();
460 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
462 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
464 return item
.m_pItem
->HasHilight();
467 // -----------------------------------------------------------------------------
469 // -----------------------------------------------------------------------------
471 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
473 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
475 return item
.m_pItem
->GetParent();
478 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
480 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
483 return GetNextChild(item
, cookie
);
486 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
488 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
490 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
491 if ( (size_t)cookie
< children
.Count() )
493 return item
.m_pItem
->GetChildren().Item(cookie
++);
497 // there are no more of them
502 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
504 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
506 wxGenericTreeItem
*i
= item
.m_pItem
;
507 wxGenericTreeItem
*parent
= i
->GetParent();
508 if ( parent
== NULL
)
510 // root item doesn't have any siblings
514 wxArrayTreeItems
& siblings
= parent
->GetChildren();
515 int index
= siblings
.Index(i
);
516 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
518 size_t n
= (size_t)(index
+ 1);
519 return n
== siblings
.Count() ? (wxGenericTreeItem
*)NULL
: siblings
[n
];
522 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
524 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
526 wxGenericTreeItem
*i
= item
.m_pItem
;
527 wxGenericTreeItem
*parent
= i
->GetParent();
528 if ( parent
== NULL
)
530 // root item doesn't have any siblings
534 wxArrayTreeItems
& siblings
= parent
->GetChildren();
535 int index
= siblings
.Index(i
);
536 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
538 return index
== 0 ? (wxGenericTreeItem
*)NULL
: siblings
[(size_t)(index
- 1)];
541 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
543 wxFAIL_MSG("not implemented");
548 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
550 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
552 wxFAIL_MSG("not implemented");
557 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
559 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
561 wxFAIL_MSG("not implemented");
566 // -----------------------------------------------------------------------------
568 // -----------------------------------------------------------------------------
570 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
572 const wxString
& text
,
573 int image
, int selImage
,
574 wxTreeItemData
*data
)
576 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
579 // should we give a warning here?
580 return AddRoot(text
, image
, selImage
, data
);
584 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
591 data
->m_pItem
= item
;
594 parent
->Insert( item
, previous
);
601 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
602 int image
, int selImage
,
603 wxTreeItemData
*data
)
605 wxCHECK_MSG( !m_anchor
, NULL
, "tree can have only one root" );
608 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
609 image
, selImage
, data
);
612 data
->m_pItem
= m_anchor
;
615 AdjustMyScrollbars();
621 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
622 const wxString
& text
,
623 int image
, int selImage
,
624 wxTreeItemData
*data
)
626 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
629 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
630 const wxTreeItemId
& idPrevious
,
631 const wxString
& text
,
632 int image
, int selImage
,
633 wxTreeItemData
*data
)
635 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
638 // should we give a warning here?
639 return AddRoot(text
, image
, selImage
, data
);
642 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
643 wxASSERT_MSG( index
!= NOT_FOUND
,
644 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
645 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
648 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
649 const wxString
& text
,
650 int image
, int selImage
,
651 wxTreeItemData
*data
)
653 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
656 // should we give a warning here?
657 return AddRoot(text
, image
, selImage
, data
);
660 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
661 image
, selImage
, data
);
664 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
666 wxGenericTreeItem
*item
= itemId
.m_pItem
;
667 wxGenericTreeItem
*parent
= item
->GetParent();
671 parent
->GetChildren().Remove(item
);
679 void wxTreeCtrl::DeleteAllItems()
690 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
692 wxGenericTreeItem
*item
= itemId
.m_pItem
;
694 if ( item
->IsExpanded() )
697 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
699 event
.SetEventObject( this );
700 if ( ProcessEvent( event
) && event
.m_code
)
702 // cancelled by program
708 RefreshSubtree(item
);
710 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
711 ProcessEvent( event
);
714 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
716 wxGenericTreeItem
*item
= itemId
.m_pItem
;
718 if ( !item
->IsExpanded() )
721 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
723 event
.SetEventObject( this );
724 if ( ProcessEvent( event
) && event
.m_code
)
726 // cancelled by program
732 wxArrayTreeItems
& children
= item
->GetChildren();
733 size_t count
= children
.Count();
734 for ( size_t n
= 0; n
< count
; n
++ )
736 Collapse(children
[n
]);
739 CalculatePositions();
741 RefreshSubtree(item
);
743 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
744 ProcessEvent( event
);
747 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
753 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
755 wxGenericTreeItem
*item
= itemId
.m_pItem
;
757 if ( item
->IsExpanded() )
763 void wxTreeCtrl::Unselect()
767 m_current
->SetHilight( FALSE
);
768 RefreshLine( m_current
);
772 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
774 wxGenericTreeItem
*item
= itemId
.m_pItem
;
776 if ( m_current
!= item
)
778 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
780 event
.m_itemOld
= m_current
;
781 event
.SetEventObject( this );
782 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
787 m_current
->SetHilight( FALSE
);
788 RefreshLine( m_current
);
792 m_current
->SetHilight( TRUE
);
793 RefreshLine( m_current
);
795 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
796 GetEventHandler()->ProcessEvent( event
);
800 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
802 wxGenericTreeItem
*gitem
= item
.m_pItem
;
804 int item_y
= gitem
->GetY();
808 ViewStart( &start_x
, &start_y
);
811 if (item_y
< start_y
+3)
815 m_anchor
->GetSize( x
, y
);
817 int x_pos
= GetScrollPos( wxHORIZONTAL
);
818 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, item_y
/10 );
824 GetClientSize( &w
, &h
);
826 if (item_y
> start_y
+h
-26)
830 m_anchor
->GetSize( x
, y
);
832 int x_pos
= GetScrollPos( wxHORIZONTAL
);
833 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-h
+30)/10 );
838 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
840 wxFAIL_MSG("not implemented");
843 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
844 wxClassInfo
* WXUNUSED(textCtrlClass
) )
846 wxFAIL_MSG("not implemented");
851 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
853 wxFAIL_MSG("not implemented");
858 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
860 wxFAIL_MSG("not implemented");
863 void wxTreeCtrl::SortChildren( const wxTreeItemId
& WXUNUSED(item
),
864 wxTreeItemCmpFunc
*WXUNUSED(cmpFunction
))
866 wxFAIL_MSG("not implemented");
869 wxImageList
*wxTreeCtrl::GetImageList() const
871 return m_imageListNormal
;
874 wxImageList
*wxTreeCtrl::GetStateImageList() const
876 return m_imageListState
;
879 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
881 m_imageListNormal
= imageList
;
884 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
886 m_imageListState
= imageList
;
889 // -----------------------------------------------------------------------------
891 // -----------------------------------------------------------------------------
892 void wxTreeCtrl::AdjustMyScrollbars()
898 m_anchor
->GetSize( x
, y
);
900 int x_pos
= GetScrollPos( wxHORIZONTAL
);
901 int y_pos
= GetScrollPos( wxVERTICAL
);
902 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
906 SetScrollbars( 0, 0, 0, 0 );
910 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
912 int horizX
= level
*m_indent
;
914 item
->SetX( horizX
+33 );
915 item
->SetY( y
-m_lineHeight
/3 );
916 item
->SetHeight( m_lineHeight
);
918 item
->SetCross( horizX
+15, y
);
922 int exposed_x
= dc
.LogicalToDeviceX( 0 );
923 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
925 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
928 int endX
= horizX
+ 10;
930 if (!item
->HasChildren()) endX
+= 20;
932 dc
.DrawLine( startX
, y
, endX
, y
);
936 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
937 dc
.SetPen( *wxGREY_PEN
);
938 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
939 dc
.SetPen( *wxBLACK_PEN
);
940 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
942 if (!item
->IsExpanded())
943 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
946 if (item
->HasHilight())
948 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
950 dc
.SetBrush( *m_hilightBrush
);
954 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
958 if (item
->GetImage() != -1)
960 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
965 dc
.SetPen( *wxBLACK_PEN
);
967 dc
.SetPen( *wxTRANSPARENT_PEN
);
969 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
971 if (item
->GetImage() != -1)
973 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
974 m_imageListNormal
->Draw( item
->GetImage(), dc
, item
->GetX(), item
->GetY()-1, wxIMAGELIST_DRAW_TRANSPARENT
);
975 dc
.DestroyClippingRegion();
977 dc
.DrawText( item
->GetText(), image_w
+item
->GetX(), item
->GetY() );
979 dc
.SetPen( *wxBLACK_PEN
);
980 dc
.SetTextForeground( *wxBLACK
);
981 dc
.SetBrush( *wxWHITE_BRUSH
);
985 dc
.SetBrush( *wxWHITE_BRUSH
);
986 dc
.SetPen( *wxTRANSPARENT_PEN
);
990 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
994 if (item
->GetImage() != -1)
996 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1000 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
1002 if (item
->GetImage() != -1)
1004 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1005 m_imageListNormal
->Draw( item
->GetImage(), dc
, item
->GetX(), item
->GetY()-1, wxIMAGELIST_DRAW_TRANSPARENT
);
1006 dc
.DestroyClippingRegion();
1009 dc
.DrawText( item
->GetText(), image_w
+item
->GetX(), item
->GetY() );
1010 dc
.SetPen( *wxBLACK_PEN
);
1014 if ( !item
->IsExpanded() )
1019 wxArrayTreeItems
& children
= item
->GetChildren();
1020 size_t count
= children
.Count();
1021 for ( size_t n
= 0; n
< count
; n
++ )
1026 PaintLevel( children
[n
], dc
, level
+1, y
);
1029 dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1032 // -----------------------------------------------------------------------------
1033 // wxWindows callbacks
1034 // -----------------------------------------------------------------------------
1036 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1044 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1046 dc
.SetPen( m_dottedPen
);
1047 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1049 int y
= m_lineHeight
/ 2 + 2;
1050 PaintLevel( m_anchor
, dc
, 0, y
);
1053 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1057 RefreshLine( m_current
);
1060 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1064 RefreshLine( m_current
);
1067 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1075 switch (event
.KeyCode())
1080 if (HasChildren(m_current
) && !IsExpanded(m_current
))
1089 if (IsExpanded(m_current
))
1091 Collapse(m_current
);
1098 wxTreeEvent
event( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1099 event
.m_item
= m_current
;
1101 event
.SetEventObject( this );
1102 GetEventHandler()->ProcessEvent( event
);
1107 wxTreeItemId prev
= GetPrevSibling( m_current
);
1111 EnsureVisible( prev
);
1115 prev
= GetParent( m_current
);
1118 EnsureVisible( prev
);
1126 if (IsExpanded(m_current
))
1129 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1130 SelectItem( child
);
1131 EnsureVisible( child
);
1135 wxTreeItemId next
= GetNextSibling( m_current
);
1138 wxTreeItemId current
= m_current
;
1139 while (current
&& !next
)
1141 current
= GetParent( current
);
1142 if (current
) next
= GetNextSibling( current
);
1148 EnsureVisible( next
);
1157 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1159 if ( !(event
.LeftDown() || event
.LeftDClick()) )
1165 wxClientDC
dc(this);
1167 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1168 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1170 bool onButton
= FALSE
;
1171 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1175 if (!IsSelected(item
)) SelectItem(item
);
1177 if ( event
.LeftDClick() )
1179 wxTreeEvent
event( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1180 event
.m_item
= item
;
1182 event
.SetEventObject( this );
1183 GetEventHandler()->ProcessEvent( event
);
1192 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1194 if (!m_dirty
) return;
1198 CalculatePositions();
1200 AdjustMyScrollbars();
1203 // -----------------------------------------------------------------------------
1204 // -----------------------------------------------------------------------------
1205 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
,
1210 int horizX
= level
*m_indent
;
1212 item
->SetX( horizX
+33 );
1213 item
->SetY( y
-m_lineHeight
/3-2 );
1214 item
->SetHeight( m_lineHeight
);
1216 if ( item
->IsExpanded() )
1219 wxArrayTreeItems
& children
= item
->GetChildren();
1220 size_t count
= children
.Count();
1221 for ( size_t n
= 0; n
< count
; n
++ )
1224 CalculateLevel( children
[n
], dc
, level
+1, y
);
1228 void wxTreeCtrl::CalculatePositions()
1233 wxClientDC
dc(this);
1236 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1238 dc
.SetPen( m_dottedPen
);
1239 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1241 int y
= m_lineHeight
/ 2 + 2;
1242 CalculateLevel( m_anchor
, dc
, 0, y
);
1245 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1247 wxClientDC
dc(this);
1252 GetClientSize( &cw
, &ch
);
1255 rect
.x
= dc
.LogicalToDeviceX( 0 );
1257 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1260 Refresh( TRUE
, &rect
);
1262 AdjustMyScrollbars();
1265 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1267 wxClientDC
dc(this);
1271 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1272 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1274 rect
.height
= dc
.GetCharHeight() + 6;
1275 Refresh( TRUE
, &rect
);