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
; }
86 // get count of all children (and grand children if 'recursively')
87 size_t GetChildrenCount(bool recursively
= TRUE
) const;
89 void Insert(wxGenericTreeItem
*child
, size_t index
)
90 { m_children
.Insert(child
, index
); }
92 void SetCross( int x
, int y
);
93 void GetSize( int &x
, int &y
);
95 // return the item at given position (or NULL if no item), onButton is TRUE
96 // if the point belongs to the item's button, otherwise it lies on the
98 wxGenericTreeItem
*HitTest( const wxPoint
& point
, bool &onButton
);
100 void Expand() { m_isCollapsed
= FALSE
; }
101 void Collapse() { m_isCollapsed
= TRUE
; }
103 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
106 bool HasChildren() const { return !m_children
.IsEmpty(); }
107 bool HasHilight() const { return m_hasHilight
; }
108 bool IsExpanded() const { return !m_isCollapsed
; }
109 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
110 bool IsBold() const { return m_isBold
; }
118 wxTreeItemData
*m_data
;
120 // use bitfields to save size
121 int m_isCollapsed
:1;
122 int m_hasHilight
:1; // same as focused
123 int m_hasPlus
:1; // used for item which doesn't have
124 // children but still has a [+] button
125 int m_isBold
:1; // render the label in bold font
128 long m_height
, m_width
;
129 int m_xCross
, m_yCross
;
131 wxArrayTreeItems m_children
;
132 wxGenericTreeItem
*m_parent
;
135 // =============================================================================
137 // =============================================================================
139 // -----------------------------------------------------------------------------
141 // -----------------------------------------------------------------------------
143 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
145 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
146 : wxNotifyEvent( commandType
, id
)
149 m_itemOld
= (wxGenericTreeItem
*)NULL
;
152 // -----------------------------------------------------------------------------
154 // -----------------------------------------------------------------------------
156 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
157 const wxString
& text
,
159 int image
, int selImage
,
160 wxTreeItemData
*data
)
164 m_selImage
= selImage
;
167 m_xCross
= m_yCross
= 0;
171 m_isCollapsed
= TRUE
;
172 m_hasHilight
= FALSE
;
178 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
181 wxGenericTreeItem::~wxGenericTreeItem()
185 size_t count
= m_children
.Count();
186 for ( size_t n
= 0; n
< count
; n
++ )
187 delete m_children
[n
];
190 void wxGenericTreeItem::SetText( const wxString
&text
, wxDC
& dc
)
194 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
197 void wxGenericTreeItem::Reset()
204 m_height
= m_width
= 0;
211 m_isCollapsed
= TRUE
;
213 m_parent
= (wxGenericTreeItem
*)NULL
;
216 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
218 size_t count
= m_children
.Count();
222 size_t total
= count
;
223 for ( size_t n
= 0; n
< count
; n
++ )
225 total
+= m_children
[n
]->GetChildrenCount();
231 void wxGenericTreeItem::SetCross( int x
, int y
)
237 void wxGenericTreeItem::GetSize( int &x
, int &y
)
239 if ( y
< m_y
) y
= m_y
;
240 int width
= m_x
+ m_width
;
241 if (width
> x
) x
= width
;
245 size_t count
= m_children
.Count();
246 for ( size_t n
= 0; n
< count
; n
++ )
248 m_children
[n
]->GetSize( x
, y
);
253 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
256 if ((point
.y
> m_y
) && (point
.y
< m_y
+ m_height
))
259 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
260 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
261 (IsExpanded() || HasPlus()))
267 if ((point
.x
> m_x
) && (point
.x
< m_x
+m_width
))
277 size_t count
= m_children
.Count();
278 for ( size_t n
= 0; n
< count
; n
++ )
280 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, onButton
);
290 // -----------------------------------------------------------------------------
291 // wxTreeCtrl implementation
292 // -----------------------------------------------------------------------------
294 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
296 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
297 EVT_PAINT (wxTreeCtrl::OnPaint
)
298 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
299 EVT_CHAR (wxTreeCtrl::OnChar
)
300 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
301 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
302 EVT_IDLE (wxTreeCtrl::OnIdle
)
305 // -----------------------------------------------------------------------------
306 // construction/destruction
307 // -----------------------------------------------------------------------------
308 void wxTreeCtrl::Init()
311 m_anchor
= (wxGenericTreeItem
*) NULL
;
320 m_hilightBrush
= new wxBrush
322 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
327 m_imageListState
= (wxImageList
*) NULL
;
330 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
331 const wxPoint
& pos
, const wxSize
& size
,
333 const wxValidator
&validator
,
334 const wxString
& name
)
338 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
340 SetValidator( validator
);
342 SetBackgroundColour( *wxWHITE
);
343 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
348 wxTreeCtrl::~wxTreeCtrl()
350 wxDELETE( m_hilightBrush
);
351 wxDELETE( m_anchor
);
354 // -----------------------------------------------------------------------------
356 // -----------------------------------------------------------------------------
358 size_t wxTreeCtrl::GetCount() const
360 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
363 void wxTreeCtrl::SetIndent(unsigned int indent
)
369 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
371 wxCHECK_MSG( item
.IsOk(), 0u, "invalid tree item" );
373 return item
.m_pItem
->GetChildrenCount(recursively
);
376 // -----------------------------------------------------------------------------
377 // functions to work with tree items
378 // -----------------------------------------------------------------------------
380 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
382 wxCHECK_MSG( item
.IsOk(), "", "invalid tree item" );
384 return item
.m_pItem
->GetText();
387 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
389 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
391 return item
.m_pItem
->GetImage();
394 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
396 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
398 return item
.m_pItem
->GetSelectedImage();
401 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
403 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
405 return item
.m_pItem
->GetData();
408 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
410 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
413 item
.m_pItem
->SetText(text
, dc
);
416 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
418 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
420 item
.m_pItem
->SetImage(image
);
423 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
425 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
427 item
.m_pItem
->SetSelectedImage(image
);
430 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
432 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
434 item
.m_pItem
->SetData(data
);
437 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
439 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
441 item
.m_pItem
->SetHasPlus(has
);
444 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
446 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
448 // avoid redrawing the tree if no real change
449 wxGenericTreeItem
*pItem
= item
.m_pItem
;
450 if ( pItem
->IsBold() != bold
)
452 pItem
->SetBold(bold
);
457 // -----------------------------------------------------------------------------
458 // item status inquiries
459 // -----------------------------------------------------------------------------
461 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
463 wxFAIL_MSG("not implemented");
468 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
470 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
472 return !item
.m_pItem
->GetChildren().IsEmpty();
475 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
477 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
479 return item
.m_pItem
->IsExpanded();
482 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
484 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
486 return item
.m_pItem
->HasHilight();
489 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
491 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
493 return item
.m_pItem
->IsBold();
496 // -----------------------------------------------------------------------------
498 // -----------------------------------------------------------------------------
500 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
502 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
504 return item
.m_pItem
->GetParent();
507 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
509 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
512 return GetNextChild(item
, cookie
);
515 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
517 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
519 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
520 if ( (size_t)cookie
< children
.Count() )
522 return item
.m_pItem
->GetChildren().Item(cookie
++);
526 // there are no more of them
531 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
533 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
535 wxGenericTreeItem
*i
= item
.m_pItem
;
536 wxGenericTreeItem
*parent
= i
->GetParent();
537 if ( parent
== NULL
)
539 // root item doesn't have any siblings
543 wxArrayTreeItems
& siblings
= parent
->GetChildren();
544 int index
= siblings
.Index(i
);
545 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
547 size_t n
= (size_t)(index
+ 1);
548 return n
== siblings
.Count() ? (wxGenericTreeItem
*)NULL
: siblings
[n
];
551 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
553 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
555 wxGenericTreeItem
*i
= item
.m_pItem
;
556 wxGenericTreeItem
*parent
= i
->GetParent();
557 if ( parent
== NULL
)
559 // root item doesn't have any siblings
563 wxArrayTreeItems
& siblings
= parent
->GetChildren();
564 int index
= siblings
.Index(i
);
565 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
567 return index
== 0 ? (wxGenericTreeItem
*)NULL
: siblings
[(size_t)(index
- 1)];
570 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
572 wxFAIL_MSG("not implemented");
577 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
579 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
581 wxFAIL_MSG("not implemented");
586 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
588 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
590 wxFAIL_MSG("not implemented");
595 // -----------------------------------------------------------------------------
597 // -----------------------------------------------------------------------------
599 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
601 const wxString
& text
,
602 int image
, int selImage
,
603 wxTreeItemData
*data
)
605 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
608 // should we give a warning here?
609 return AddRoot(text
, image
, selImage
, data
);
613 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
620 data
->m_pItem
= item
;
623 parent
->Insert( item
, previous
);
630 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
631 int image
, int selImage
,
632 wxTreeItemData
*data
)
634 wxCHECK_MSG( !m_anchor
, NULL
, "tree can have only one root" );
637 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
638 image
, selImage
, data
);
641 data
->m_pItem
= m_anchor
;
644 AdjustMyScrollbars();
650 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
651 const wxString
& text
,
652 int image
, int selImage
,
653 wxTreeItemData
*data
)
655 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
658 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
659 const wxTreeItemId
& idPrevious
,
660 const wxString
& text
,
661 int image
, int selImage
,
662 wxTreeItemData
*data
)
664 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
667 // should we give a warning here?
668 return AddRoot(text
, image
, selImage
, data
);
671 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
672 wxASSERT_MSG( index
!= NOT_FOUND
,
673 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
674 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
677 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
678 const wxString
& text
,
679 int image
, int selImage
,
680 wxTreeItemData
*data
)
682 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
685 // should we give a warning here?
686 return AddRoot(text
, image
, selImage
, data
);
689 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
690 image
, selImage
, data
);
693 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
695 wxGenericTreeItem
*item
= itemId
.m_pItem
;
696 wxGenericTreeItem
*parent
= item
->GetParent();
700 parent
->GetChildren().Remove(item
);
708 void wxTreeCtrl::DeleteAllItems()
719 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
721 wxGenericTreeItem
*item
= itemId
.m_pItem
;
723 if ( !item
->HasPlus() )
726 if ( item
->IsExpanded() )
729 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
731 event
.SetEventObject( this );
732 if ( ProcessEvent( event
) && event
.m_code
)
734 // cancelled by program
740 RefreshSubtree(item
);
742 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
743 ProcessEvent( event
);
746 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
748 wxGenericTreeItem
*item
= itemId
.m_pItem
;
750 if ( !item
->IsExpanded() )
753 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
755 event
.SetEventObject( this );
756 if ( ProcessEvent( event
) && event
.m_code
)
758 // cancelled by program
764 wxArrayTreeItems
& children
= item
->GetChildren();
765 size_t count
= children
.Count();
766 for ( size_t n
= 0; n
< count
; n
++ )
768 Collapse(children
[n
]);
771 CalculatePositions();
773 RefreshSubtree(item
);
775 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
776 ProcessEvent( event
);
779 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
785 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
787 wxGenericTreeItem
*item
= itemId
.m_pItem
;
789 if ( item
->IsExpanded() )
795 void wxTreeCtrl::Unselect()
799 m_current
->SetHilight( FALSE
);
800 RefreshLine( m_current
);
804 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
806 wxGenericTreeItem
*item
= itemId
.m_pItem
;
808 if ( m_current
!= item
)
810 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
812 event
.m_itemOld
= m_current
;
813 event
.SetEventObject( this );
814 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
819 m_current
->SetHilight( FALSE
);
820 RefreshLine( m_current
);
824 m_current
->SetHilight( TRUE
);
825 RefreshLine( m_current
);
827 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
828 GetEventHandler()->ProcessEvent( event
);
832 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
834 wxGenericTreeItem
*gitem
= item
.m_pItem
;
836 int item_y
= gitem
->GetY();
840 ViewStart( &start_x
, &start_y
);
843 if (item_y
< start_y
+3)
847 m_anchor
->GetSize( x
, y
);
849 int x_pos
= GetScrollPos( wxHORIZONTAL
);
850 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, item_y
/10 );
856 GetClientSize( &w
, &h
);
858 if (item_y
> start_y
+h
-26)
862 m_anchor
->GetSize( x
, y
);
864 int x_pos
= GetScrollPos( wxHORIZONTAL
);
865 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-h
+30)/10 );
870 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
872 wxFAIL_MSG("not implemented");
875 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
876 wxClassInfo
* WXUNUSED(textCtrlClass
) )
878 wxFAIL_MSG("not implemented");
883 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
885 wxFAIL_MSG("not implemented");
890 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
892 wxFAIL_MSG("not implemented");
895 void wxTreeCtrl::SortChildren( const wxTreeItemId
& WXUNUSED(item
),
896 wxTreeItemCmpFunc
*WXUNUSED(cmpFunction
))
898 wxFAIL_MSG("not implemented");
901 wxImageList
*wxTreeCtrl::GetImageList() const
903 return m_imageListNormal
;
906 wxImageList
*wxTreeCtrl::GetStateImageList() const
908 return m_imageListState
;
911 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
913 m_imageListNormal
= imageList
;
916 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
918 m_imageListState
= imageList
;
921 // -----------------------------------------------------------------------------
923 // -----------------------------------------------------------------------------
924 void wxTreeCtrl::AdjustMyScrollbars()
930 m_anchor
->GetSize( x
, y
);
932 int x_pos
= GetScrollPos( wxHORIZONTAL
);
933 int y_pos
= GetScrollPos( wxVERTICAL
);
934 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
938 SetScrollbars( 0, 0, 0, 0 );
942 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
944 // render bold items in bold
948 if ( item
->IsBold() )
950 fontOld
= dc
.GetFont();
953 // @@ is there any better way to make a bold variant of old font?
954 fontNew
= wxFont( fontOld
.GetPointSize(),
958 fontOld
.GetUnderlined());
963 wxFAIL_MSG("wxDC::GetFont() failed!");
969 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
973 if (item
->GetImage() != -1)
975 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
979 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
981 if (item
->GetImage() != -1)
983 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
984 m_imageListNormal
->Draw( item
->GetImage(), dc
,
985 item
->GetX(), item
->GetY()-1,
986 wxIMAGELIST_DRAW_TRANSPARENT
);
987 dc
.DestroyClippingRegion();
990 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY() );
992 // restore normal font for bold items
995 dc
.SetFont( fontOld
);
999 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1001 int horizX
= level
*m_indent
;
1003 item
->SetX( horizX
+33 );
1004 item
->SetY( y
-m_lineHeight
/3 );
1005 item
->SetHeight( m_lineHeight
);
1007 item
->SetCross( horizX
+15, y
);
1011 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1012 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1014 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
1016 int startX
= horizX
;
1017 int endX
= horizX
+ 10;
1019 if (!item
->HasChildren()) endX
+= 20;
1021 dc
.DrawLine( startX
, y
, endX
, y
);
1023 if (item
->HasPlus())
1025 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
1026 dc
.SetPen( *wxGREY_PEN
);
1027 dc
.SetBrush( *wxWHITE_BRUSH
);
1028 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
1029 dc
.SetPen( *wxBLACK_PEN
);
1030 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
1032 if (!item
->IsExpanded())
1033 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
1036 if (item
->HasHilight())
1038 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1040 dc
.SetBrush( *m_hilightBrush
);
1043 dc
.SetPen( *wxBLACK_PEN
);
1045 dc
.SetPen( *wxTRANSPARENT_PEN
);
1047 PaintItem(item
, dc
);
1049 dc
.SetPen( *wxBLACK_PEN
);
1050 dc
.SetTextForeground( *wxBLACK
);
1051 dc
.SetBrush( *wxWHITE_BRUSH
);
1055 dc
.SetBrush( *wxWHITE_BRUSH
);
1056 dc
.SetPen( *wxTRANSPARENT_PEN
);
1058 PaintItem(item
, dc
);
1060 dc
.SetPen( *wxBLACK_PEN
);
1064 if ( !item
->IsExpanded() )
1069 wxArrayTreeItems
& children
= item
->GetChildren();
1070 size_t count
= children
.Count();
1071 for ( size_t n
= 0; n
< count
; n
++ )
1076 PaintLevel( children
[n
], dc
, level
+1, y
);
1079 dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1082 // -----------------------------------------------------------------------------
1083 // wxWindows callbacks
1084 // -----------------------------------------------------------------------------
1086 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1094 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1096 dc
.SetPen( m_dottedPen
);
1097 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1099 int y
= m_lineHeight
/ 2 + 2;
1100 PaintLevel( m_anchor
, dc
, 0, y
);
1103 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1107 RefreshLine( m_current
);
1110 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1114 RefreshLine( m_current
);
1117 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1119 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1120 te
.m_code
= event
.KeyCode();
1121 te
.SetEventObject( this );
1122 GetEventHandler()->ProcessEvent( te
);
1130 switch (event
.KeyCode())
1134 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1142 if (IsExpanded(m_current
))
1144 Collapse(m_current
);
1156 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1157 event
.m_item
= m_current
;
1159 event
.SetEventObject( this );
1160 GetEventHandler()->ProcessEvent( event
);
1167 wxTreeItemId prev
= GetPrevSibling( m_current
);
1171 EnsureVisible( prev
);
1175 prev
= GetParent( m_current
);
1178 EnsureVisible( prev
);
1186 // this works the same as the down arrow except that we also expand the
1187 // item if it wasn't expanded yet
1193 if (IsExpanded(m_current
))
1196 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1197 SelectItem( child
);
1198 EnsureVisible( child
);
1202 wxTreeItemId next
= GetNextSibling( m_current
);
1205 wxTreeItemId current
= m_current
;
1206 while (current
&& !next
)
1208 current
= GetParent( current
);
1209 if (current
) next
= GetNextSibling( current
);
1215 EnsureVisible( next
);
1226 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& WXUNUSED(flags
))
1228 bool onButton
= FALSE
;
1229 return m_anchor
->HitTest( point
, onButton
);
1232 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1234 if ( !(event
.LeftDown() || event
.LeftDClick()) )
1240 wxClientDC
dc(this);
1242 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1243 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1245 bool onButton
= FALSE
;
1246 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1250 if (!IsSelected(item
)) SelectItem(item
);
1252 if ( event
.LeftDClick() )
1254 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1255 event
.m_item
= item
;
1257 event
.SetEventObject( this );
1258 GetEventHandler()->ProcessEvent( event
);
1267 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1269 if (!m_dirty
) return;
1273 CalculatePositions();
1275 AdjustMyScrollbars();
1278 // -----------------------------------------------------------------------------
1279 // -----------------------------------------------------------------------------
1280 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
,
1285 int horizX
= level
*m_indent
;
1287 item
->SetX( horizX
+33 );
1288 item
->SetY( y
-m_lineHeight
/3-2 );
1289 item
->SetHeight( m_lineHeight
);
1291 if ( item
->IsExpanded() )
1294 wxArrayTreeItems
& children
= item
->GetChildren();
1295 size_t count
= children
.Count();
1296 for ( size_t n
= 0; n
< count
; n
++ )
1299 CalculateLevel( children
[n
], dc
, level
+1, y
);
1303 void wxTreeCtrl::CalculatePositions()
1308 wxClientDC
dc(this);
1311 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1313 dc
.SetPen( m_dottedPen
);
1314 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1316 int y
= m_lineHeight
/ 2 + 2;
1317 CalculateLevel( m_anchor
, dc
, 0, y
);
1320 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1322 wxClientDC
dc(this);
1327 GetClientSize( &cw
, &ch
);
1330 rect
.x
= dc
.LogicalToDeviceX( 0 );
1332 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1335 Refresh( TRUE
, &rect
);
1337 AdjustMyScrollbars();
1340 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1342 wxClientDC
dc(this);
1346 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1347 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1349 rect
.height
= dc
.GetCharHeight() + 6;
1350 Refresh( TRUE
, &rect
);