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 and ...) of this item
85 size_t GetTotalNumberOfChildren() 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::GetTotalNumberOfChildren() const
213 size_t count
= m_children
.Count();
214 size_t total
= count
;
215 for ( size_t n
= 0; n
< count
; n
++ )
217 total
+= m_children
[n
]->GetTotalNumberOfChildren();
223 void wxGenericTreeItem::SetCross( int x
, int y
)
229 void wxGenericTreeItem::GetSize( int &x
, int &y
)
231 // FIXME what does this all mean??
232 if ( y
< m_y
) y
= m_y
;
233 int width
= m_x
+ m_width
;
234 if (width
> x
) x
= width
;
238 size_t count
= m_children
.Count();
239 for ( size_t n
= 0; n
< count
; n
++ )
241 m_children
[n
]->GetSize( x
, y
);
246 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
249 if ((point
.y
> m_y
) && (point
.y
< m_y
+ m_height
))
252 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
253 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
254 (IsExpanded() || HasPlus()))
260 if ((point
.x
> m_x
) && (point
.x
< m_x
+m_width
))
270 size_t count
= m_children
.Count();
271 for ( size_t n
= 0; n
< count
; n
++ )
273 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, onButton
);
283 // -----------------------------------------------------------------------------
284 // wxTreeCtrl implementation
285 // -----------------------------------------------------------------------------
287 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
289 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
290 EVT_PAINT (wxTreeCtrl::OnPaint
)
291 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
292 EVT_CHAR (wxTreeCtrl::OnChar
)
293 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
294 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
297 // -----------------------------------------------------------------------------
298 // construction/destruction
299 // -----------------------------------------------------------------------------
300 void wxTreeCtrl::Init()
303 m_anchor
= (wxGenericTreeItem
*) NULL
;
311 m_hilightBrush
= new wxBrush
313 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
318 m_imageListState
= (wxImageList
*) NULL
;
321 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
322 const wxPoint
& pos
, const wxSize
& size
,
323 long style
, const wxString
& name
)
327 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
, name
);
329 SetBackgroundColour( *wxWHITE
);
330 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
335 wxTreeCtrl::~wxTreeCtrl()
337 wxDELETE( m_hilightBrush
);
338 wxDELETE( m_anchor
);
341 // -----------------------------------------------------------------------------
343 // -----------------------------------------------------------------------------
345 size_t wxTreeCtrl::GetCount() const
347 return m_anchor
== NULL
? 0u : m_anchor
->GetTotalNumberOfChildren();
350 void wxTreeCtrl::SetIndent(unsigned int indent
)
356 // -----------------------------------------------------------------------------
357 // functions to work with tree items
358 // -----------------------------------------------------------------------------
360 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
362 return item
.m_pItem
->GetText();
365 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
367 return item
.m_pItem
->GetImage();
370 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
372 return item
.m_pItem
->GetSelectedImage();
375 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
377 return item
.m_pItem
->GetData();
380 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
383 item
.m_pItem
->SetText(text
, dc
);
386 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
388 item
.m_pItem
->SetImage(image
);
391 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
393 item
.m_pItem
->SetSelectedImage(image
);
396 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
398 item
.m_pItem
->SetData(data
);
401 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
403 item
.m_pItem
->SetHasPlus(has
);
406 // -----------------------------------------------------------------------------
407 // item status inquiries
408 // -----------------------------------------------------------------------------
410 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
412 wxFAIL_MSG("not implemented");
417 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
419 return !item
.m_pItem
->GetChildren().IsEmpty();
422 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
424 return item
.m_pItem
->IsExpanded();
427 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
429 return item
.m_pItem
->HasHilight();
432 // -----------------------------------------------------------------------------
434 // -----------------------------------------------------------------------------
436 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
438 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
440 return item
.m_pItem
->GetParent();
443 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
445 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
448 return GetNextChild(item
, cookie
);
451 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
453 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
455 return item
.m_pItem
->GetChildren().Item(cookie
++);
458 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
460 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
462 wxGenericTreeItem
*i
= item
.m_pItem
;
463 wxGenericTreeItem
*parent
= i
->GetParent();
464 if ( parent
== NULL
)
466 // root item doesn't have any siblings
470 wxArrayTreeItems
& siblings
= parent
->GetChildren();
471 int index
= siblings
.Index(i
);
472 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
474 size_t n
= (size_t)(index
+ 1);
475 return n
== siblings
.Count() ? (wxGenericTreeItem
*)NULL
: siblings
[n
];
478 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
480 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
482 wxGenericTreeItem
*i
= item
.m_pItem
;
483 wxGenericTreeItem
*parent
= i
->GetParent();
484 if ( parent
== NULL
)
486 // root item doesn't have any siblings
490 wxArrayTreeItems
& siblings
= parent
->GetChildren();
491 int index
= siblings
.Index(i
);
492 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
494 return index
== 0 ? (wxGenericTreeItem
*)NULL
: siblings
[(size_t)(index
- 1)];
497 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
499 wxFAIL_MSG("not implemented");
504 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
506 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
508 wxFAIL_MSG("not implemented");
513 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
515 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
517 wxFAIL_MSG("not implemented");
522 // -----------------------------------------------------------------------------
524 // -----------------------------------------------------------------------------
526 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
528 const wxString
& text
,
529 int image
, int selImage
,
530 wxTreeItemData
*data
)
532 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
535 // should we give a warning here?
536 return AddRoot(text
, image
, selImage
, data
);
540 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
547 data
->m_pItem
= item
;
550 parent
->Insert( item
, previous
);
552 CalculatePositions();
555 GetClientSize( &cw
, &ch
);
560 rect
.x
= dc
.LogicalToDeviceX( 0 );
562 rect
.width
= 10000; // @@@ not very elegant...
567 rect
.y
= dc
.LogicalToDeviceY( parent
->GetChildren().Item(previous
)->GetY() );
569 else // it's the 1st child
571 rect
.y
= dc
.LogicalToDeviceY( parent
->GetY() );
574 AdjustMyScrollbars();
576 if ( rect
.height
> 0 )
577 Refresh( FALSE
, &rect
);
582 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
583 int image
, int selImage
,
584 wxTreeItemData
*data
)
586 wxCHECK_MSG( !m_anchor
, NULL
, "tree can have only one root" );
589 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
590 image
, selImage
, data
);
593 data
->m_pItem
= m_anchor
;
596 AdjustMyScrollbars();
602 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
603 const wxString
& text
,
604 int image
, int selImage
,
605 wxTreeItemData
*data
)
607 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
610 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
611 const wxTreeItemId
& idPrevious
,
612 const wxString
& text
,
613 int image
, int selImage
,
614 wxTreeItemData
*data
)
616 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
619 // should we give a warning here?
620 return AddRoot(text
, image
, selImage
, data
);
623 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
624 wxASSERT_MSG( index
!= NOT_FOUND
,
625 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
626 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
629 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
630 const wxString
& text
,
631 int image
, int selImage
,
632 wxTreeItemData
*data
)
634 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
637 // should we give a warning here?
638 return AddRoot(text
, image
, selImage
, data
);
641 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
642 image
, selImage
, data
);
645 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
647 wxGenericTreeItem
*item
= itemId
.m_pItem
;
654 void wxTreeCtrl::DeleteAllItems()
665 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
667 wxGenericTreeItem
*item
= itemId
.m_pItem
;
669 if ( item
->IsExpanded() )
672 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
674 event
.SetEventObject( this );
675 if ( ProcessEvent( event
) && event
.m_code
)
677 // cancelled by program
683 RefreshSubtree(item
);
685 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
686 ProcessEvent( event
);
689 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
691 wxGenericTreeItem
*item
= itemId
.m_pItem
;
693 if ( !item
->IsExpanded() )
696 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
698 event
.SetEventObject( this );
699 if ( ProcessEvent( event
) && event
.m_code
)
701 // cancelled by program
707 wxArrayTreeItems
& children
= item
->GetChildren();
708 size_t count
= children
.Count();
709 for ( size_t n
= 0; n
< count
; n
++ )
711 Collapse(children
[n
]);
714 CalculatePositions();
716 RefreshSubtree(item
);
718 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
719 ProcessEvent( event
);
722 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
728 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
730 wxGenericTreeItem
*item
= itemId
.m_pItem
;
732 if ( item
->IsExpanded() )
738 void wxTreeCtrl::Unselect()
742 m_current
->SetHilight( FALSE
);
743 RefreshLine( m_current
);
747 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
749 wxGenericTreeItem
*item
= itemId
.m_pItem
;
751 if ( m_current
!= item
)
753 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
755 event
.m_itemOld
= m_current
;
756 event
.SetEventObject( this );
757 if ( ProcessEvent( event
) && event
.WasVetoed() )
762 m_current
->SetHilight( FALSE
);
763 RefreshLine( m_current
);
767 m_current
->SetHilight( TRUE
);
768 RefreshLine( m_current
);
770 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
771 ProcessEvent( event
);
775 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& WXUNUSED(item
))
777 wxFAIL_MSG("not implemented");
780 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
782 wxFAIL_MSG("not implemented");
785 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
786 wxClassInfo
* WXUNUSED(textCtrlClass
) )
788 wxFAIL_MSG("not implemented");
793 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
795 wxFAIL_MSG("not implemented");
800 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
802 wxFAIL_MSG("not implemented");
805 void wxTreeCtrl::SortChildren( const wxTreeItemId
& WXUNUSED(item
),
806 wxTreeItemCmpFunc
*WXUNUSED(cmpFunction
))
808 wxFAIL_MSG("not implemented");
811 // -----------------------------------------------------------------------------
812 // images are not currently supported, but we still provide stubs for these
814 // -----------------------------------------------------------------------------
815 wxImageList
*wxTreeCtrl::GetImageList() const
817 return m_imageListNormal
;
820 wxImageList
*wxTreeCtrl::GetStateImageList() const
822 return m_imageListState
;
825 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
827 m_imageListNormal
= imageList
;
830 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
832 m_imageListState
= imageList
;
835 // -----------------------------------------------------------------------------
837 // -----------------------------------------------------------------------------
838 void wxTreeCtrl::AdjustMyScrollbars()
844 m_anchor
->GetSize( x
, y
);
846 int x_pos
= GetScrollPos( wxHORIZONTAL
);
847 int y_pos
= GetScrollPos( wxVERTICAL
);
848 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
852 SetScrollbars( 0, 0, 0, 0 );
856 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
858 int horizX
= level
*m_indent
;
860 item
->SetX( horizX
+33 );
861 item
->SetY( y
-m_lineHeight
/3 );
862 item
->SetHeight( m_lineHeight
);
864 item
->SetCross( horizX
+15, y
);
868 int exposed_x
= dc
.LogicalToDeviceX( 0 );
869 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
871 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
874 int endX
= horizX
+ 10;
876 if (!item
->HasChildren()) endX
+= 20;
878 dc
.DrawLine( startX
, y
, endX
, y
);
882 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
883 dc
.SetPen( *wxGREY_PEN
);
884 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
885 dc
.SetPen( *wxBLACK_PEN
);
886 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
888 if (!item
->IsExpanded())
889 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
892 if (item
->HasHilight())
894 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
895 dc
.SetBrush( *m_hilightBrush
);
899 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
903 if (item
->GetImage() != -1)
905 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
910 dc
.SetPen( *wxBLACK_PEN
);
912 dc
.SetPen( *wxTRANSPARENT_PEN
);
914 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
916 if (item
->GetImage() != -1)
918 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
919 m_imageListNormal
->Draw( item
->GetImage(), dc
, item
->GetX(), item
->GetY()-1, wxIMAGELIST_DRAW_TRANSPARENT
);
920 dc
.DestroyClippingRegion();
922 dc
.DrawText( item
->GetText(), image_w
+item
->GetX(), item
->GetY() );
924 dc
.SetPen( *wxBLACK_PEN
);
925 dc
.SetTextForeground( *wxBLACK
);
926 dc
.SetBrush( *wxWHITE_BRUSH
);
930 dc
.SetBrush( *wxWHITE_BRUSH
);
931 dc
.SetPen( *wxTRANSPARENT_PEN
);
935 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
939 if (item
->GetImage() != -1)
941 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
945 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
947 if (item
->GetImage() != -1)
949 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
950 m_imageListNormal
->Draw( item
->GetImage(), dc
, item
->GetX(), item
->GetY()-1, wxIMAGELIST_DRAW_TRANSPARENT
);
951 dc
.DestroyClippingRegion();
954 dc
.DrawText( item
->GetText(), image_w
+item
->GetX(), item
->GetY() );
955 dc
.SetPen( *wxBLACK_PEN
);
959 if ( !item
->IsExpanded() )
964 wxArrayTreeItems
& children
= item
->GetChildren();
965 size_t count
= children
.Count();
966 for ( size_t n
= 0; n
< count
; n
++ )
971 PaintLevel( children
[n
], dc
, level
+1, y
);
974 dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
977 // -----------------------------------------------------------------------------
978 // wxWindows callbacks
979 // -----------------------------------------------------------------------------
981 void wxTreeCtrl::OnPaint( const wxPaintEvent
&WXUNUSED(event
) )
989 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
991 dc
.SetPen( m_dottedPen
);
992 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
994 int y
= m_lineHeight
/ 2 + 2;
995 PaintLevel( m_anchor
, dc
, 0, y
);
998 void wxTreeCtrl::OnSetFocus( const wxFocusEvent
&WXUNUSED(event
) )
1002 RefreshLine( m_current
);
1005 void wxTreeCtrl::OnKillFocus( const wxFocusEvent
&WXUNUSED(event
) )
1009 RefreshLine( m_current
);
1012 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1014 // TODO process '+', '-' (expand/collapse branch) and cursor keys
1018 void wxTreeCtrl::OnMouse( const wxMouseEvent
&event
)
1020 if ( !(event
.LeftDown() || event
.LeftDClick()) )
1026 wxClientDC
dc(this);
1028 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1029 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1031 bool onButton
= FALSE
;
1032 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1038 if ( event
.LeftDClick() )
1040 wxTreeEvent
event( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1041 event
.m_item
= item
;
1043 event
.SetEventObject( this );
1044 ProcessEvent( event
);
1053 // -----------------------------------------------------------------------------
1054 // -----------------------------------------------------------------------------
1055 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
,
1060 int horizX
= level
*m_indent
;
1062 item
->SetX( horizX
+33 );
1063 item
->SetY( y
-m_lineHeight
/3-2 );
1064 item
->SetHeight( m_lineHeight
);
1066 if ( item
->IsExpanded() )
1069 wxArrayTreeItems
& children
= item
->GetChildren();
1070 size_t count
= children
.Count();
1071 for ( size_t n
= 0; n
< count
; n
++ )
1074 CalculateLevel( children
[n
], dc
, level
+1, y
);
1078 void wxTreeCtrl::CalculatePositions()
1083 wxClientDC
dc(this);
1086 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1088 dc
.SetPen( m_dottedPen
);
1089 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1091 int y
= m_lineHeight
/ 2 + 2;
1092 CalculateLevel( m_anchor
, dc
, 0, y
);
1095 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1097 wxClientDC
dc(this);
1102 GetClientSize( &cw
, &ch
);
1105 rect
.x
= dc
.LogicalToDeviceX( 0 );
1107 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1110 Refresh( TRUE
, &rect
);
1112 AdjustMyScrollbars();
1115 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1117 wxClientDC
dc(this);
1121 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1122 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1124 rect
.height
= dc
.GetCharHeight() + 6;
1126 Refresh( TRUE
, &rect
);