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"
31 // -----------------------------------------------------------------------------
33 // -----------------------------------------------------------------------------
35 WX_DEFINE_ARRAY(wxGenericTreeItem
*, wxArrayTreeItems
);
37 // -----------------------------------------------------------------------------
39 // -----------------------------------------------------------------------------
42 class WXDLLEXPORT wxGenericTreeItem
46 wxGenericTreeItem() { m_data
= NULL
; }
47 wxGenericTreeItem( wxGenericTreeItem
*parent
,
50 int image
, int selImage
,
51 wxTreeItemData
*data
);
53 inline ~wxGenericTreeItem();
56 wxArrayTreeItems
& GetChildren() { return m_children
; }
58 const wxString
& GetText() const { return m_text
; }
59 int GetImage() const { return m_image
; }
60 int GetSelectedImage() const { return m_selImage
; }
61 wxTreeItemData
*GetData() const { return m_data
; }
63 void SetText( const wxString
&text
, wxDC
& dc
);
64 void SetImage(int image
) { m_image
= image
; }
65 void SetSelectedImage(int image
) { m_selImage
= image
; }
66 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
68 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
70 int GetX() const { return m_x
; }
71 int GetY() const { return m_y
; }
73 void SetHeight(int h
) { m_height
= h
; }
75 void SetX(int x
) { m_x
= x
; }
76 void SetY(int y
) { m_y
= y
; }
78 wxGenericTreeItem
*GetParent() const { return m_parent
; }
83 // get count of all children (and grand children and ...) of this item
84 size_t GetTotalNumberOfChildren() const;
86 void Insert(wxGenericTreeItem
*child
, size_t index
)
87 { m_children
.Insert(child
, index
); }
89 void SetCross( int x
, int y
);
90 void GetSize( int &x
, int &y
);
92 // return the item at given position (or NULL if no item), onButton is TRUE
93 // if the point belongs to the item's button, otherwise it lies on the
95 wxGenericTreeItem
*HitTest( const wxPoint
& point
, bool &onButton
);
97 void Expand() { m_isCollapsed
= FALSE
; }
98 void Collapse() { m_isCollapsed
= TRUE
; }
100 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
103 bool HasChildren() const { return !m_children
.IsEmpty(); }
104 bool HasHilight() const { return m_hasHilight
; }
105 bool IsExpanded() const { return !m_isCollapsed
; }
106 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
114 wxTreeItemData
*m_data
;
116 // @@ probably should use bitfields to save size
118 m_hasHilight
, // same as focused
119 m_hasPlus
; // used for item which doesn't have
120 // children but still has a [+] button
123 long m_height
, m_width
;
124 int m_xCross
, m_yCross
;
126 wxArrayTreeItems m_children
;
127 wxGenericTreeItem
*m_parent
;
130 // =============================================================================
132 // =============================================================================
134 // -----------------------------------------------------------------------------
136 // -----------------------------------------------------------------------------
138 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxCommandEvent
)
140 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
141 : wxCommandEvent( commandType
, id
)
144 m_itemOld
= (wxGenericTreeItem
*)NULL
;
147 // -----------------------------------------------------------------------------
149 // -----------------------------------------------------------------------------
151 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
152 const wxString
& text
,
154 int image
, int selImage
,
155 wxTreeItemData
*data
)
159 m_selImage
= selImage
;
162 m_xCross
= m_yCross
= 0;
166 m_isCollapsed
= TRUE
;
167 m_hasHilight
= FALSE
;
171 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
174 wxGenericTreeItem::~wxGenericTreeItem()
178 size_t count
= m_children
.Count();
179 for ( size_t n
= 0; n
< count
; n
++ )
180 delete m_children
[n
];
183 void wxGenericTreeItem::SetText( const wxString
&text
, wxDC
& dc
)
187 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
190 void wxGenericTreeItem::Reset()
197 m_height
= m_width
= 0;
204 m_isCollapsed
= TRUE
;
206 m_parent
= (wxGenericTreeItem
*)NULL
;
209 size_t wxGenericTreeItem::GetTotalNumberOfChildren() const
211 size_t count
= m_children
.Count();
212 size_t total
= count
;
213 for ( size_t n
= 0; n
< count
; n
++ )
215 total
+= m_children
[n
]->GetTotalNumberOfChildren();
221 void wxGenericTreeItem::SetCross( int x
, int y
)
227 void wxGenericTreeItem::GetSize( int &x
, int &y
)
229 // FIXME what does this all mean??
230 if ( y
< m_y
+ 10 ) y
= m_y
+10;
231 int width
= m_x
+ m_width
;
232 if (width
> x
) x
= width
;
234 size_t count
= m_children
.Count();
235 for ( size_t n
= 0; n
< count
; n
++ )
237 m_children
[n
]->GetSize( x
, y
);
241 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
244 if ((point
.y
> m_y
) && (point
.y
< m_y
+ m_height
))
247 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
248 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
255 if ((point
.x
> m_x
) && (point
.x
< m_x
+m_width
))
265 size_t count
= m_children
.Count();
266 for ( size_t n
= 0; n
< count
; n
++ )
268 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, onButton
);
278 // -----------------------------------------------------------------------------
279 // wxTreeCtrl implementation
280 // -----------------------------------------------------------------------------
282 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
284 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
285 EVT_PAINT (wxTreeCtrl::OnPaint
)
286 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
287 EVT_CHAR (wxTreeCtrl::OnChar
)
288 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
289 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
292 // -----------------------------------------------------------------------------
293 // construction/destruction
294 // -----------------------------------------------------------------------------
295 void wxTreeCtrl::Init()
298 m_anchor
= (wxGenericTreeItem
*) NULL
;
306 m_hilightBrush
= new wxBrush
308 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
313 m_imageListState
= (wxImageList
*) NULL
;
316 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
317 const wxPoint
& pos
, const wxSize
& size
,
318 long style
, const wxString
& name
)
322 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
, name
);
324 SetBackgroundColour( *wxWHITE
);
325 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
330 wxTreeCtrl::~wxTreeCtrl()
332 wxDELETE( m_hilightBrush
);
333 wxDELETE( m_anchor
);
336 // -----------------------------------------------------------------------------
338 // -----------------------------------------------------------------------------
340 size_t wxTreeCtrl::GetCount() const
342 return m_anchor
== NULL
? 0u : m_anchor
->GetTotalNumberOfChildren();
345 void wxTreeCtrl::SetIndent(unsigned int indent
)
351 // -----------------------------------------------------------------------------
352 // functions to work with tree items
353 // -----------------------------------------------------------------------------
355 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
357 return item
.m_pItem
->GetText();
360 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
362 return item
.m_pItem
->GetImage();
365 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
367 return item
.m_pItem
->GetSelectedImage();
370 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
372 return item
.m_pItem
->GetData();
375 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
378 return item
.m_pItem
->SetText(text
, dc
);
381 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
383 return item
.m_pItem
->SetImage(image
);
386 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
388 return item
.m_pItem
->SetSelectedImage(image
);
391 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
393 return item
.m_pItem
->SetData(data
);
396 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
398 item
.m_pItem
->SetHasPlus(has
);
401 // -----------------------------------------------------------------------------
402 // item status inquiries
403 // -----------------------------------------------------------------------------
405 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
407 wxFAIL_MSG("not implemented");
412 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
414 return !item
.m_pItem
->GetChildren().IsEmpty();
417 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
419 return item
.m_pItem
->IsExpanded();
422 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
424 return item
.m_pItem
->HasHilight();
427 // -----------------------------------------------------------------------------
429 // -----------------------------------------------------------------------------
431 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
433 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
435 return item
.m_pItem
->GetParent();
438 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
440 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
443 return GetNextChild(item
, cookie
);
446 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
448 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
450 return item
.m_pItem
->GetChildren().Item(cookie
++);
453 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
455 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
457 wxGenericTreeItem
*i
= item
.m_pItem
;
458 wxGenericTreeItem
*parent
= i
->GetParent();
459 if ( parent
== NULL
)
461 // root item doesn't have any siblings
465 wxArrayTreeItems
& siblings
= parent
->GetChildren();
466 int index
= siblings
.Index(i
);
467 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
469 size_t n
= (size_t)(index
+ 1);
470 return n
== siblings
.Count() ? NULL
: siblings
[n
];
473 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
475 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
477 wxGenericTreeItem
*i
= item
.m_pItem
;
478 wxGenericTreeItem
*parent
= i
->GetParent();
479 if ( parent
== NULL
)
481 // root item doesn't have any siblings
485 wxArrayTreeItems
& siblings
= parent
->GetChildren();
486 int index
= siblings
.Index(i
);
487 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
489 return index
== 0 ? NULL
: siblings
[(size_t)(index
- 1)];
492 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
494 wxFAIL_MSG("not implemented");
499 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
501 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
503 wxFAIL_MSG("not implemented");
508 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
510 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
512 wxFAIL_MSG("not implemented");
517 // -----------------------------------------------------------------------------
519 // -----------------------------------------------------------------------------
521 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
523 const wxString
& text
,
524 int image
, int selImage
,
525 wxTreeItemData
*data
)
527 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
530 // should we give a warning here?
531 return AddRoot(text
, image
, selImage
, data
);
535 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
542 data
->m_pItem
= item
;
545 parent
->Insert( item
, previous
);
547 CalculatePositions();
550 GetClientSize( &cw
, &ch
);
555 rect
.x
= dc
.LogicalToDeviceX( 0 );
557 rect
.width
= 10000; // @@@ not very elegant...
562 rect
.y
= dc
.LogicalToDeviceY( parent
->GetChildren().Item(previous
)->GetY() );
564 else // it's the 1st child
566 rect
.y
= dc
.LogicalToDeviceY( parent
->GetY() );
569 AdjustMyScrollbars();
571 if ( rect
.height
> 0 )
572 Refresh( FALSE
, &rect
);
577 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
578 int image
, int selImage
,
579 wxTreeItemData
*data
)
581 wxCHECK_MSG( !m_anchor
, NULL
, "tree can have only one root" );
584 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
585 image
, selImage
, data
);
588 data
->m_pItem
= m_anchor
;
591 AdjustMyScrollbars();
597 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
598 const wxString
& text
,
599 int image
, int selImage
,
600 wxTreeItemData
*data
)
602 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
605 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
606 const wxTreeItemId
& idPrevious
,
607 const wxString
& text
,
608 int image
, int selImage
,
609 wxTreeItemData
*data
)
611 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
614 // should we give a warning here?
615 return AddRoot(text
, image
, selImage
, data
);
618 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
619 wxASSERT_MSG( index
!= NOT_FOUND
,
620 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
621 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
624 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
625 const wxString
& text
,
626 int image
, int selImage
,
627 wxTreeItemData
*data
)
629 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
632 // should we give a warning here?
633 return AddRoot(text
, image
, selImage
, data
);
636 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
637 image
, selImage
, data
);
640 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
642 wxGenericTreeItem
*item
= itemId
.m_pItem
;
649 void wxTreeCtrl::DeleteAllItems()
660 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
662 wxGenericTreeItem
*item
= itemId
.m_pItem
;
664 if ( item
->IsExpanded() )
667 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
669 event
.SetEventObject( this );
670 if ( ProcessEvent( event
) && event
.m_code
)
672 // cancelled by program
678 RefreshSubtree(item
);
680 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
681 ProcessEvent( event
);
684 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
686 wxGenericTreeItem
*item
= itemId
.m_pItem
;
688 if ( !item
->IsExpanded() )
691 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
693 event
.SetEventObject( this );
694 if ( ProcessEvent( event
) && event
.m_code
)
696 // cancelled by program
702 wxArrayTreeItems
& children
= item
->GetChildren();
703 size_t count
= children
.Count();
704 for ( size_t n
= 0; n
< count
; n
++ )
706 Collapse(children
[n
]);
709 CalculatePositions();
711 RefreshSubtree(item
);
713 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
714 ProcessEvent( event
);
717 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
723 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
725 wxGenericTreeItem
*item
= itemId
.m_pItem
;
727 if ( item
->IsExpanded() )
733 void wxTreeCtrl::Unselect()
737 m_current
->SetHilight( FALSE
);
738 RefreshLine( m_current
);
742 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
744 wxGenericTreeItem
*item
= itemId
.m_pItem
;
746 if ( m_current
!= item
)
748 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
750 event
.m_itemOld
= m_current
;
751 event
.SetEventObject( this );
752 if ( ProcessEvent( event
) && event
.WasVetoed() )
757 m_current
->SetHilight( FALSE
);
758 RefreshLine( m_current
);
762 m_current
->SetHilight( TRUE
);
763 RefreshLine( m_current
);
765 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
766 ProcessEvent( event
);
770 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
772 wxFAIL_MSG("not implemented");
775 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
777 wxFAIL_MSG("not implemented");
780 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
781 wxClassInfo
* textCtrlClass
)
783 wxFAIL_MSG("not implemented");
788 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
790 wxFAIL_MSG("not implemented");
795 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
797 wxFAIL_MSG("not implemented");
800 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
,
801 wxTreeItemCmpFunc
*cmpFunction
)
803 wxFAIL_MSG("not implemented");
806 // -----------------------------------------------------------------------------
807 // images are not currently supported, but we still provide stubs for these
809 // -----------------------------------------------------------------------------
810 wxImageList
*wxTreeCtrl::GetImageList() const
812 return m_imageListNormal
;
815 wxImageList
*wxTreeCtrl::GetStateImageList() const
817 return m_imageListState
;
820 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
822 m_imageListNormal
= imageList
;
825 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
827 m_imageListState
= imageList
;
830 // -----------------------------------------------------------------------------
832 // -----------------------------------------------------------------------------
833 void wxTreeCtrl::AdjustMyScrollbars()
839 m_anchor
->GetSize( x
, y
);
841 int x_pos
= GetScrollPos( wxHORIZONTAL
);
842 int y_pos
= GetScrollPos( wxVERTICAL
);
843 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
847 SetScrollbars( 0, 0, 0, 0 );
851 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
853 int horizX
= level
*m_indent
;
855 item
->SetX( horizX
+33 );
856 item
->SetY( y
-m_lineHeight
/3 );
857 item
->SetHeight( m_lineHeight
);
859 item
->SetCross( horizX
+15, y
);
863 int exposed_x
= dc
.LogicalToDeviceX( 0 );
864 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
866 if (IsExposed( exposed_x
, exposed_y
, 1000, m_lineHeight
+4 ))
869 int endX
= horizX
+ 10;
871 if (!item
->HasChildren()) endX
+= 20;
873 dc
.DrawLine( startX
, y
, endX
, y
);
877 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
878 dc
.SetPen( *wxGREY_PEN
);
879 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
880 dc
.SetPen( *wxBLACK_PEN
);
881 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
883 if (!item
->IsExpanded())
884 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
887 if (item
->HasHilight())
889 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
890 dc
.SetBrush( *m_hilightBrush
);
892 dc
.GetTextExtent( item
->GetText(), &tw
, &th
);
895 dc
.SetPen( *wxBLACK_PEN
);
896 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, tw
+4, th
+4 );
900 dc
.SetPen( *wxTRANSPARENT_PEN
);
901 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, tw
+4, th
+4 );
903 dc
.DrawText( item
->GetText(), item
->GetX(), item
->GetY() );
905 dc
.SetPen( *wxBLACK_PEN
);
906 dc
.SetTextForeground( *wxBLACK
);
907 dc
.SetBrush( *wxWHITE_BRUSH
);
911 dc
.SetBrush( *wxWHITE_BRUSH
);
912 dc
.SetPen( *wxTRANSPARENT_PEN
);
914 dc
.GetTextExtent( item
->GetText(), &tw
, &th
);
915 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, tw
+4, th
+4 );
916 dc
.DrawText( item
->GetText(), item
->GetX(), item
->GetY() );
917 dc
.SetPen( *wxBLACK_PEN
);
921 if ( !item
->IsExpanded() )
926 wxArrayTreeItems
& children
= item
->GetChildren();
927 size_t count
= children
.Count();
928 for ( size_t n
= 0; n
< count
; n
++ )
933 PaintLevel( children
[n
], dc
, level
+1, y
);
936 dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
939 // -----------------------------------------------------------------------------
940 // wxWindows callbacks
941 // -----------------------------------------------------------------------------
943 void wxTreeCtrl::OnPaint( const wxPaintEvent
&WXUNUSED(event
) )
951 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
953 dc
.SetPen( m_dottedPen
);
954 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
956 int y
= m_lineHeight
/ 2 + 2;
957 PaintLevel( m_anchor
, dc
, 0, y
);
960 void wxTreeCtrl::OnSetFocus( const wxFocusEvent
&WXUNUSED(event
) )
964 RefreshLine( m_current
);
967 void wxTreeCtrl::OnKillFocus( const wxFocusEvent
&WXUNUSED(event
) )
971 RefreshLine( m_current
);
974 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
976 // TODO process '+', '-' (expand/collapse branch) and cursor keys
980 void wxTreeCtrl::OnMouse( const wxMouseEvent
&event
)
982 if ( !(event
.LeftDown() || event
.LeftDClick()) )
990 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
991 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
993 bool onButton
= FALSE
;
994 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1000 if ( event
.LeftDClick() )
1002 wxTreeEvent
event( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1003 event
.m_item
= item
;
1005 event
.SetEventObject( this );
1006 ProcessEvent( event
);
1015 // -----------------------------------------------------------------------------
1016 // -----------------------------------------------------------------------------
1017 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
,
1022 int horizX
= level
*m_indent
;
1024 item
->SetX( horizX
+33 );
1025 item
->SetY( y
-m_lineHeight
/3-2 );
1026 item
->SetHeight( m_lineHeight
);
1028 if ( item
->IsExpanded() )
1031 wxArrayTreeItems
& children
= item
->GetChildren();
1032 size_t count
= children
.Count();
1033 for ( size_t n
= 0; n
< count
; n
++ )
1036 CalculateLevel( children
[n
], dc
, level
+1, y
);
1040 void wxTreeCtrl::CalculatePositions()
1045 wxClientDC
dc(this);
1048 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1050 dc
.SetPen( m_dottedPen
);
1051 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1053 int y
= m_lineHeight
/ 2 + 2;
1054 CalculateLevel( m_anchor
, dc
, 0, y
);
1057 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1059 wxClientDC
dc(this);
1064 GetClientSize( &cw
, &ch
);
1067 rect
.x
= dc
.LogicalToDeviceX( 0 );
1069 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1072 Refresh( TRUE
, &rect
);
1074 AdjustMyScrollbars();
1077 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1079 wxClientDC
dc(this);
1083 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1084 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1086 rect
.height
= dc
.GetCharHeight() + 6;
1088 Refresh( TRUE
, &rect
);