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
);
54 inline ~wxGenericTreeItem();
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
;
697 void wxTreeCtrl::DeleteAllItems()
708 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
710 wxGenericTreeItem
*item
= itemId
.m_pItem
;
712 if ( item
->IsExpanded() )
715 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
717 event
.SetEventObject( this );
718 if ( ProcessEvent( event
) && event
.m_code
)
720 // cancelled by program
726 RefreshSubtree(item
);
728 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
729 ProcessEvent( event
);
732 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
734 wxGenericTreeItem
*item
= itemId
.m_pItem
;
736 if ( !item
->IsExpanded() )
739 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
741 event
.SetEventObject( this );
742 if ( ProcessEvent( event
) && event
.m_code
)
744 // cancelled by program
750 wxArrayTreeItems
& children
= item
->GetChildren();
751 size_t count
= children
.Count();
752 for ( size_t n
= 0; n
< count
; n
++ )
754 Collapse(children
[n
]);
757 CalculatePositions();
759 RefreshSubtree(item
);
761 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
762 ProcessEvent( event
);
765 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
771 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
773 wxGenericTreeItem
*item
= itemId
.m_pItem
;
775 if ( item
->IsExpanded() )
781 void wxTreeCtrl::Unselect()
785 m_current
->SetHilight( FALSE
);
786 RefreshLine( m_current
);
790 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
792 wxGenericTreeItem
*item
= itemId
.m_pItem
;
794 if ( m_current
!= item
)
796 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
798 event
.m_itemOld
= m_current
;
799 event
.SetEventObject( this );
800 if ( ProcessEvent( event
) && event
.WasVetoed() )
805 m_current
->SetHilight( FALSE
);
806 RefreshLine( m_current
);
810 m_current
->SetHilight( TRUE
);
811 RefreshLine( m_current
);
813 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
814 ProcessEvent( event
);
818 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& WXUNUSED(item
))
820 wxFAIL_MSG("not implemented");
823 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
825 wxFAIL_MSG("not implemented");
828 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
829 wxClassInfo
* WXUNUSED(textCtrlClass
) )
831 wxFAIL_MSG("not implemented");
836 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
838 wxFAIL_MSG("not implemented");
843 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
845 wxFAIL_MSG("not implemented");
848 void wxTreeCtrl::SortChildren( const wxTreeItemId
& WXUNUSED(item
),
849 wxTreeItemCmpFunc
*WXUNUSED(cmpFunction
))
851 wxFAIL_MSG("not implemented");
854 // -----------------------------------------------------------------------------
855 // images are not currently supported, but we still provide stubs for these
857 // -----------------------------------------------------------------------------
858 wxImageList
*wxTreeCtrl::GetImageList() const
860 return m_imageListNormal
;
863 wxImageList
*wxTreeCtrl::GetStateImageList() const
865 return m_imageListState
;
868 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
870 m_imageListNormal
= imageList
;
873 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
875 m_imageListState
= imageList
;
878 // -----------------------------------------------------------------------------
880 // -----------------------------------------------------------------------------
881 void wxTreeCtrl::AdjustMyScrollbars()
887 m_anchor
->GetSize( x
, y
);
889 int x_pos
= GetScrollPos( wxHORIZONTAL
);
890 int y_pos
= GetScrollPos( wxVERTICAL
);
891 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
895 SetScrollbars( 0, 0, 0, 0 );
899 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
901 int horizX
= level
*m_indent
;
903 item
->SetX( horizX
+33 );
904 item
->SetY( y
-m_lineHeight
/3 );
905 item
->SetHeight( m_lineHeight
);
907 item
->SetCross( horizX
+15, y
);
911 int exposed_x
= dc
.LogicalToDeviceX( 0 );
912 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
914 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
917 int endX
= horizX
+ 10;
919 if (!item
->HasChildren()) endX
+= 20;
921 dc
.DrawLine( startX
, y
, endX
, y
);
925 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
926 dc
.SetPen( *wxGREY_PEN
);
927 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
928 dc
.SetPen( *wxBLACK_PEN
);
929 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
931 if (!item
->IsExpanded())
932 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
935 if (item
->HasHilight())
937 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
938 dc
.SetBrush( *m_hilightBrush
);
942 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
946 if (item
->GetImage() != -1)
948 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
953 dc
.SetPen( *wxBLACK_PEN
);
955 dc
.SetPen( *wxTRANSPARENT_PEN
);
957 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
959 if (item
->GetImage() != -1)
961 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
962 m_imageListNormal
->Draw( item
->GetImage(), dc
, item
->GetX(), item
->GetY()-1, wxIMAGELIST_DRAW_TRANSPARENT
);
963 dc
.DestroyClippingRegion();
965 dc
.DrawText( item
->GetText(), image_w
+item
->GetX(), item
->GetY() );
967 dc
.SetPen( *wxBLACK_PEN
);
968 dc
.SetTextForeground( *wxBLACK
);
969 dc
.SetBrush( *wxWHITE_BRUSH
);
973 dc
.SetBrush( *wxWHITE_BRUSH
);
974 dc
.SetPen( *wxTRANSPARENT_PEN
);
978 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
982 if (item
->GetImage() != -1)
984 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
988 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
990 if (item
->GetImage() != -1)
992 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
993 m_imageListNormal
->Draw( item
->GetImage(), dc
, item
->GetX(), item
->GetY()-1, wxIMAGELIST_DRAW_TRANSPARENT
);
994 dc
.DestroyClippingRegion();
997 dc
.DrawText( item
->GetText(), image_w
+item
->GetX(), item
->GetY() );
998 dc
.SetPen( *wxBLACK_PEN
);
1002 if ( !item
->IsExpanded() )
1007 wxArrayTreeItems
& children
= item
->GetChildren();
1008 size_t count
= children
.Count();
1009 for ( size_t n
= 0; n
< count
; n
++ )
1014 PaintLevel( children
[n
], dc
, level
+1, y
);
1017 dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1020 // -----------------------------------------------------------------------------
1021 // wxWindows callbacks
1022 // -----------------------------------------------------------------------------
1024 void wxTreeCtrl::OnPaint( const wxPaintEvent
&WXUNUSED(event
) )
1032 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1034 dc
.SetPen( m_dottedPen
);
1035 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1037 int y
= m_lineHeight
/ 2 + 2;
1038 PaintLevel( m_anchor
, dc
, 0, y
);
1041 void wxTreeCtrl::OnSetFocus( const wxFocusEvent
&WXUNUSED(event
) )
1045 RefreshLine( m_current
);
1048 void wxTreeCtrl::OnKillFocus( const wxFocusEvent
&WXUNUSED(event
) )
1052 RefreshLine( m_current
);
1055 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1057 // TODO process '+', '-' (expand/collapse branch) and cursor keys
1061 void wxTreeCtrl::OnMouse( const wxMouseEvent
&event
)
1063 if ( !(event
.LeftDown() || event
.LeftDClick()) )
1069 wxClientDC
dc(this);
1071 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1072 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1074 bool onButton
= FALSE
;
1075 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1081 if ( event
.LeftDClick() )
1083 wxTreeEvent
event( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1084 event
.m_item
= item
;
1086 event
.SetEventObject( this );
1087 ProcessEvent( event
);
1096 // -----------------------------------------------------------------------------
1097 // -----------------------------------------------------------------------------
1098 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
,
1103 int horizX
= level
*m_indent
;
1105 item
->SetX( horizX
+33 );
1106 item
->SetY( y
-m_lineHeight
/3-2 );
1107 item
->SetHeight( m_lineHeight
);
1109 if ( item
->IsExpanded() )
1112 wxArrayTreeItems
& children
= item
->GetChildren();
1113 size_t count
= children
.Count();
1114 for ( size_t n
= 0; n
< count
; n
++ )
1117 CalculateLevel( children
[n
], dc
, level
+1, y
);
1121 void wxTreeCtrl::CalculatePositions()
1126 wxClientDC
dc(this);
1129 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1131 dc
.SetPen( m_dottedPen
);
1132 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1134 int y
= m_lineHeight
/ 2 + 2;
1135 CalculateLevel( m_anchor
, dc
, 0, y
);
1138 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1140 wxClientDC
dc(this);
1145 GetClientSize( &cw
, &ch
);
1148 rect
.x
= dc
.LogicalToDeviceX( 0 );
1150 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1153 Refresh( TRUE
, &rect
);
1155 AdjustMyScrollbars();
1158 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1160 wxClientDC
dc(this);
1164 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1165 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1167 rect
.height
= dc
.GetCharHeight() + 6;
1169 Refresh( TRUE
, &rect
);