1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "treectrl.h"
15 #include "wx/treectrl.h"
16 #include "wx/settings.h"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 IMPLEMENT_DYNAMIC_CLASS(wxTreeItem
, wxObject
)
24 wxTreeItem::wxTreeItem(void)
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
,wxCommandEvent
)
42 wxTreeEvent::wxTreeEvent( WXTYPE commandType
, int id
) :
43 wxCommandEvent( commandType
, id
)
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeItem
,wxObject
)
55 wxGenericTreeItem::wxGenericTreeItem( wxGenericTreeItem
*parent
)
62 wxGenericTreeItem::wxGenericTreeItem( wxGenericTreeItem
*parent
, const wxTreeItem
& item
, wxDC
*dc
)
70 void wxGenericTreeItem::SetItem( const wxTreeItem
&item
, wxDC
*dc
)
72 if ((item
.m_mask
& wxTREE_MASK_HANDLE
) == wxTREE_MASK_HANDLE
)
73 m_itemId
= item
.m_itemId
;
74 if ((item
.m_mask
& wxTREE_MASK_STATE
) == wxTREE_MASK_STATE
)
75 m_state
= item
.m_state
;
76 if ((item
.m_mask
& wxTREE_MASK_TEXT
) == wxTREE_MASK_TEXT
)
78 if ((item
.m_mask
& wxTREE_MASK_IMAGE
) == wxTREE_MASK_IMAGE
)
79 m_image
= item
.m_image
;
80 if ((item
.m_mask
& wxTREE_MASK_SELECTED_IMAGE
) == wxTREE_MASK_SELECTED_IMAGE
)
81 m_selectedImage
= item
.m_selectedImage
;
82 if ((item
.m_mask
& wxTREE_MASK_CHILDREN
) == wxTREE_MASK_CHILDREN
)
83 m_hasChildren
= (item
.m_children
> 0);
84 if ((item
.m_mask
& wxTREE_MASK_DATA
) == wxTREE_MASK_DATA
)
88 dc
->GetTextExtent( m_text
, &lw
, &lh
);
93 void wxGenericTreeItem::SetText( const wxString
&text
, wxDC
*dc
)
98 dc
->GetTextExtent( m_text
, &lw
, &lh
);
103 void wxGenericTreeItem::Reset(void)
109 m_selectedImage
= -1;
111 m_hasChildren
= FALSE
;
120 m_children
.DeleteContents( TRUE
);
124 void wxGenericTreeItem::GetItem( wxTreeItem
&item
) const
126 if ((item
.m_mask
& wxTREE_MASK_STATE
) == wxTREE_MASK_STATE
)
127 item
.m_state
= m_state
;
128 if ((item
.m_mask
& wxTREE_MASK_TEXT
) == wxTREE_MASK_TEXT
)
129 item
.m_text
= m_text
;
130 if ((item
.m_mask
& wxTREE_MASK_IMAGE
) == wxTREE_MASK_IMAGE
)
131 item
.m_image
= m_image
;
132 if ((item
.m_mask
& wxTREE_MASK_SELECTED_IMAGE
) == wxTREE_MASK_SELECTED_IMAGE
)
133 item
.m_selectedImage
= m_selectedImage
;
134 if ((item
.m_mask
& wxTREE_MASK_CHILDREN
) == wxTREE_MASK_CHILDREN
)
135 item
.m_children
= (int)m_hasChildren
;
136 if ((item
.m_mask
& wxTREE_MASK_DATA
) == wxTREE_MASK_DATA
)
137 item
.m_data
= m_data
;
140 bool wxGenericTreeItem::HasChildren(void)
142 return m_hasChildren
;
145 bool wxGenericTreeItem::HasPlus(void)
147 return (m_hasChildren
&& (m_children
.Number() == 0));
150 int wxGenericTreeItem::NumberOfVisibleDescendents(void)
152 int ret
= m_children
.Number();
153 wxNode
*node
= m_children
.First();
156 wxGenericTreeItem
*item
= (wxGenericTreeItem
*)node
->Data();
157 ret
+= item
->NumberOfVisibleDescendents();
163 int wxGenericTreeItem::NumberOfVisibleChildren(void)
165 return m_children
.Number();
168 wxGenericTreeItem
*wxGenericTreeItem::FindItem( long itemId
) const
170 if (m_itemId
== itemId
) return (wxGenericTreeItem
*)(this);
171 wxNode
*node
= m_children
.First();
174 wxGenericTreeItem
*item
= (wxGenericTreeItem
*)node
->Data();
175 wxGenericTreeItem
*res
= item
->FindItem( itemId
);
176 if (res
) return (wxGenericTreeItem
*)(res
);
182 void wxGenericTreeItem::AddChild( wxGenericTreeItem
*child
)
184 m_children
.Append( child
);
187 void wxGenericTreeItem::SetCross( int x
, int y
)
193 void wxGenericTreeItem::GetSize( int &x
, int &y
)
195 if (y
< m_y
+ 10) y
= m_y
+10;
196 int width
= m_x
+ m_width
;
197 if (width
> x
) x
= width
;
198 wxNode
*node
= m_children
.First();
201 wxGenericTreeItem
*item
= (wxGenericTreeItem
*)node
->Data();
202 item
->GetSize( x
, y
);
207 long wxGenericTreeItem::HitTest( const wxPoint
& point
, int &flags
)
209 if (m_parent
&& ((point
.y
> m_y
) && (point
.y
< m_y
+m_height
)))
211 if ((point
.x
> m_xCross
-5) &&
212 (point
.x
< m_xCross
+5) &&
213 (point
.y
> m_yCross
-5) &&
214 (point
.y
< m_yCross
+5) &&
218 flags
= wxTREE_HITTEST_ONITEMBUTTON
;
221 if ((point
.x
> m_x
) && (point
.x
< m_x
+m_width
))
223 flags
= wxTREE_HITTEST_ONITEMLABEL
;
228 flags
= wxTREE_HITTEST_ONITEMRIGHT
;
231 flags
= wxTREE_HITTEST_ONITEMINDENT
;
236 wxNode
*node
= m_children
.First();
239 wxGenericTreeItem
*child
= (wxGenericTreeItem
*)node
->Data();
240 long res
= child
->HitTest( point
, flags
);
241 if (res
!= -1) return res
;
248 void wxGenericTreeItem::PrepareEvent( wxTreeEvent
&event
)
250 event
.m_item
.m_itemId
= m_itemId
;
251 event
.m_item
.m_state
= m_state
;
252 event
.m_item
.m_text
= m_text
;
253 event
.m_item
.m_image
= m_image
;
254 event
.m_item
.m_selectedImage
= m_selectedImage
;
255 event
.m_item
.m_children
= (int)m_hasChildren
;
256 event
.m_item
.m_data
= m_data
;
259 event
.m_pointDrag
.x
= 0;
260 event
.m_pointDrag
.y
= 0;
263 void wxGenericTreeItem::SendKeyDown( wxWindow
*target
)
265 wxTreeEvent
event( wxEVT_COMMAND_TREE_KEY_DOWN
, target
->GetId() );
266 PrepareEvent( event
);
267 event
.SetEventObject( target
);
268 target
->ProcessEvent( event
);
271 void wxGenericTreeItem::SendSelected( wxWindow
*target
)
273 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGED
, target
->GetId() );
274 PrepareEvent( event
);
275 event
.SetEventObject( target
);
276 target
->ProcessEvent( event
);
279 void wxGenericTreeItem::SendDelete( wxWindow
*target
)
281 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
,
283 PrepareEvent( event
);
284 event
.SetEventObject( target
);
285 target
->ProcessEvent( event
);
288 void wxGenericTreeItem::SendExpand( wxWindow
*target
)
290 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDED
,
292 PrepareEvent( event
);
293 event
.SetEventObject( target
);
294 target
->ProcessEvent( event
);
297 void wxGenericTreeItem::SetHilight( bool set
)
302 bool wxGenericTreeItem::HasHilight(void)
307 //-----------------------------------------------------------------------------
309 //-----------------------------------------------------------------------------
311 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
,wxScrolledWindow
314 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
315 EVT_PAINT (wxTreeCtrl::OnPaint
)
316 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
317 EVT_CHAR (wxTreeCtrl::OnChar
)
318 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
319 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
322 wxTreeCtrl::wxTreeCtrl(void)
334 m_hilightBrush
= new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
), wxSOLID
);
337 wxTreeCtrl::wxTreeCtrl(wxWindow
*parent
, const wxWindowID id
,
340 const long style
, const wxString
& name
)
352 m_hilightBrush
= new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
), wxSOLID
);
353 Create( parent
, id
, pos
, size
, style
, name
);
356 wxTreeCtrl::~wxTreeCtrl(void)
358 if (m_dc
) delete m_dc
;
361 bool wxTreeCtrl::Create(wxWindow
*parent
, const wxWindowID id
,
365 , const wxString
& name
)
367 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
, name
);
368 SetBackgroundColour( *wxWHITE
);
369 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
373 int wxTreeCtrl::GetCount(void) const
375 if (!m_anchor
) return 0;
376 return m_anchor
->NumberOfVisibleDescendents();
379 long wxTreeCtrl::InsertItem( const long parent
, const wxString
& label
, const int image
,
380 const int selImage
, const long WXUNUSED(insertAfter
) )
382 wxGenericTreeItem
*p
= NULL
;
385 if (m_anchor
) return -1;
389 p
= FindItem( parent
);
394 item
.m_mask
= wxTREE_MASK_HANDLE
;
395 item
.m_itemId
= m_lastId
;
396 if (!label
.IsNull() || (label
== ""))
399 item
.m_mask
|= wxTREE_MASK_TEXT
;
403 item
.m_image
= image
;
404 item
.m_mask
|= wxTREE_MASK_IMAGE
;
408 item
.m_selectedImage
= selImage
;
409 item
.m_mask
|= wxTREE_MASK_SELECTED_IMAGE
;
413 wxGenericTreeItem
*new_child
= new wxGenericTreeItem( p
, item
, &dc
);
415 p
->AddChild( new_child
);
417 m_anchor
= new_child
;
421 CalculatePositions();
424 GetClientSize( NULL
, &ch
);
427 rect
.x
= 0; rect
.y
= 0;
428 rect
.width
= 10000; rect
.height
= ch
;
431 if (p
->m_children
.Number() == 1)
433 rect
.y
= dc
.LogicalToDeviceY( p
->m_y
);
437 wxNode
*node
= p
->m_children
.Member( new_child
)->Previous();
438 wxGenericTreeItem
* last_child
= (wxGenericTreeItem
*)node
->Data();
439 rect
.y
= dc
.LogicalToDeviceY( last_child
->m_y
);
444 dc
.GetDeviceOrigin( &doX
, &doY
);
445 rect
.height
= ch
-rect
.y
-doY
;
447 AdjustMyScrollbars();
449 if (rect
.height
> 0) Refresh( FALSE
, &rect
);
453 AdjustMyScrollbars();
461 long wxTreeCtrl::InsertItem( const long parent
, wxTreeItem
&info
, const long WXUNUSED(insertAfter
) )
463 int oldMask
= info
.m_mask
;
464 wxGenericTreeItem
*p
= NULL
;
467 if (m_anchor
) return -1;
471 p
= FindItem( parent
);
474 printf( "TreeItem not found.\n" );
479 if ((info
.m_mask
& wxTREE_MASK_HANDLE
) == 0)
482 info
.m_itemId
= m_lastId
;
483 info
.m_mask
|= wxTREE_MASK_HANDLE
;
492 wxGenericTreeItem
*new_child
= new wxGenericTreeItem( p
, info
, &dc
);
494 p
->AddChild( new_child
);
496 m_anchor
= new_child
;
500 CalculatePositions();
503 GetClientSize( NULL
, &ch
);
506 rect
.x
= 0; rect
.y
= 0;
507 rect
.width
= 10000; rect
.height
= ch
;
510 if (p
->m_children
.Number() == 1)
512 rect
.y
= dc
.LogicalToDeviceY( p
->m_y
);
516 wxNode
*node
= p
->m_children
.Member( new_child
)->Previous();
517 wxGenericTreeItem
* last_child
= (wxGenericTreeItem
*)node
->Data();
518 rect
.y
= dc
.LogicalToDeviceY( last_child
->m_y
);
523 dc
.GetDeviceOrigin( &doX
, &doY
);
524 rect
.height
= ch
-rect
.y
-doY
;
526 AdjustMyScrollbars();
528 if (rect
.height
> 0) Refresh( FALSE
, &rect
);
532 AdjustMyScrollbars();
537 info
.m_mask
= oldMask
;
541 bool wxTreeCtrl::ExpandItem( const long item
, const int action
)
543 wxGenericTreeItem
*i
= FindItem( item
);
544 if (!i
) return FALSE
;
547 case wxTREE_EXPAND_EXPAND
:
549 i
->SendExpand( this );
552 case wxTREE_EXPAND_COLLAPSE_RESET
:
553 case wxTREE_EXPAND_COLLAPSE
:
555 wxNode
*node
= i
->m_children
.First();
558 wxGenericTreeItem
*child
= (wxGenericTreeItem
*)node
->Data();
559 child
->SendDelete( this );
561 node
= i
->m_children
.First();
566 GetClientSize( &cw
, &ch
);
572 rect
.y
= dc
.LogicalToDeviceY( i
->m_y
);
575 dc
.GetDeviceOrigin( NULL
, &doY
);
576 rect
.height
= ch
-rect
.y
-doY
;
577 Refresh( TRUE
, &rect
);
579 AdjustMyScrollbars();
582 case wxTREE_EXPAND_TOGGLE
:
585 ExpandItem( item
, wxTREE_EXPAND_EXPAND
);
587 ExpandItem( item
, wxTREE_EXPAND_COLLAPSE
);
594 bool wxTreeCtrl::DeleteAllItems(void)
602 bool wxTreeCtrl::GetItem( wxTreeItem
&info
) const
604 wxGenericTreeItem
*i
= FindItem( info
.m_itemId
);
605 if (!i
) return FALSE
;
610 long wxTreeCtrl::GetItemData( const long item
) const
612 wxGenericTreeItem
*i
= FindItem( item
);
617 wxString
wxTreeCtrl::GetItemText( const long item
) const
619 wxGenericTreeItem
*i
= FindItem( item
);
624 long wxTreeCtrl::GetParent( const long item
) const
626 wxGenericTreeItem
*i
= FindItem( item
);
630 return i
->m_parent
->m_itemId
;
633 long wxTreeCtrl::GetRootItem(void) const
635 if (m_anchor
) return m_anchor
->m_itemId
;
639 long wxTreeCtrl::GetSelection(void) const
644 bool wxTreeCtrl::SelectItem( const long WXUNUSED(item
) ) const
649 bool wxTreeCtrl::ItemHasChildren( const long item
) const
651 wxGenericTreeItem
*i
= FindItem( item
);
652 if (!i
) return FALSE
;
653 return i
->m_hasChildren
;
656 void wxTreeCtrl::SetIndent( const int indent
)
662 int wxTreeCtrl::GetIndent(void) const
667 bool wxTreeCtrl::SetItem( wxTreeItem
&info
)
669 wxGenericTreeItem
*i
= FindItem( info
.m_itemId
);
670 if (!i
) return FALSE
;
672 i
->SetItem( info
, &dc
);
676 bool wxTreeCtrl::SetItemData( const long item
, const long data
)
678 wxGenericTreeItem
*i
= FindItem( item
);
679 if (!i
) return FALSE
;
684 bool wxTreeCtrl::SetItemText( const long item
, const wxString
&text
)
686 wxGenericTreeItem
*i
= FindItem( item
);
687 if (!i
) return FALSE
;
689 i
->SetText( text
, &dc
);
693 long wxTreeCtrl::HitTest( const wxPoint
& point
, int &flags
)
696 if (!m_anchor
) return -1;
697 return m_anchor
->HitTest( point
, flags
);
700 void wxTreeCtrl::AdjustMyScrollbars(void)
706 m_anchor
->GetSize( x
, y
);
708 int x_pos
= GetScrollPos( wxHORIZONTAL
);
709 int y_pos
= GetScrollPos( wxVERTICAL
);
710 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
714 SetScrollbars( 0, 0, 0, 0 );
718 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxPaintDC
&dc
, int level
, int &y
)
720 int horizX
= level
*m_indent
+10;
722 wxNode
*node
= item
->m_children
.First();
725 wxGenericTreeItem
*child
= (wxGenericTreeItem
*)node
->Data();
726 dc
.SetPen( m_dottedPen
);
728 child
->SetCross( horizX
+15, y
);
732 if (level
!= 0) oldY
-= (m_lineHeight
-5);
733 dc
.DrawLine( horizX
, oldY
, horizX
, y
);
736 child
->m_x
= horizX
+33;
737 child
->m_y
= y
-m_lineHeight
/3;
738 child
->m_height
= m_lineHeight
;
740 if (IsExposed( 0, child
->m_y
-2, 10000, m_lineHeight
+4 ))
743 if ((node
->Previous()) || (level
!= 0))
744 startX
= horizX
; else startX
= horizX
-10;
745 if (child
->HasChildren())
746 endX
= horizX
+10; else endX
= horizX
+30;
747 dc
.DrawLine( startX
, y
, endX
, y
);
749 if (child
->HasChildren())
751 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
752 dc
.SetPen( *wxGREY_PEN
);
753 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
754 dc
.SetPen( *wxBLACK_PEN
);
755 dc
.DrawLine( horizX
+13, y
, horizX
+17, y
);
756 if (child
->HasPlus())
757 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+2 );
760 if (child
->HasHilight())
762 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
763 dc
.SetBrush( *m_hilightBrush
);
765 dc
.SetPen( wxBLACK_PEN
);
767 dc
.SetPen( wxTRANSPARENT_PEN
);
770 dc
.GetTextExtent( child
->m_text
, &tw
, &th
);
771 dc
.DrawRectangle( child
->m_x
-2, child
->m_y
-2, tw
+4, th
+4 );
774 dc
.DrawText( child
->m_text
, child
->m_x
, child
->m_y
);
776 if (child
->HasHilight())
778 dc
.SetTextForeground( *wxBLACK
);
779 dc
.SetBrush( *wxWHITE_BRUSH
);
780 dc
.SetPen( *wxBLACK_PEN
);
786 if (child
->NumberOfVisibleChildren() > 0)
787 PaintLevel( child
, dc
, level
+1, y
);
792 void wxTreeCtrl::OnPaint( const wxPaintEvent
&WXUNUSED(event
) )
794 if (!m_anchor
) return;
798 m_dc
= new wxPaintDC(this);
802 m_dc
->SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
804 m_dc
->SetPen( m_dottedPen
);
805 m_lineHeight
= (int)(m_dc
->GetCharHeight() + 4);
807 int y
= m_lineHeight
/ 2 + 2;
808 PaintLevel( m_anchor
, *m_dc
, 0, y
);
811 void wxTreeCtrl::OnSetFocus( const wxFocusEvent
&WXUNUSED(event
) )
814 if (m_current
) RefreshLine( m_current
);
817 void wxTreeCtrl::OnKillFocus( const wxFocusEvent
&WXUNUSED(event
) )
820 if (m_current
) RefreshLine( m_current
);
823 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
828 void wxTreeCtrl::OnMouse( const wxMouseEvent
&event
)
830 if (!event
.LeftDown() &&
831 !event
.LeftDClick()) return;
835 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
836 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
839 long id
= HitTest( wxPoint(x
,y
), flag
);
840 if (id
== -1) return;
841 wxGenericTreeItem
*item
= FindItem( id
);
844 if ((flag
!= wxTREE_HITTEST_ONITEMBUTTON
) &&
845 (flag
!= wxTREE_HITTEST_ONITEMLABEL
)) return;
847 if (m_current
!= item
)
851 m_current
->SetHilight( FALSE
);
852 RefreshLine( m_current
);
855 m_current
->SetHilight( TRUE
);
856 RefreshLine( m_current
);
857 m_current
->SendSelected( this );
860 if (event
.LeftDClick()) m_current
->SendKeyDown( this );
862 if (flag
== wxTREE_HITTEST_ONITEMBUTTON
)
864 ExpandItem( item
->m_itemId
, wxTREE_EXPAND_TOGGLE
);
869 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxPaintDC
&dc
, int level
, int &y
)
871 int horizX
= level
*m_indent
+10;
872 wxNode
*node
= item
->m_children
.First();
875 wxGenericTreeItem
*child
= (wxGenericTreeItem
*)node
->Data();
876 dc
.SetPen( m_dottedPen
);
879 if ((node
->Previous()) || (level
!= 0))
880 startX
= horizX
; else startX
= horizX
-10;
881 if (child
->HasChildren())
882 endX
= horizX
+10; else endX
= horizX
+30;
884 child
->m_x
= horizX
+33;
885 child
->m_y
= y
-m_lineHeight
/3-2;
886 child
->m_height
= m_lineHeight
;
889 if (child
->NumberOfVisibleChildren() > 0)
890 CalculateLevel( child
, dc
, level
+1, y
);
896 void wxTreeCtrl::CalculatePositions(void)
898 if (!m_anchor
) return;
903 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
905 dc
.SetPen( m_dottedPen
);
906 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
908 int y
= m_lineHeight
/ 2 + 2;
909 CalculateLevel( m_anchor
, dc
, 0, y
);
912 wxGenericTreeItem
*wxTreeCtrl::FindItem( long itemId
) const
914 if (!m_anchor
) return NULL
;
915 return m_anchor
->FindItem( itemId
);
918 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
924 rect
.x
= dc
.LogicalToDeviceX( item
->m_x
-2 );
925 rect
.y
= dc
.LogicalToDeviceY( item
->m_y
-2 );
927 rect
.height
= dc
.GetCharHeight()+4;
928 Refresh( TRUE
, &rect
);