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 // FIXME what does this all mean??
235 if ( y
< m_y
) y
= m_y
;
236 int width
= m_x
+ m_width
;
237 if (width
> x
) x
= width
;
241 size_t count
= m_children
.Count();
242 for ( size_t n
= 0; n
< count
; n
++ )
244 m_children
[n
]->GetSize( x
, y
);
249 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
252 if ((point
.y
> m_y
) && (point
.y
< m_y
+ m_height
))
255 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
256 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
257 (IsExpanded() || HasPlus()))
263 if ((point
.x
> m_x
) && (point
.x
< m_x
+m_width
))
273 size_t count
= m_children
.Count();
274 for ( size_t n
= 0; n
< count
; n
++ )
276 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, onButton
);
286 // -----------------------------------------------------------------------------
287 // wxTreeCtrl implementation
288 // -----------------------------------------------------------------------------
290 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
292 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
293 EVT_PAINT (wxTreeCtrl::OnPaint
)
294 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
295 EVT_CHAR (wxTreeCtrl::OnChar
)
296 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
297 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
300 // -----------------------------------------------------------------------------
301 // construction/destruction
302 // -----------------------------------------------------------------------------
303 void wxTreeCtrl::Init()
306 m_anchor
= (wxGenericTreeItem
*) NULL
;
314 m_hilightBrush
= new wxBrush
316 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
321 m_imageListState
= (wxImageList
*) NULL
;
324 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
325 const wxPoint
& pos
, const wxSize
& size
,
326 long style
, const wxString
& name
)
330 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
, name
);
332 SetBackgroundColour( *wxWHITE
);
333 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
338 wxTreeCtrl::~wxTreeCtrl()
340 wxDELETE( m_hilightBrush
);
341 wxDELETE( m_anchor
);
344 // -----------------------------------------------------------------------------
346 // -----------------------------------------------------------------------------
348 size_t wxTreeCtrl::GetCount() const
350 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
353 void wxTreeCtrl::SetIndent(unsigned int indent
)
359 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
361 wxCHECK_MSG( item
.IsOk(), 0u, "invalid tree item" );
363 return item
.m_pItem
->GetChildrenCount(recursively
);
366 // -----------------------------------------------------------------------------
367 // functions to work with tree items
368 // -----------------------------------------------------------------------------
370 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
372 wxCHECK_MSG( item
.IsOk(), "", "invalid tree item" );
374 return item
.m_pItem
->GetText();
377 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
379 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
381 return item
.m_pItem
->GetImage();
384 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
386 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
388 return item
.m_pItem
->GetSelectedImage();
391 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
393 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
395 return item
.m_pItem
->GetData();
398 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
400 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
403 item
.m_pItem
->SetText(text
, dc
);
406 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
408 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
410 item
.m_pItem
->SetImage(image
);
413 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
415 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
417 item
.m_pItem
->SetSelectedImage(image
);
420 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
422 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
424 item
.m_pItem
->SetData(data
);
427 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
429 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
431 item
.m_pItem
->SetHasPlus(has
);
434 // -----------------------------------------------------------------------------
435 // item status inquiries
436 // -----------------------------------------------------------------------------
438 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
440 wxFAIL_MSG("not implemented");
445 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
447 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
449 return !item
.m_pItem
->GetChildren().IsEmpty();
452 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
454 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
456 return item
.m_pItem
->IsExpanded();
459 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
461 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
463 return item
.m_pItem
->HasHilight();
466 // -----------------------------------------------------------------------------
468 // -----------------------------------------------------------------------------
470 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
472 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
474 return item
.m_pItem
->GetParent();
477 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
479 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
482 return GetNextChild(item
, cookie
);
485 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
487 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
489 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
490 if ( (size_t)cookie
< children
.Count() )
492 return item
.m_pItem
->GetChildren().Item(cookie
++);
496 // there are no more of them
501 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
503 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
505 wxGenericTreeItem
*i
= item
.m_pItem
;
506 wxGenericTreeItem
*parent
= i
->GetParent();
507 if ( parent
== NULL
)
509 // root item doesn't have any siblings
513 wxArrayTreeItems
& siblings
= parent
->GetChildren();
514 int index
= siblings
.Index(i
);
515 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
517 size_t n
= (size_t)(index
+ 1);
518 return n
== siblings
.Count() ? (wxGenericTreeItem
*)NULL
: siblings
[n
];
521 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
523 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
525 wxGenericTreeItem
*i
= item
.m_pItem
;
526 wxGenericTreeItem
*parent
= i
->GetParent();
527 if ( parent
== NULL
)
529 // root item doesn't have any siblings
533 wxArrayTreeItems
& siblings
= parent
->GetChildren();
534 int index
= siblings
.Index(i
);
535 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
537 return index
== 0 ? (wxGenericTreeItem
*)NULL
: siblings
[(size_t)(index
- 1)];
540 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
542 wxFAIL_MSG("not implemented");
547 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
549 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
551 wxFAIL_MSG("not implemented");
556 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
558 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
560 wxFAIL_MSG("not implemented");
565 // -----------------------------------------------------------------------------
567 // -----------------------------------------------------------------------------
569 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
571 const wxString
& text
,
572 int image
, int selImage
,
573 wxTreeItemData
*data
)
575 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
578 // should we give a warning here?
579 return AddRoot(text
, image
, selImage
, data
);
583 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
590 data
->m_pItem
= item
;
593 parent
->Insert( item
, previous
);
595 CalculatePositions();
598 GetClientSize( &cw
, &ch
);
603 rect
.x
= dc
.LogicalToDeviceX( 0 );
605 rect
.width
= 10000; // @@@ not very elegant...
610 rect
.y
= dc
.LogicalToDeviceY( parent
->GetChildren().Item(previous
)->GetY() );
612 else // it's the 1st child
614 rect
.y
= dc
.LogicalToDeviceY( parent
->GetY() );
617 AdjustMyScrollbars();
619 if ( rect
.height
> 0 )
620 Refresh( FALSE
, &rect
);
625 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
626 int image
, int selImage
,
627 wxTreeItemData
*data
)
629 wxCHECK_MSG( !m_anchor
, NULL
, "tree can have only one root" );
632 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
633 image
, selImage
, data
);
636 data
->m_pItem
= m_anchor
;
639 AdjustMyScrollbars();
645 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
646 const wxString
& text
,
647 int image
, int selImage
,
648 wxTreeItemData
*data
)
650 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
653 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
654 const wxTreeItemId
& idPrevious
,
655 const wxString
& text
,
656 int image
, int selImage
,
657 wxTreeItemData
*data
)
659 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
662 // should we give a warning here?
663 return AddRoot(text
, image
, selImage
, data
);
666 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
667 wxASSERT_MSG( index
!= NOT_FOUND
,
668 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
669 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
672 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
673 const wxString
& text
,
674 int image
, int selImage
,
675 wxTreeItemData
*data
)
677 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
680 // should we give a warning here?
681 return AddRoot(text
, image
, selImage
, data
);
684 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
685 image
, selImage
, data
);
688 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
690 wxGenericTreeItem
*item
= itemId
.m_pItem
;
691 wxGenericTreeItem
*parent
= item
->GetParent();
695 parent
->GetChildren().Remove(item
);
703 void wxTreeCtrl::DeleteAllItems()
714 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
716 wxGenericTreeItem
*item
= itemId
.m_pItem
;
718 if ( item
->IsExpanded() )
721 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
723 event
.SetEventObject( this );
724 if ( ProcessEvent( event
) && event
.m_code
)
726 // cancelled by program
732 RefreshSubtree(item
);
734 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
735 ProcessEvent( event
);
738 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
740 wxGenericTreeItem
*item
= itemId
.m_pItem
;
742 if ( !item
->IsExpanded() )
745 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
747 event
.SetEventObject( this );
748 if ( ProcessEvent( event
) && event
.m_code
)
750 // cancelled by program
756 wxArrayTreeItems
& children
= item
->GetChildren();
757 size_t count
= children
.Count();
758 for ( size_t n
= 0; n
< count
; n
++ )
760 Collapse(children
[n
]);
763 CalculatePositions();
765 RefreshSubtree(item
);
767 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
768 ProcessEvent( event
);
771 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
777 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
779 wxGenericTreeItem
*item
= itemId
.m_pItem
;
781 if ( item
->IsExpanded() )
787 void wxTreeCtrl::Unselect()
791 m_current
->SetHilight( FALSE
);
792 RefreshLine( m_current
);
796 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
798 wxGenericTreeItem
*item
= itemId
.m_pItem
;
800 if ( m_current
!= item
)
802 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
804 event
.m_itemOld
= m_current
;
805 event
.SetEventObject( this );
806 if ( ProcessEvent( event
) && event
.WasVetoed() )
811 m_current
->SetHilight( FALSE
);
812 RefreshLine( m_current
);
816 m_current
->SetHilight( TRUE
);
817 RefreshLine( m_current
);
819 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
820 ProcessEvent( event
);
824 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& WXUNUSED(item
))
826 wxFAIL_MSG("not implemented");
829 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
831 wxFAIL_MSG("not implemented");
834 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
835 wxClassInfo
* WXUNUSED(textCtrlClass
) )
837 wxFAIL_MSG("not implemented");
842 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
844 wxFAIL_MSG("not implemented");
849 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
851 wxFAIL_MSG("not implemented");
854 void wxTreeCtrl::SortChildren( const wxTreeItemId
& WXUNUSED(item
),
855 wxTreeItemCmpFunc
*WXUNUSED(cmpFunction
))
857 wxFAIL_MSG("not implemented");
860 // -----------------------------------------------------------------------------
861 // images are not currently supported, but we still provide stubs for these
863 // -----------------------------------------------------------------------------
864 wxImageList
*wxTreeCtrl::GetImageList() const
866 return m_imageListNormal
;
869 wxImageList
*wxTreeCtrl::GetStateImageList() const
871 return m_imageListState
;
874 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
876 m_imageListNormal
= imageList
;
879 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
881 m_imageListState
= imageList
;
884 // -----------------------------------------------------------------------------
886 // -----------------------------------------------------------------------------
887 void wxTreeCtrl::AdjustMyScrollbars()
893 m_anchor
->GetSize( x
, y
);
895 int x_pos
= GetScrollPos( wxHORIZONTAL
);
896 int y_pos
= GetScrollPos( wxVERTICAL
);
897 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
901 SetScrollbars( 0, 0, 0, 0 );
905 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
907 int horizX
= level
*m_indent
;
909 item
->SetX( horizX
+33 );
910 item
->SetY( y
-m_lineHeight
/3 );
911 item
->SetHeight( m_lineHeight
);
913 item
->SetCross( horizX
+15, y
);
917 int exposed_x
= dc
.LogicalToDeviceX( 0 );
918 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
920 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
923 int endX
= horizX
+ 10;
925 if (!item
->HasChildren()) endX
+= 20;
927 dc
.DrawLine( startX
, y
, endX
, y
);
931 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
932 dc
.SetPen( *wxGREY_PEN
);
933 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
934 dc
.SetPen( *wxBLACK_PEN
);
935 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
937 if (!item
->IsExpanded())
938 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
941 if (item
->HasHilight())
943 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
944 dc
.SetBrush( *m_hilightBrush
);
948 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
952 if (item
->GetImage() != -1)
954 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
959 dc
.SetPen( *wxBLACK_PEN
);
961 dc
.SetPen( *wxTRANSPARENT_PEN
);
963 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
965 if (item
->GetImage() != -1)
967 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
968 m_imageListNormal
->Draw( item
->GetImage(), dc
, item
->GetX(), item
->GetY()-1, wxIMAGELIST_DRAW_TRANSPARENT
);
969 dc
.DestroyClippingRegion();
971 dc
.DrawText( item
->GetText(), image_w
+item
->GetX(), item
->GetY() );
973 dc
.SetPen( *wxBLACK_PEN
);
974 dc
.SetTextForeground( *wxBLACK
);
975 dc
.SetBrush( *wxWHITE_BRUSH
);
979 dc
.SetBrush( *wxWHITE_BRUSH
);
980 dc
.SetPen( *wxTRANSPARENT_PEN
);
984 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
988 if (item
->GetImage() != -1)
990 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
994 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
996 if (item
->GetImage() != -1)
998 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
999 m_imageListNormal
->Draw( item
->GetImage(), dc
, item
->GetX(), item
->GetY()-1, wxIMAGELIST_DRAW_TRANSPARENT
);
1000 dc
.DestroyClippingRegion();
1003 dc
.DrawText( item
->GetText(), image_w
+item
->GetX(), item
->GetY() );
1004 dc
.SetPen( *wxBLACK_PEN
);
1008 if ( !item
->IsExpanded() )
1013 wxArrayTreeItems
& children
= item
->GetChildren();
1014 size_t count
= children
.Count();
1015 for ( size_t n
= 0; n
< count
; n
++ )
1020 PaintLevel( children
[n
], dc
, level
+1, y
);
1023 dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1026 // -----------------------------------------------------------------------------
1027 // wxWindows callbacks
1028 // -----------------------------------------------------------------------------
1030 void wxTreeCtrl::OnPaint( const wxPaintEvent
&WXUNUSED(event
) )
1038 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1040 dc
.SetPen( m_dottedPen
);
1041 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1043 int y
= m_lineHeight
/ 2 + 2;
1044 PaintLevel( m_anchor
, dc
, 0, y
);
1047 void wxTreeCtrl::OnSetFocus( const wxFocusEvent
&WXUNUSED(event
) )
1051 RefreshLine( m_current
);
1054 void wxTreeCtrl::OnKillFocus( const wxFocusEvent
&WXUNUSED(event
) )
1058 RefreshLine( m_current
);
1061 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1063 // TODO process '+', '-' (expand/collapse branch) and cursor keys
1067 void wxTreeCtrl::OnMouse( const wxMouseEvent
&event
)
1069 if ( !(event
.LeftDown() || event
.LeftDClick()) )
1075 wxClientDC
dc(this);
1077 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1078 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1080 bool onButton
= FALSE
;
1081 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1087 if ( event
.LeftDClick() )
1089 wxTreeEvent
event( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1090 event
.m_item
= item
;
1092 event
.SetEventObject( this );
1093 ProcessEvent( event
);
1102 // -----------------------------------------------------------------------------
1103 // -----------------------------------------------------------------------------
1104 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
,
1109 int horizX
= level
*m_indent
;
1111 item
->SetX( horizX
+33 );
1112 item
->SetY( y
-m_lineHeight
/3-2 );
1113 item
->SetHeight( m_lineHeight
);
1115 if ( item
->IsExpanded() )
1118 wxArrayTreeItems
& children
= item
->GetChildren();
1119 size_t count
= children
.Count();
1120 for ( size_t n
= 0; n
< count
; n
++ )
1123 CalculateLevel( children
[n
], dc
, level
+1, y
);
1127 void wxTreeCtrl::CalculatePositions()
1132 wxClientDC
dc(this);
1135 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1137 dc
.SetPen( m_dottedPen
);
1138 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1140 int y
= m_lineHeight
/ 2 + 2;
1141 CalculateLevel( m_anchor
, dc
, 0, y
);
1144 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1146 wxClientDC
dc(this);
1151 GetClientSize( &cw
, &ch
);
1154 rect
.x
= dc
.LogicalToDeviceX( 0 );
1156 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1159 Refresh( TRUE
, &rect
);
1161 AdjustMyScrollbars();
1164 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1166 wxClientDC
dc(this);
1170 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1171 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1173 rect
.height
= dc
.GetCharHeight() + 6;
1175 Refresh( TRUE
, &rect
);