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 void SetBold(bool bold
) { m_isBold
= bold
; }
73 int GetX() const { return m_x
; }
74 int GetY() const { return m_y
; }
76 void SetHeight(int h
) { m_height
= h
; }
78 void SetX(int x
) { m_x
= x
; }
79 void SetY(int y
) { m_y
= y
; }
81 wxGenericTreeItem
*GetParent() const { return m_parent
; }
84 void DeleteChildren();
87 // get count of all children (and grand children if 'recursively')
88 size_t GetChildrenCount(bool recursively
= TRUE
) const;
90 void Insert(wxGenericTreeItem
*child
, size_t index
)
91 { m_children
.Insert(child
, index
); }
93 void SetCross( int x
, int y
);
94 void GetSize( int &x
, int &y
);
96 // return the item at given position (or NULL if no item), onButton is TRUE
97 // if the point belongs to the item's button, otherwise it lies on the
99 wxGenericTreeItem
*HitTest( const wxPoint
& point
, bool &onButton
);
101 void Expand() { m_isCollapsed
= FALSE
; }
102 void Collapse() { m_isCollapsed
= TRUE
; }
104 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
107 bool HasChildren() const { return !m_children
.IsEmpty(); }
108 bool HasHilight() const { return m_hasHilight
; }
109 bool IsExpanded() const { return !m_isCollapsed
; }
110 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
111 bool IsBold() const { return m_isBold
; }
119 wxTreeItemData
*m_data
;
121 // use bitfields to save size
122 int m_isCollapsed
:1;
123 int m_hasHilight
:1; // same as focused
124 int m_hasPlus
:1; // used for item which doesn't have
125 // children but still has a [+] button
126 int m_isBold
:1; // render the label in bold font
129 long m_height
, m_width
;
130 int m_xCross
, m_yCross
;
132 wxArrayTreeItems m_children
;
133 wxGenericTreeItem
*m_parent
;
136 // =============================================================================
138 // =============================================================================
140 // -----------------------------------------------------------------------------
142 // -----------------------------------------------------------------------------
144 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
146 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
147 : wxNotifyEvent( commandType
, id
)
150 m_itemOld
= (wxGenericTreeItem
*)NULL
;
153 // -----------------------------------------------------------------------------
155 // -----------------------------------------------------------------------------
157 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
158 const wxString
& text
,
160 int image
, int selImage
,
161 wxTreeItemData
*data
)
165 m_selImage
= selImage
;
168 m_xCross
= m_yCross
= 0;
172 m_isCollapsed
= TRUE
;
173 m_hasHilight
= FALSE
;
179 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
182 wxGenericTreeItem::~wxGenericTreeItem()
189 void wxGenericTreeItem::DeleteChildren()
191 size_t count
= m_children
.Count();
192 for ( size_t n
= 0; n
< count
; n
++ )
193 delete m_children
[n
];
198 void wxGenericTreeItem::SetText( const wxString
&text
, wxDC
& dc
)
202 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
205 void wxGenericTreeItem::Reset()
212 m_height
= m_width
= 0;
219 m_isCollapsed
= TRUE
;
221 m_parent
= (wxGenericTreeItem
*)NULL
;
224 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
226 size_t count
= m_children
.Count();
230 size_t total
= count
;
231 for ( size_t n
= 0; n
< count
; n
++ )
233 total
+= m_children
[n
]->GetChildrenCount();
239 void wxGenericTreeItem::SetCross( int x
, int y
)
245 void wxGenericTreeItem::GetSize( int &x
, int &y
)
247 if ( y
< m_y
) y
= m_y
;
248 int width
= m_x
+ m_width
;
249 if (width
> x
) x
= width
;
253 size_t count
= m_children
.Count();
254 for ( size_t n
= 0; n
< count
; n
++ )
256 m_children
[n
]->GetSize( x
, y
);
261 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
264 if ((point
.y
> m_y
) && (point
.y
< m_y
+ m_height
))
267 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
268 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
269 (IsExpanded() || HasPlus()))
275 if ((point
.x
> m_x
) && (point
.x
< m_x
+m_width
))
285 size_t count
= m_children
.Count();
286 for ( size_t n
= 0; n
< count
; n
++ )
288 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, onButton
);
298 // -----------------------------------------------------------------------------
299 // wxTreeCtrl implementation
300 // -----------------------------------------------------------------------------
302 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
304 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
305 EVT_PAINT (wxTreeCtrl::OnPaint
)
306 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
307 EVT_CHAR (wxTreeCtrl::OnChar
)
308 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
309 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
310 EVT_IDLE (wxTreeCtrl::OnIdle
)
313 // -----------------------------------------------------------------------------
314 // construction/destruction
315 // -----------------------------------------------------------------------------
316 void wxTreeCtrl::Init()
319 m_anchor
= (wxGenericTreeItem
*) NULL
;
328 m_hilightBrush
= new wxBrush
330 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
335 m_imageListState
= (wxImageList
*) NULL
;
338 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
339 const wxPoint
& pos
, const wxSize
& size
,
341 const wxValidator
&validator
,
342 const wxString
& name
)
346 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
348 SetValidator( validator
);
350 SetBackgroundColour( *wxWHITE
);
351 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
356 wxTreeCtrl::~wxTreeCtrl()
358 wxDELETE( m_hilightBrush
);
359 wxDELETE( m_anchor
);
362 // -----------------------------------------------------------------------------
364 // -----------------------------------------------------------------------------
366 size_t wxTreeCtrl::GetCount() const
368 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
371 void wxTreeCtrl::SetIndent(unsigned int indent
)
377 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
379 wxCHECK_MSG( item
.IsOk(), 0u, "invalid tree item" );
381 return item
.m_pItem
->GetChildrenCount(recursively
);
384 // -----------------------------------------------------------------------------
385 // functions to work with tree items
386 // -----------------------------------------------------------------------------
388 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
390 wxCHECK_MSG( item
.IsOk(), "", "invalid tree item" );
392 return item
.m_pItem
->GetText();
395 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
397 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
399 return item
.m_pItem
->GetImage();
402 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
404 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
406 return item
.m_pItem
->GetSelectedImage();
409 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
411 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
413 return item
.m_pItem
->GetData();
416 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
418 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
421 item
.m_pItem
->SetText(text
, dc
);
424 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
426 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
428 item
.m_pItem
->SetImage(image
);
431 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
433 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
435 item
.m_pItem
->SetSelectedImage(image
);
438 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
440 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
442 item
.m_pItem
->SetData(data
);
445 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
447 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
449 item
.m_pItem
->SetHasPlus(has
);
452 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
454 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
456 // avoid redrawing the tree if no real change
457 wxGenericTreeItem
*pItem
= item
.m_pItem
;
458 if ( pItem
->IsBold() != bold
)
460 pItem
->SetBold(bold
);
465 // -----------------------------------------------------------------------------
466 // item status inquiries
467 // -----------------------------------------------------------------------------
469 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
471 wxFAIL_MSG("not implemented");
476 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
478 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
480 return !item
.m_pItem
->GetChildren().IsEmpty();
483 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
485 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
487 return item
.m_pItem
->IsExpanded();
490 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
492 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
494 return item
.m_pItem
->HasHilight();
497 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
499 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
501 return item
.m_pItem
->IsBold();
504 // -----------------------------------------------------------------------------
506 // -----------------------------------------------------------------------------
508 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
510 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
512 return item
.m_pItem
->GetParent();
515 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
517 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
520 return GetNextChild(item
, cookie
);
523 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
525 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
527 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
528 if ( (size_t)cookie
< children
.Count() )
530 return item
.m_pItem
->GetChildren().Item(cookie
++);
534 // there are no more of them
539 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
541 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
543 wxGenericTreeItem
*i
= item
.m_pItem
;
544 wxGenericTreeItem
*parent
= i
->GetParent();
545 if ( parent
== NULL
)
547 // root item doesn't have any siblings
551 wxArrayTreeItems
& siblings
= parent
->GetChildren();
552 int index
= siblings
.Index(i
);
553 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
555 size_t n
= (size_t)(index
+ 1);
556 return n
== siblings
.Count() ? (wxGenericTreeItem
*)NULL
: siblings
[n
];
559 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
561 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
563 wxGenericTreeItem
*i
= item
.m_pItem
;
564 wxGenericTreeItem
*parent
= i
->GetParent();
565 if ( parent
== NULL
)
567 // root item doesn't have any siblings
571 wxArrayTreeItems
& siblings
= parent
->GetChildren();
572 int index
= siblings
.Index(i
);
573 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
575 return index
== 0 ? (wxGenericTreeItem
*)NULL
: siblings
[(size_t)(index
- 1)];
578 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
580 wxFAIL_MSG("not implemented");
585 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
587 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
589 wxFAIL_MSG("not implemented");
594 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
596 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
598 wxFAIL_MSG("not implemented");
603 // -----------------------------------------------------------------------------
605 // -----------------------------------------------------------------------------
607 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
609 const wxString
& text
,
610 int image
, int selImage
,
611 wxTreeItemData
*data
)
613 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
616 // should we give a warning here?
617 return AddRoot(text
, image
, selImage
, data
);
621 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
628 data
->m_pItem
= item
;
631 parent
->Insert( item
, previous
);
638 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
639 int image
, int selImage
,
640 wxTreeItemData
*data
)
642 wxCHECK_MSG( !m_anchor
, NULL
, "tree can have only one root" );
645 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
646 image
, selImage
, data
);
649 data
->m_pItem
= m_anchor
;
652 AdjustMyScrollbars();
658 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
659 const wxString
& text
,
660 int image
, int selImage
,
661 wxTreeItemData
*data
)
663 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
666 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
667 const wxTreeItemId
& idPrevious
,
668 const wxString
& text
,
669 int image
, int selImage
,
670 wxTreeItemData
*data
)
672 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
675 // should we give a warning here?
676 return AddRoot(text
, image
, selImage
, data
);
679 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
680 wxASSERT_MSG( index
!= NOT_FOUND
,
681 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
682 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
685 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
686 const wxString
& text
,
687 int image
, int selImage
,
688 wxTreeItemData
*data
)
690 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
693 // should we give a warning here?
694 return AddRoot(text
, image
, selImage
, data
);
697 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
698 image
, selImage
, data
);
701 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
703 wxGenericTreeItem
*item
= itemId
.m_pItem
;
704 item
->DeleteChildren();
709 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
711 wxGenericTreeItem
*item
= itemId
.m_pItem
;
712 wxGenericTreeItem
*parent
= item
->GetParent();
714 // notify the parent...
715 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
717 event
.SetEventObject( this );
718 ProcessEvent( event
);
722 parent
->GetChildren().Remove(item
);
730 void wxTreeCtrl::DeleteAllItems()
741 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
743 wxGenericTreeItem
*item
= itemId
.m_pItem
;
745 if ( !item
->HasPlus() )
748 if ( item
->IsExpanded() )
751 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
753 event
.SetEventObject( this );
754 if ( ProcessEvent( event
) && event
.m_code
)
756 // cancelled by program
762 RefreshSubtree(item
);
764 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
765 ProcessEvent( event
);
768 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
770 wxGenericTreeItem
*item
= itemId
.m_pItem
;
772 if ( !item
->IsExpanded() )
775 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
777 event
.SetEventObject( this );
778 if ( ProcessEvent( event
) && event
.m_code
)
780 // cancelled by program
786 wxArrayTreeItems
& children
= item
->GetChildren();
787 size_t count
= children
.Count();
788 for ( size_t n
= 0; n
< count
; n
++ )
790 Collapse(children
[n
]);
793 CalculatePositions();
795 RefreshSubtree(item
);
797 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
798 ProcessEvent( event
);
801 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
804 DeleteChildren(item
);
807 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
809 wxGenericTreeItem
*item
= itemId
.m_pItem
;
811 if ( item
->IsExpanded() )
817 void wxTreeCtrl::Unselect()
821 m_current
->SetHilight( FALSE
);
822 RefreshLine( m_current
);
826 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
828 wxGenericTreeItem
*item
= itemId
.m_pItem
;
830 if ( m_current
!= item
)
832 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
834 event
.m_itemOld
= m_current
;
835 event
.SetEventObject( this );
836 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
841 m_current
->SetHilight( FALSE
);
842 RefreshLine( m_current
);
846 m_current
->SetHilight( TRUE
);
847 RefreshLine( m_current
);
849 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
850 GetEventHandler()->ProcessEvent( event
);
854 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
856 wxGenericTreeItem
*gitem
= item
.m_pItem
;
858 int item_y
= gitem
->GetY();
862 ViewStart( &start_x
, &start_y
);
865 if (item_y
< start_y
+3)
869 m_anchor
->GetSize( x
, y
);
871 int x_pos
= GetScrollPos( wxHORIZONTAL
);
872 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, item_y
/10 );
878 GetClientSize( &w
, &h
);
880 if (item_y
> start_y
+h
-26)
884 m_anchor
->GetSize( x
, y
);
886 int x_pos
= GetScrollPos( wxHORIZONTAL
);
887 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-h
+30)/10 );
892 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
894 wxFAIL_MSG("not implemented");
897 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
898 wxClassInfo
* WXUNUSED(textCtrlClass
) )
900 wxFAIL_MSG("not implemented");
905 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
907 wxFAIL_MSG("not implemented");
912 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
914 wxFAIL_MSG("not implemented");
917 void wxTreeCtrl::SortChildren( const wxTreeItemId
& WXUNUSED(item
),
918 wxTreeItemCmpFunc
*WXUNUSED(cmpFunction
))
920 wxFAIL_MSG("not implemented");
923 wxImageList
*wxTreeCtrl::GetImageList() const
925 return m_imageListNormal
;
928 wxImageList
*wxTreeCtrl::GetStateImageList() const
930 return m_imageListState
;
933 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
935 m_imageListNormal
= imageList
;
938 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
940 m_imageListState
= imageList
;
943 // -----------------------------------------------------------------------------
945 // -----------------------------------------------------------------------------
946 void wxTreeCtrl::AdjustMyScrollbars()
952 m_anchor
->GetSize( x
, y
);
954 int x_pos
= GetScrollPos( wxHORIZONTAL
);
955 int y_pos
= GetScrollPos( wxVERTICAL
);
956 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
960 SetScrollbars( 0, 0, 0, 0 );
964 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
966 // render bold items in bold
970 if ( item
->IsBold() )
972 fontOld
= dc
.GetFont();
975 // @@ is there any better way to make a bold variant of old font?
976 fontNew
= wxFont( fontOld
.GetPointSize(),
980 fontOld
.GetUnderlined());
985 wxFAIL_MSG("wxDC::GetFont() failed!");
991 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
995 if (item
->GetImage() != -1)
997 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1001 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
1003 if (item
->GetImage() != -1)
1005 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1006 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1007 item
->GetX(), item
->GetY()-1,
1008 wxIMAGELIST_DRAW_TRANSPARENT
);
1009 dc
.DestroyClippingRegion();
1012 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY() );
1014 // restore normal font for bold items
1017 dc
.SetFont( fontOld
);
1021 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1023 int horizX
= level
*m_indent
;
1025 item
->SetX( horizX
+33 );
1026 item
->SetY( y
-m_lineHeight
/3 );
1027 item
->SetHeight( m_lineHeight
);
1029 item
->SetCross( horizX
+15, y
);
1033 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1034 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1036 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
1038 int startX
= horizX
;
1039 int endX
= horizX
+ 10;
1041 if (!item
->HasChildren()) endX
+= 20;
1043 dc
.DrawLine( startX
, y
, endX
, y
);
1045 if (item
->HasPlus())
1047 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
1048 dc
.SetPen( *wxGREY_PEN
);
1049 dc
.SetBrush( *wxWHITE_BRUSH
);
1050 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
1051 dc
.SetPen( *wxBLACK_PEN
);
1052 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
1054 if (!item
->IsExpanded())
1055 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
1058 if (item
->HasHilight())
1060 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1062 dc
.SetBrush( *m_hilightBrush
);
1065 dc
.SetPen( *wxBLACK_PEN
);
1067 dc
.SetPen( *wxTRANSPARENT_PEN
);
1069 PaintItem(item
, dc
);
1071 dc
.SetPen( *wxBLACK_PEN
);
1072 dc
.SetTextForeground( *wxBLACK
);
1073 dc
.SetBrush( *wxWHITE_BRUSH
);
1077 dc
.SetBrush( *wxWHITE_BRUSH
);
1078 dc
.SetPen( *wxTRANSPARENT_PEN
);
1080 PaintItem(item
, dc
);
1082 dc
.SetPen( *wxBLACK_PEN
);
1086 if ( item
->IsExpanded() )
1090 wxArrayTreeItems
& children
= item
->GetChildren();
1091 size_t count
= children
.Count();
1092 for ( size_t n
= 0; n
< count
; n
++ )
1097 PaintLevel( children
[n
], dc
, level
+1, y
);
1100 // it may happen that the item is expanded but has no items (when you
1101 // delete all its children for example) - don't draw the vertical line
1104 dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1108 // -----------------------------------------------------------------------------
1109 // wxWindows callbacks
1110 // -----------------------------------------------------------------------------
1112 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1120 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1122 dc
.SetPen( m_dottedPen
);
1123 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1125 int y
= m_lineHeight
/ 2 + 2;
1126 PaintLevel( m_anchor
, dc
, 0, y
);
1129 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1133 RefreshLine( m_current
);
1136 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1140 RefreshLine( m_current
);
1143 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1145 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1146 te
.m_code
= event
.KeyCode();
1147 te
.SetEventObject( this );
1148 GetEventHandler()->ProcessEvent( te
);
1156 switch (event
.KeyCode())
1160 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1168 if (IsExpanded(m_current
))
1170 Collapse(m_current
);
1182 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1183 event
.m_item
= m_current
;
1185 event
.SetEventObject( this );
1186 GetEventHandler()->ProcessEvent( event
);
1193 wxTreeItemId prev
= GetPrevSibling( m_current
);
1197 EnsureVisible( prev
);
1201 prev
= GetParent( m_current
);
1204 EnsureVisible( prev
);
1212 // this works the same as the down arrow except that we also expand the
1213 // item if it wasn't expanded yet
1219 if (IsExpanded(m_current
))
1222 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1223 SelectItem( child
);
1224 EnsureVisible( child
);
1228 wxTreeItemId next
= GetNextSibling( m_current
);
1231 wxTreeItemId current
= m_current
;
1232 while (current
&& !next
)
1234 current
= GetParent( current
);
1235 if (current
) next
= GetNextSibling( current
);
1241 EnsureVisible( next
);
1252 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& WXUNUSED(flags
))
1254 bool onButton
= FALSE
;
1255 return m_anchor
->HitTest( point
, onButton
);
1258 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1260 if ( !(event
.LeftDown() || event
.LeftDClick()) )
1266 wxClientDC
dc(this);
1268 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1269 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1271 bool onButton
= FALSE
;
1272 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1276 if (!IsSelected(item
)) SelectItem(item
);
1278 if ( event
.LeftDClick() )
1280 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1281 event
.m_item
= item
;
1283 event
.SetEventObject( this );
1284 GetEventHandler()->ProcessEvent( event
);
1293 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1295 if (!m_dirty
) return;
1299 CalculatePositions();
1301 AdjustMyScrollbars();
1304 // -----------------------------------------------------------------------------
1305 // -----------------------------------------------------------------------------
1306 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
,
1311 int horizX
= level
*m_indent
;
1313 item
->SetX( horizX
+33 );
1314 item
->SetY( y
-m_lineHeight
/3-2 );
1315 item
->SetHeight( m_lineHeight
);
1317 if ( item
->IsExpanded() )
1320 wxArrayTreeItems
& children
= item
->GetChildren();
1321 size_t count
= children
.Count();
1322 for ( size_t n
= 0; n
< count
; n
++ )
1325 CalculateLevel( children
[n
], dc
, level
+1, y
);
1329 void wxTreeCtrl::CalculatePositions()
1334 wxClientDC
dc(this);
1337 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1339 dc
.SetPen( m_dottedPen
);
1340 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1342 int y
= m_lineHeight
/ 2 + 2;
1343 CalculateLevel( m_anchor
, dc
, 0, y
);
1346 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1348 wxClientDC
dc(this);
1353 GetClientSize( &cw
, &ch
);
1356 rect
.x
= dc
.LogicalToDeviceX( 0 );
1358 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1361 Refresh( TRUE
, &rect
);
1363 AdjustMyScrollbars();
1366 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1368 wxClientDC
dc(this);
1372 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1373 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1375 rect
.height
= dc
.GetCharHeight() + 6;
1376 Refresh( TRUE
, &rect
);