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
,
332 long style
, const wxString
& name
)
336 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
338 SetBackgroundColour( *wxWHITE
);
339 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
344 wxTreeCtrl::~wxTreeCtrl()
346 wxDELETE( m_hilightBrush
);
347 wxDELETE( m_anchor
);
350 // -----------------------------------------------------------------------------
352 // -----------------------------------------------------------------------------
354 size_t wxTreeCtrl::GetCount() const
356 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
359 void wxTreeCtrl::SetIndent(unsigned int indent
)
365 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
367 wxCHECK_MSG( item
.IsOk(), 0u, "invalid tree item" );
369 return item
.m_pItem
->GetChildrenCount(recursively
);
372 // -----------------------------------------------------------------------------
373 // functions to work with tree items
374 // -----------------------------------------------------------------------------
376 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
378 wxCHECK_MSG( item
.IsOk(), "", "invalid tree item" );
380 return item
.m_pItem
->GetText();
383 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
385 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
387 return item
.m_pItem
->GetImage();
390 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
392 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
394 return item
.m_pItem
->GetSelectedImage();
397 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
399 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
401 return item
.m_pItem
->GetData();
404 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
406 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
409 item
.m_pItem
->SetText(text
, dc
);
412 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
414 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
416 item
.m_pItem
->SetImage(image
);
419 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
421 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
423 item
.m_pItem
->SetSelectedImage(image
);
426 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
428 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
430 item
.m_pItem
->SetData(data
);
433 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
435 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
437 item
.m_pItem
->SetHasPlus(has
);
440 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
442 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
444 // avoid redrawing the tree if no real change
445 wxGenericTreeItem
*pItem
= item
.m_pItem
;
446 if ( pItem
->IsBold() != bold
)
448 pItem
->SetBold(bold
);
453 // -----------------------------------------------------------------------------
454 // item status inquiries
455 // -----------------------------------------------------------------------------
457 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
459 wxFAIL_MSG("not implemented");
464 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
466 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
468 return !item
.m_pItem
->GetChildren().IsEmpty();
471 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
473 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
475 return item
.m_pItem
->IsExpanded();
478 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
480 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
482 return item
.m_pItem
->HasHilight();
485 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
487 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
489 return item
.m_pItem
->IsBold();
492 // -----------------------------------------------------------------------------
494 // -----------------------------------------------------------------------------
496 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
498 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
500 return item
.m_pItem
->GetParent();
503 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
505 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
508 return GetNextChild(item
, cookie
);
511 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
513 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
515 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
516 if ( (size_t)cookie
< children
.Count() )
518 return item
.m_pItem
->GetChildren().Item(cookie
++);
522 // there are no more of them
527 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
529 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
531 wxGenericTreeItem
*i
= item
.m_pItem
;
532 wxGenericTreeItem
*parent
= i
->GetParent();
533 if ( parent
== NULL
)
535 // root item doesn't have any siblings
539 wxArrayTreeItems
& siblings
= parent
->GetChildren();
540 int index
= siblings
.Index(i
);
541 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
543 size_t n
= (size_t)(index
+ 1);
544 return n
== siblings
.Count() ? (wxGenericTreeItem
*)NULL
: siblings
[n
];
547 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
549 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
551 wxGenericTreeItem
*i
= item
.m_pItem
;
552 wxGenericTreeItem
*parent
= i
->GetParent();
553 if ( parent
== NULL
)
555 // root item doesn't have any siblings
559 wxArrayTreeItems
& siblings
= parent
->GetChildren();
560 int index
= siblings
.Index(i
);
561 wxASSERT( index
!= NOT_FOUND
); // I'm not a child of my parent?
563 return index
== 0 ? (wxGenericTreeItem
*)NULL
: siblings
[(size_t)(index
- 1)];
566 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
568 wxFAIL_MSG("not implemented");
573 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
575 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
577 wxFAIL_MSG("not implemented");
582 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
584 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
586 wxFAIL_MSG("not implemented");
591 // -----------------------------------------------------------------------------
593 // -----------------------------------------------------------------------------
595 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
597 const wxString
& text
,
598 int image
, int selImage
,
599 wxTreeItemData
*data
)
601 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
604 // should we give a warning here?
605 return AddRoot(text
, image
, selImage
, data
);
609 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
616 data
->m_pItem
= item
;
619 parent
->Insert( item
, previous
);
626 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
627 int image
, int selImage
,
628 wxTreeItemData
*data
)
630 wxCHECK_MSG( !m_anchor
, NULL
, "tree can have only one root" );
633 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
634 image
, selImage
, data
);
637 data
->m_pItem
= m_anchor
;
640 AdjustMyScrollbars();
646 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
647 const wxString
& text
,
648 int image
, int selImage
,
649 wxTreeItemData
*data
)
651 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
654 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
655 const wxTreeItemId
& idPrevious
,
656 const wxString
& text
,
657 int image
, int selImage
,
658 wxTreeItemData
*data
)
660 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
663 // should we give a warning here?
664 return AddRoot(text
, image
, selImage
, data
);
667 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
668 wxASSERT_MSG( index
!= NOT_FOUND
,
669 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
670 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
673 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
674 const wxString
& text
,
675 int image
, int selImage
,
676 wxTreeItemData
*data
)
678 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
681 // should we give a warning here?
682 return AddRoot(text
, image
, selImage
, data
);
685 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
686 image
, selImage
, data
);
689 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
691 wxGenericTreeItem
*item
= itemId
.m_pItem
;
692 wxGenericTreeItem
*parent
= item
->GetParent();
696 parent
->GetChildren().Remove(item
);
704 void wxTreeCtrl::DeleteAllItems()
715 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
717 wxGenericTreeItem
*item
= itemId
.m_pItem
;
719 if ( !item
->HasPlus() )
722 if ( item
->IsExpanded() )
725 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
727 event
.SetEventObject( this );
728 if ( ProcessEvent( event
) && event
.m_code
)
730 // cancelled by program
736 RefreshSubtree(item
);
738 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
739 ProcessEvent( event
);
742 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
744 wxGenericTreeItem
*item
= itemId
.m_pItem
;
746 if ( !item
->IsExpanded() )
749 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
751 event
.SetEventObject( this );
752 if ( ProcessEvent( event
) && event
.m_code
)
754 // cancelled by program
760 wxArrayTreeItems
& children
= item
->GetChildren();
761 size_t count
= children
.Count();
762 for ( size_t n
= 0; n
< count
; n
++ )
764 Collapse(children
[n
]);
767 CalculatePositions();
769 RefreshSubtree(item
);
771 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
772 ProcessEvent( event
);
775 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
781 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
783 wxGenericTreeItem
*item
= itemId
.m_pItem
;
785 if ( item
->IsExpanded() )
791 void wxTreeCtrl::Unselect()
795 m_current
->SetHilight( FALSE
);
796 RefreshLine( m_current
);
800 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
802 wxGenericTreeItem
*item
= itemId
.m_pItem
;
804 if ( m_current
!= item
)
806 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
808 event
.m_itemOld
= m_current
;
809 event
.SetEventObject( this );
810 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
815 m_current
->SetHilight( FALSE
);
816 RefreshLine( m_current
);
820 m_current
->SetHilight( TRUE
);
821 RefreshLine( m_current
);
823 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
824 GetEventHandler()->ProcessEvent( event
);
828 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
830 wxGenericTreeItem
*gitem
= item
.m_pItem
;
832 int item_y
= gitem
->GetY();
836 ViewStart( &start_x
, &start_y
);
839 if (item_y
< start_y
+3)
843 m_anchor
->GetSize( x
, y
);
845 int x_pos
= GetScrollPos( wxHORIZONTAL
);
846 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, item_y
/10 );
852 GetClientSize( &w
, &h
);
854 if (item_y
> start_y
+h
-26)
858 m_anchor
->GetSize( x
, y
);
860 int x_pos
= GetScrollPos( wxHORIZONTAL
);
861 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-h
+30)/10 );
866 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
868 wxFAIL_MSG("not implemented");
871 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
872 wxClassInfo
* WXUNUSED(textCtrlClass
) )
874 wxFAIL_MSG("not implemented");
879 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
881 wxFAIL_MSG("not implemented");
886 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
888 wxFAIL_MSG("not implemented");
891 void wxTreeCtrl::SortChildren( const wxTreeItemId
& WXUNUSED(item
),
892 wxTreeItemCmpFunc
*WXUNUSED(cmpFunction
))
894 wxFAIL_MSG("not implemented");
897 wxImageList
*wxTreeCtrl::GetImageList() const
899 return m_imageListNormal
;
902 wxImageList
*wxTreeCtrl::GetStateImageList() const
904 return m_imageListState
;
907 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
909 m_imageListNormal
= imageList
;
912 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
914 m_imageListState
= imageList
;
917 // -----------------------------------------------------------------------------
919 // -----------------------------------------------------------------------------
920 void wxTreeCtrl::AdjustMyScrollbars()
926 m_anchor
->GetSize( x
, y
);
928 int x_pos
= GetScrollPos( wxHORIZONTAL
);
929 int y_pos
= GetScrollPos( wxVERTICAL
);
930 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
934 SetScrollbars( 0, 0, 0, 0 );
938 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
940 // render bold items in bold
944 if ( item
->IsBold() )
946 fontOld
= dc
.GetFont();
949 // @@ is there any better way to make a bold variant of old font?
950 fontNew
= wxFont( fontOld
.GetPointSize(),
954 fontOld
.GetUnderlined());
959 wxFAIL_MSG("wxDC::GetFont() failed!");
965 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
969 if (item
->GetImage() != -1)
971 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
975 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
977 if (item
->GetImage() != -1)
979 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
980 m_imageListNormal
->Draw( item
->GetImage(), dc
,
981 item
->GetX(), item
->GetY()-1,
982 wxIMAGELIST_DRAW_TRANSPARENT
);
983 dc
.DestroyClippingRegion();
986 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY() );
988 // restore normal font for bold items
991 dc
.SetFont( fontOld
);
995 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
997 int horizX
= level
*m_indent
;
999 item
->SetX( horizX
+33 );
1000 item
->SetY( y
-m_lineHeight
/3 );
1001 item
->SetHeight( m_lineHeight
);
1003 item
->SetCross( horizX
+15, y
);
1007 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1008 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1010 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
1012 int startX
= horizX
;
1013 int endX
= horizX
+ 10;
1015 if (!item
->HasChildren()) endX
+= 20;
1017 dc
.DrawLine( startX
, y
, endX
, y
);
1019 if (item
->HasPlus())
1021 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
1022 dc
.SetPen( *wxGREY_PEN
);
1023 dc
.SetBrush( *wxWHITE_BRUSH
);
1024 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
1025 dc
.SetPen( *wxBLACK_PEN
);
1026 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
1028 if (!item
->IsExpanded())
1029 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
1032 if (item
->HasHilight())
1034 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1036 dc
.SetBrush( *m_hilightBrush
);
1039 dc
.SetPen( *wxBLACK_PEN
);
1041 dc
.SetPen( *wxTRANSPARENT_PEN
);
1043 PaintItem(item
, dc
);
1045 dc
.SetPen( *wxBLACK_PEN
);
1046 dc
.SetTextForeground( *wxBLACK
);
1047 dc
.SetBrush( *wxWHITE_BRUSH
);
1051 dc
.SetBrush( *wxWHITE_BRUSH
);
1052 dc
.SetPen( *wxTRANSPARENT_PEN
);
1054 PaintItem(item
, dc
);
1056 dc
.SetPen( *wxBLACK_PEN
);
1060 if ( !item
->IsExpanded() )
1065 wxArrayTreeItems
& children
= item
->GetChildren();
1066 size_t count
= children
.Count();
1067 for ( size_t n
= 0; n
< count
; n
++ )
1072 PaintLevel( children
[n
], dc
, level
+1, y
);
1075 dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1078 // -----------------------------------------------------------------------------
1079 // wxWindows callbacks
1080 // -----------------------------------------------------------------------------
1082 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1090 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1092 dc
.SetPen( m_dottedPen
);
1093 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1095 int y
= m_lineHeight
/ 2 + 2;
1096 PaintLevel( m_anchor
, dc
, 0, y
);
1099 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1103 RefreshLine( m_current
);
1106 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1110 RefreshLine( m_current
);
1113 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1115 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1116 te
.m_code
= event
.KeyCode();
1117 te
.SetEventObject( this );
1118 GetEventHandler()->ProcessEvent( te
);
1126 switch (event
.KeyCode())
1130 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1138 if (IsExpanded(m_current
))
1140 Collapse(m_current
);
1152 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1153 event
.m_item
= m_current
;
1155 event
.SetEventObject( this );
1156 GetEventHandler()->ProcessEvent( event
);
1163 wxTreeItemId prev
= GetPrevSibling( m_current
);
1167 EnsureVisible( prev
);
1171 prev
= GetParent( m_current
);
1174 EnsureVisible( prev
);
1182 // this works the same as the down arrow except that we also expand the
1183 // item if it wasn't expanded yet
1189 if (IsExpanded(m_current
))
1192 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1193 SelectItem( child
);
1194 EnsureVisible( child
);
1198 wxTreeItemId next
= GetNextSibling( m_current
);
1201 wxTreeItemId current
= m_current
;
1202 while (current
&& !next
)
1204 current
= GetParent( current
);
1205 if (current
) next
= GetNextSibling( current
);
1211 EnsureVisible( next
);
1222 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1224 if ( !(event
.LeftDown() || event
.LeftDClick()) )
1230 wxClientDC
dc(this);
1232 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1233 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1235 bool onButton
= FALSE
;
1236 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1240 if (!IsSelected(item
)) SelectItem(item
);
1242 if ( event
.LeftDClick() )
1244 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1245 event
.m_item
= item
;
1247 event
.SetEventObject( this );
1248 GetEventHandler()->ProcessEvent( event
);
1257 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1259 if (!m_dirty
) return;
1263 CalculatePositions();
1265 AdjustMyScrollbars();
1268 // -----------------------------------------------------------------------------
1269 // -----------------------------------------------------------------------------
1270 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
,
1275 int horizX
= level
*m_indent
;
1277 item
->SetX( horizX
+33 );
1278 item
->SetY( y
-m_lineHeight
/3-2 );
1279 item
->SetHeight( m_lineHeight
);
1281 if ( item
->IsExpanded() )
1284 wxArrayTreeItems
& children
= item
->GetChildren();
1285 size_t count
= children
.Count();
1286 for ( size_t n
= 0; n
< count
; n
++ )
1289 CalculateLevel( children
[n
], dc
, level
+1, y
);
1293 void wxTreeCtrl::CalculatePositions()
1298 wxClientDC
dc(this);
1301 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT
) );
1303 dc
.SetPen( m_dottedPen
);
1304 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1306 int y
= m_lineHeight
/ 2 + 2;
1307 CalculateLevel( m_anchor
, dc
, 0, y
);
1310 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1312 wxClientDC
dc(this);
1317 GetClientSize( &cw
, &ch
);
1320 rect
.x
= dc
.LogicalToDeviceX( 0 );
1322 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1325 Refresh( TRUE
, &rect
);
1327 AdjustMyScrollbars();
1330 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1332 wxClientDC
dc(this);
1336 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1337 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1339 rect
.height
= dc
.GetCharHeight() + 6;
1340 Refresh( TRUE
, &rect
);