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 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #include "wx/generic/treectrl.h"
32 #include "wx/generic/imaglist.h"
33 #include "wx/settings.h"
36 #include "wx/dynarray.h"
37 #include "wx/arrimpl.cpp"
38 #include "wx/dcclient.h"
39 #include "wx/msgdlg.h"
41 // -----------------------------------------------------------------------------
43 // -----------------------------------------------------------------------------
45 class WXDLLEXPORT wxGenericTreeItem
;
47 WX_DEFINE_ARRAY(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
48 WX_DEFINE_OBJARRAY(wxArrayTreeItemIds
);
50 // -----------------------------------------------------------------------------
52 // -----------------------------------------------------------------------------
55 class WXDLLEXPORT wxGenericTreeItem
59 wxGenericTreeItem() { m_data
= NULL
; }
60 wxGenericTreeItem( wxGenericTreeItem
*parent
,
63 int image
, int selImage
,
64 wxTreeItemData
*data
);
69 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
71 const wxString
& GetText() const { return m_text
; }
72 int GetImage() const { return m_image
; }
73 int GetSelectedImage() const { return m_selImage
; }
74 wxTreeItemData
*GetData() const { return m_data
; }
76 void SetText( const wxString
&text
);
77 void SetImage(int image
) { m_image
= image
; }
78 void SetSelectedImage(int image
) { m_selImage
= image
; }
79 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
81 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
83 void SetBold(bool bold
) { m_isBold
= bold
; }
85 int GetX() const { return m_x
; }
86 int GetY() const { return m_y
; }
88 void SetX(int x
) { m_x
= x
; }
89 void SetY(int y
) { m_y
= y
; }
91 int GetHeight() const { return m_height
; }
92 int GetWidth() const { return m_width
; }
94 void SetHeight(int h
) { m_height
= h
; }
95 void SetWidth(int w
) { m_width
= w
; }
98 wxGenericTreeItem
*GetParent() const { return m_parent
; }
101 // deletes all children notifying the treectrl about it if !NULL pointer
103 void DeleteChildren(wxTreeCtrl
*tree
= NULL
);
104 // FIXME don't know what is it for
107 // get count of all children (and grand children if 'recursively')
108 size_t GetChildrenCount(bool recursively
= TRUE
) const;
110 void Insert(wxGenericTreeItem
*child
, size_t index
)
111 { m_children
.Insert(child
, index
); }
113 void SetCross( int x
, int y
);
114 void GetSize( int &x
, int &y
, const wxTreeCtrl
* );
116 // return the item at given position (or NULL if no item), onButton is TRUE
117 // if the point belongs to the item's button, otherwise it lies on the
119 wxGenericTreeItem
*HitTest( const wxPoint
& point
, const wxTreeCtrl
*, int &flags
);
121 void Expand() { m_isCollapsed
= FALSE
; }
122 void Collapse() { m_isCollapsed
= TRUE
; }
124 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
127 bool HasChildren() const { return !m_children
.IsEmpty(); }
128 bool HasHilight() const { return m_hasHilight
; }
129 bool IsExpanded() const { return !m_isCollapsed
; }
130 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
131 bool IsBold() const { return m_isBold
; }
139 wxTreeItemData
*m_data
;
141 // use bitfields to save size
142 int m_isCollapsed
:1;
143 int m_hasHilight
:1; // same as focused
144 int m_hasPlus
:1; // used for item which doesn't have
145 // children but still has a [+] button
146 int m_isBold
:1; // render the label in bold font
149 long m_height
, m_width
;
150 int m_xCross
, m_yCross
;
152 wxArrayGenericTreeItems m_children
;
153 wxGenericTreeItem
*m_parent
;
156 // =============================================================================
158 // =============================================================================
160 #define PIXELS_PER_UNIT 10
161 // -----------------------------------------------------------------------------
163 // -----------------------------------------------------------------------------
165 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
167 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
168 : wxNotifyEvent( commandType
, id
)
171 m_itemOld
= (wxGenericTreeItem
*)NULL
;
174 // -----------------------------------------------------------------------------
176 // -----------------------------------------------------------------------------
178 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
179 const wxString
& text
,
181 int image
, int selImage
,
182 wxTreeItemData
*data
)
186 m_selImage
= selImage
;
189 m_xCross
= m_yCross
= 0;
193 m_isCollapsed
= TRUE
;
194 m_hasHilight
= FALSE
;
200 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
201 // TODO : Add the width of the image
202 // PB : We don't know which image is shown (image, selImage)
203 // We don't even know imageList from the treectrl this item belongs to !!!
204 // At this point m_width doesn't mean much, this can be remove !
207 wxGenericTreeItem::~wxGenericTreeItem()
211 wxASSERT_MSG( m_children
.IsEmpty(),
212 _T("please call DeleteChildren() before deleting the item") );
215 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
217 size_t count
= m_children
.Count();
218 for ( size_t n
= 0; n
< count
; n
++ )
220 wxGenericTreeItem
*child
= m_children
[n
];
223 tree
->SendDeleteEvent(child
);
226 child
->DeleteChildren(tree
);
233 void wxGenericTreeItem::SetText( const wxString
&text
)
238 void wxGenericTreeItem::Reset()
245 m_height
= m_width
= 0;
252 m_isCollapsed
= TRUE
;
254 m_parent
= (wxGenericTreeItem
*)NULL
;
257 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
259 size_t count
= m_children
.Count();
263 size_t total
= count
;
264 for ( size_t n
= 0; n
< count
; ++n
)
266 total
+= m_children
[n
]->GetChildrenCount();
272 void wxGenericTreeItem::SetCross( int x
, int y
)
278 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxTreeCtrl
*theTree
)
280 int bottomY
=m_y
+theTree
->GetLineHeight(this);
281 if ( y
< bottomY
) y
= bottomY
;
282 int width
= m_x
+ m_width
;
283 if ( x
< width
) x
= width
;
287 size_t count
= m_children
.Count();
288 for ( size_t n
= 0; n
< count
; ++n
)
290 m_children
[n
]->GetSize( x
, y
, theTree
);
295 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
296 const wxTreeCtrl
*theTree
,
299 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
301 if (point
.y
<m_y
+theTree
->GetLineHeight(this)/2) flags
|=wxTREE_HITTEST_ONITEMUPPERPART
;
302 else flags
|=wxTREE_HITTEST_ONITEMLOWERPART
;
305 // Because that is the size of the plus sign, RR
306 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
307 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
308 (IsExpanded() || HasPlus()))
310 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
314 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
319 // assuming every image (normal and selected ) has the same size !
320 if ((m_image
!=-1) && theTree
->m_imageListNormal
)
321 theTree
->m_imageListNormal
->GetSize(m_image
, image_w
, image_h
);
323 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
324 flags
|=wxTREE_HITTEST_ONITEMICON
;
326 flags
|=wxTREE_HITTEST_ONITEMLABEL
;
331 if (point
.x
< m_x
) flags
|=wxTREE_HITTEST_ONITEMIDENT
;
332 if (point
.x
> m_x
+m_width
) flags
|=wxTREE_HITTEST_ONITEMRIGHT
;
340 size_t count
= m_children
.Count();
341 for ( size_t n
= 0; n
< count
; n
++ )
343 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
350 flags
|=wxTREE_HITTEST_NOWHERE
;
354 // -----------------------------------------------------------------------------
355 // wxTreeCtrl implementation
356 // -----------------------------------------------------------------------------
358 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
360 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
361 EVT_PAINT (wxTreeCtrl::OnPaint
)
362 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
363 EVT_CHAR (wxTreeCtrl::OnChar
)
364 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
365 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
366 EVT_IDLE (wxTreeCtrl::OnIdle
)
369 // -----------------------------------------------------------------------------
370 // construction/destruction
371 // -----------------------------------------------------------------------------
373 void wxTreeCtrl::Init()
377 m_anchor
= (wxGenericTreeItem
*) NULL
;
387 m_hilightBrush
= new wxBrush
389 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
394 m_imageListState
= (wxImageList
*) NULL
;
399 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
400 const wxPoint
& pos
, const wxSize
& size
,
402 const wxValidator
&validator
,
403 const wxString
& name
)
407 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
410 SetValidator( validator
);
413 SetBackgroundColour( *wxWHITE
);
414 // m_dottedPen = wxPen( "grey", 0, wxDOT );
415 m_dottedPen
= wxPen( "grey", 0, 0 );
420 wxTreeCtrl::~wxTreeCtrl()
422 wxDELETE( m_hilightBrush
);
427 // -----------------------------------------------------------------------------
429 // -----------------------------------------------------------------------------
431 size_t wxTreeCtrl::GetCount() const
433 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
436 void wxTreeCtrl::SetIndent(unsigned int indent
)
443 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
450 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
452 wxCHECK_MSG( item
.IsOk(), 0u, _T("invalid tree item") );
454 return item
.m_pItem
->GetChildrenCount(recursively
);
457 // -----------------------------------------------------------------------------
458 // functions to work with tree items
459 // -----------------------------------------------------------------------------
461 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
463 wxCHECK_MSG( item
.IsOk(), _T(""), _T("invalid tree item") );
465 return item
.m_pItem
->GetText();
468 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
470 wxCHECK_MSG( item
.IsOk(), -1, _T("invalid tree item") );
472 return item
.m_pItem
->GetImage();
475 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
477 wxCHECK_MSG( item
.IsOk(), -1, _T("invalid tree item") );
479 return item
.m_pItem
->GetSelectedImage();
482 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
484 wxCHECK_MSG( item
.IsOk(), NULL
, _T("invalid tree item") );
486 return item
.m_pItem
->GetData();
489 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
491 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
494 wxGenericTreeItem
*pItem
= item
.m_pItem
;
495 pItem
->SetText(text
);
496 CalculateSize(pItem
, dc
);
500 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
502 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
505 wxGenericTreeItem
*pItem
= item
.m_pItem
;
506 pItem
->SetImage(image
);
507 CalculateSize(pItem
, dc
);
511 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
513 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
516 wxGenericTreeItem
*pItem
= item
.m_pItem
;
517 pItem
->SetSelectedImage(image
);
518 CalculateSize(pItem
, dc
);
522 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
524 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
526 item
.m_pItem
->SetData(data
);
529 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
531 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
533 wxGenericTreeItem
*pItem
= item
.m_pItem
;
534 pItem
->SetHasPlus(has
);
538 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
540 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
542 // avoid redrawing the tree if no real change
543 wxGenericTreeItem
*pItem
= item
.m_pItem
;
544 if ( pItem
->IsBold() != bold
)
546 pItem
->SetBold(bold
);
551 // -----------------------------------------------------------------------------
552 // item status inquiries
553 // -----------------------------------------------------------------------------
555 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
557 wxFAIL_MSG(_T("not implemented"));
562 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
564 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
566 return !item
.m_pItem
->GetChildren().IsEmpty();
569 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
571 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
573 return item
.m_pItem
->IsExpanded();
576 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
578 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
580 return item
.m_pItem
->HasHilight();
583 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
585 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
587 return item
.m_pItem
->IsBold();
590 // -----------------------------------------------------------------------------
592 // -----------------------------------------------------------------------------
594 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
596 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
598 return item
.m_pItem
->GetParent();
601 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
603 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
606 return GetNextChild(item
, cookie
);
609 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
611 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
613 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
614 if ( (size_t)cookie
< children
.Count() )
616 return children
.Item(cookie
++);
620 // there are no more of them
621 return wxTreeItemId();
625 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
627 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
629 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
630 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
633 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
635 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
637 wxGenericTreeItem
*i
= item
.m_pItem
;
638 wxGenericTreeItem
*parent
= i
->GetParent();
639 if ( parent
== NULL
)
641 // root item doesn't have any siblings
642 return wxTreeItemId();
645 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
646 int index
= siblings
.Index(i
);
647 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
649 size_t n
= (size_t)(index
+ 1);
650 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
653 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
655 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
657 wxGenericTreeItem
*i
= item
.m_pItem
;
658 wxGenericTreeItem
*parent
= i
->GetParent();
659 if ( parent
== NULL
)
661 // root item doesn't have any siblings
662 return wxTreeItemId();
665 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
666 int index
= siblings
.Index(i
);
667 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
669 return index
== 0 ? wxTreeItemId()
670 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
673 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
675 wxFAIL_MSG(_T("not implemented"));
677 return wxTreeItemId();
680 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
682 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
684 wxFAIL_MSG(_T("not implemented"));
686 return wxTreeItemId();
689 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
691 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
693 wxFAIL_MSG(_T("not implemented"));
695 return wxTreeItemId();
698 // -----------------------------------------------------------------------------
700 // -----------------------------------------------------------------------------
702 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
704 const wxString
& text
,
705 int image
, int selImage
,
706 wxTreeItemData
*data
)
708 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
711 // should we give a warning here?
712 return AddRoot(text
, image
, selImage
, data
);
716 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
723 data
->m_pItem
= item
;
726 parent
->Insert( item
, previous
);
733 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
734 int image
, int selImage
,
735 wxTreeItemData
*data
)
737 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), _T("tree can have only one root") );
740 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
741 image
, selImage
, data
);
744 data
->m_pItem
= m_anchor
;
748 AdjustMyScrollbars();
753 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
754 const wxString
& text
,
755 int image
, int selImage
,
756 wxTreeItemData
*data
)
758 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
761 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
762 const wxTreeItemId
& idPrevious
,
763 const wxString
& text
,
764 int image
, int selImage
,
765 wxTreeItemData
*data
)
767 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
770 // should we give a warning here?
771 return AddRoot(text
, image
, selImage
, data
);
774 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
775 wxASSERT_MSG( index
!= wxNOT_FOUND
,
776 _T("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
777 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
780 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
781 const wxString
& text
,
782 int image
, int selImage
,
783 wxTreeItemData
*data
)
785 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
788 // should we give a warning here?
789 return AddRoot(text
, image
, selImage
, data
);
792 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
793 image
, selImage
, data
);
796 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
798 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
800 event
.SetEventObject( this );
801 ProcessEvent( event
);
804 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
806 wxGenericTreeItem
*item
= itemId
.m_pItem
;
807 item
->DeleteChildren(this);
812 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
814 wxGenericTreeItem
*item
= itemId
.m_pItem
;
815 wxGenericTreeItem
*parent
= item
->GetParent();
819 parent
->GetChildren().Remove(item
);
822 item
->DeleteChildren(this);
823 SendDeleteEvent(item
);
829 void wxTreeCtrl::DeleteAllItems()
833 m_anchor
->DeleteChildren(this);
842 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
844 wxGenericTreeItem
*item
= itemId
.m_pItem
;
846 if ( !item
->HasPlus() )
849 if ( item
->IsExpanded() )
852 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
854 event
.SetEventObject( this );
855 if ( ProcessEvent( event
) && event
.m_code
)
857 // cancelled by program
862 CalculatePositions();
864 RefreshSubtree(item
);
866 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
867 ProcessEvent( event
);
870 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
872 wxGenericTreeItem
*item
= itemId
.m_pItem
;
874 if ( !item
->IsExpanded() )
877 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
879 event
.SetEventObject( this );
880 if ( ProcessEvent( event
) && event
.m_code
)
882 // cancelled by program
888 wxArrayGenericTreeItems
& children
= item
->GetChildren();
889 size_t count
= children
.Count();
890 for ( size_t n
= 0; n
< count
; n
++ )
892 Collapse(children
[n
]);
895 CalculatePositions();
897 RefreshSubtree(item
);
899 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
900 ProcessEvent( event
);
903 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
906 DeleteChildren(item
);
909 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
911 wxGenericTreeItem
*item
= itemId
.m_pItem
;
913 if ( item
->IsExpanded() )
919 void wxTreeCtrl::Unselect()
923 m_current
->SetHilight( FALSE
);
924 RefreshLine( m_current
);
928 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
930 item
->SetHilight(FALSE
);
933 if (item
->HasChildren())
935 wxArrayGenericTreeItems
& children
= item
->GetChildren();
936 size_t count
= children
.Count();
937 for ( size_t n
= 0; n
< count
; ++n
)
938 UnselectAllChildren(children
[n
]);
942 void wxTreeCtrl::UnselectAll()
944 UnselectAllChildren(GetRootItem().m_pItem
);
947 // Recursive function !
948 // To stop we must have crt_item<last_item
950 // Tag all next children, when no more children,
951 // Move to parent (not to tag)
952 // Keep going... if we found last_item, we stop.
953 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
955 wxGenericTreeItem
*parent
= crt_item
->GetParent();
957 if ( parent
== NULL
) // This is root item
958 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
960 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
961 int index
= children
.Index(crt_item
);
962 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
964 size_t count
= children
.Count();
965 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
966 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
968 return TagNextChildren(parent
, last_item
, select
);
971 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
973 crt_item
->SetHilight(select
);
974 RefreshLine(crt_item
);
976 if (crt_item
==last_item
) return TRUE
;
978 if (crt_item
->HasChildren())
980 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
981 size_t count
= children
.Count();
982 for ( size_t n
= 0; n
< count
; ++n
)
983 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
989 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
991 // item2 is not necessary after item1
992 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
994 // choice first' and 'last' between item1 and item2
995 if (item1
->GetY()<item2
->GetY())
1006 bool select
=m_current
->HasHilight();
1008 if (TagAllChildrenUntilLast(first
,last
,select
)) return;
1010 TagNextChildren(first
,last
,select
);
1013 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1014 bool unselect_others
,
1015 bool extended_select
)
1017 wxCHECK_RET( itemId
.IsOk(), _T("invalid tree item") );
1019 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1021 //wxCHECK_RET( ( (!unselect_others) && is_single),
1022 // _T("this is a single selection tree") );
1024 // to keep going anyhow !!!
1027 unselect_others
=TRUE
;
1028 extended_select
=FALSE
;
1031 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1033 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1034 event
.m_item
= item
;
1035 event
.m_itemOld
= m_current
;
1036 event
.SetEventObject( this );
1037 // TODO : Here we don't send any selection mode yet !
1039 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
1043 if (unselect_others
)
1045 if (is_single
) Unselect(); // to speed up thing
1050 if (extended_select
)
1052 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1053 // don't change the mark (m_current)
1054 SelectItemRange(m_current
, item
);
1058 bool select
=TRUE
; // the default
1060 // Check if we need to toggle hilight (ctrl mode)
1061 if (!unselect_others
)
1062 select
=!item
->HasHilight();
1064 m_current
= m_key_current
= item
;
1065 m_current
->SetHilight(select
);
1066 RefreshLine( m_current
);
1069 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1070 GetEventHandler()->ProcessEvent( event
);
1073 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
, wxArrayTreeItemIds
&array
) const
1075 if (item
->HasHilight()) array
.Add(wxTreeItemId(item
));
1077 if (item
->HasChildren())
1079 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1080 size_t count
= children
.Count();
1081 for ( size_t n
= 0; n
< count
; ++n
)
1082 FillArray(children
[n
],array
);
1086 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1089 FillArray(GetRootItem().m_pItem
, array
);
1091 return array
.Count();
1094 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1096 if (!item
.IsOk()) return;
1098 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1100 // first expand all parent branches
1101 wxGenericTreeItem
*parent
= gitem
->GetParent();
1102 while ( parent
&& !parent
->IsExpanded() )
1106 parent
= parent
->GetParent();
1109 if (parent
) CalculatePositions();
1114 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1116 if (!item
.IsOk()) return;
1118 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1120 // now scroll to the item
1121 int item_y
= gitem
->GetY();
1125 ViewStart( &start_x
, &start_y
);
1126 start_y
*= PIXELS_PER_UNIT
;
1130 GetClientSize( &client_w
, &client_h
);
1132 if (item_y
< start_y
+3)
1137 m_anchor
->GetSize( x
, y
, this );
1138 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1139 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1140 // Item should appear at top
1141 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1143 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1148 m_anchor
->GetSize( x
, y
, this );
1149 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1150 item_y
+= PIXELS_PER_UNIT
+2;
1151 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1152 // Item should appear at bottom
1153 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, (item_y
+GetLineHeight(gitem
)-client_h
)/PIXELS_PER_UNIT
);
1157 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
1158 wxClassInfo
* WXUNUSED(textCtrlClass
) )
1160 wxFAIL_MSG(_T("not implemented"));
1162 return (wxTextCtrl
*)NULL
;
1165 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1167 wxFAIL_MSG(_T("not implemented"));
1169 return (wxTextCtrl
*)NULL
;
1172 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
1174 wxFAIL_MSG(_T("not implemented"));
1177 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1178 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1180 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1181 wxGenericTreeItem
**item2
)
1183 wxCHECK_MSG( s_treeBeingSorted
, 0, _T("bug in wxTreeCtrl::SortChildren()") );
1185 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1188 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1189 const wxTreeItemId
& item2
)
1191 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1194 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1196 wxCHECK_RET( itemId
.IsOk(), _T("invalid tree item") );
1198 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1200 wxCHECK_RET( !s_treeBeingSorted
,
1201 _T("wxTreeCtrl::SortChildren is not reentrant") );
1203 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1204 if ( children
.Count() > 1 )
1206 s_treeBeingSorted
= this;
1207 children
.Sort(tree_ctrl_compare_func
);
1208 s_treeBeingSorted
= NULL
;
1212 //else: don't make the tree dirty as nothing changed
1215 wxImageList
*wxTreeCtrl::GetImageList() const
1217 return m_imageListNormal
;
1220 wxImageList
*wxTreeCtrl::GetStateImageList() const
1222 return m_imageListState
;
1225 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1227 m_imageListNormal
= imageList
;
1229 // Calculate a m_lineHeight value from the image sizes.
1230 // May be toggle off. Then wxTreeCtrl will spread when
1231 // necessary (which might look ugly).
1234 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1238 n
= m_imageListNormal
->GetImageCount();
1239 for(int i
= 0; i
< n
; i
++)
1241 m_imageListNormal
->GetSize(i
, width
, height
);
1242 if(height
> m_lineHeight
) m_lineHeight
= height
;
1245 if (m_lineHeight
<40) m_lineHeight
+=4; // at least 4 pixels (odd such that a line can be drawn in between)
1246 else m_lineHeight
+=m_lineHeight
/10; // otherwise 10% extra spacing
1251 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1253 m_imageListState
= imageList
;
1256 // -----------------------------------------------------------------------------
1258 // -----------------------------------------------------------------------------
1260 void wxTreeCtrl::AdjustMyScrollbars()
1266 m_anchor
->GetSize( x
, y
, this );
1267 //y += GetLineHeight(m_anchor);
1268 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1269 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1270 int y_pos
= GetScrollPos( wxVERTICAL
);
1271 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1275 SetScrollbars( 0, 0, 0, 0 );
1279 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1281 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HIGHT
)
1282 return item
->GetHeight();
1284 return m_lineHeight
;
1287 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1289 // render bold items in bold
1295 fontOld
= dc
.GetFont();
1298 // VZ: is there any better way to make a bold variant of old font?
1299 fontNew
= wxFont( fontOld
.GetPointSize(),
1300 fontOld
.GetFamily(),
1303 fontOld
.GetUnderlined());
1304 dc
.SetFont(fontNew
);
1308 wxFAIL_MSG(_T("wxDC::GetFont() failed!"));
1314 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1318 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1320 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1323 else if (item
->GetImage() != -1)
1325 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1329 int total_h
= GetLineHeight(item
);
1331 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1333 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1335 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1336 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1338 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1339 wxIMAGELIST_DRAW_TRANSPARENT
);
1340 dc
.DestroyClippingRegion();
1342 else if (item
->GetImage() != -1)
1344 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1345 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1347 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1348 wxIMAGELIST_DRAW_TRANSPARENT
);
1349 dc
.DestroyClippingRegion();
1352 dc
.SetBackgroundMode(wxTRANSPARENT
);
1353 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1354 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1356 // restore normal font for bold items
1359 dc
.SetFont( fontOld
);
1363 // Now y stands for the top of the item, whereas it used to stand for middle !
1364 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1366 int horizX
= level
*m_indent
;
1368 item
->SetX( horizX
+m_indent
+m_spacing
);
1372 y
+=GetLineHeight(item
)/2;
1374 item
->SetCross( horizX
+m_indent
, y
);
1376 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1377 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1379 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
)+4 )) // 10000 = very much
1381 int startX
= horizX
;
1382 int endX
= horizX
+ (m_indent
-5);
1384 // if (!item->HasChildren()) endX += (m_indent+5);
1385 if (!item
->HasChildren()) endX
+= 20;
1387 dc
.DrawLine( startX
, y
, endX
, y
);
1389 if (item
->HasPlus())
1391 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1392 dc
.SetPen( *wxGREY_PEN
);
1393 dc
.SetBrush( *wxWHITE_BRUSH
);
1394 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1396 dc
.SetPen( *wxBLACK_PEN
);
1397 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1398 if (!item
->IsExpanded())
1399 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1401 dc
.SetPen( m_dottedPen
);
1404 if (item
->HasHilight())
1406 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1408 dc
.SetBrush( *m_hilightBrush
);
1411 dc
.SetPen( *wxBLACK_PEN
);
1413 dc
.SetPen( *wxTRANSPARENT_PEN
);
1415 PaintItem(item
, dc
);
1417 dc
.SetPen( m_dottedPen
);
1418 dc
.SetTextForeground( *wxBLACK
);
1419 dc
.SetBrush( *wxWHITE_BRUSH
);
1423 dc
.SetBrush( *wxWHITE_BRUSH
);
1424 dc
.SetPen( *wxTRANSPARENT_PEN
);
1426 PaintItem(item
, dc
);
1428 dc
.SetPen( m_dottedPen
);
1432 y
= oldY
+GetLineHeight(item
);
1434 if (item
->IsExpanded())
1436 oldY
+=GetLineHeight(item
)/2;
1437 int semiOldY
=y
; // (=y) for stupid compilator
1439 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1440 size_t n
, count
= children
.Count();
1441 for ( n
= 0; n
< count
; ++n
)
1444 PaintLevel( children
[n
], dc
, level
+1, y
);
1447 // it may happen that the item is expanded but has no items (when you
1448 // delete all its children for example) - don't draw the vertical line
1452 semiOldY
+=GetLineHeight(children
[--n
])/2;
1453 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1458 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1462 wxGenericTreeItem
*i
=item
.m_pItem
;
1466 dc
.SetLogicalFunction(wxINVERT
);
1469 ViewStart(&x
,&h
); // we only need x
1470 GetClientSize(&w
,&h
); // we only need w
1472 h
=GetLineHeight(i
)+1;
1473 // 2 white column at border
1474 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1477 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1481 wxGenericTreeItem
*i
=item
.m_pItem
;
1485 dc
.SetLogicalFunction(wxINVERT
);
1490 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1493 dc
.DrawLine( 0, y
, w
, y
);
1496 // -----------------------------------------------------------------------------
1497 // wxWindows callbacks
1498 // -----------------------------------------------------------------------------
1500 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1508 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1510 dc
.SetPen( m_dottedPen
);
1511 //if(GetImageList() == NULL)
1512 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1515 PaintLevel( m_anchor
, dc
, 0, y
);
1518 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1522 if (m_current
) RefreshLine( m_current
);
1525 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1529 if (m_current
) RefreshLine( m_current
);
1532 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1534 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1535 te
.m_code
= event
.KeyCode();
1536 te
.SetEventObject( this );
1537 GetEventHandler()->ProcessEvent( te
);
1539 if ( (m_current
== 0) || (m_key_current
== 0) )
1545 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1546 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1547 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1549 switch (event
.KeyCode())
1553 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1561 if (IsExpanded(m_current
))
1563 Collapse(m_current
);
1575 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1576 event
.m_item
= m_current
;
1578 event
.SetEventObject( this );
1579 GetEventHandler()->ProcessEvent( event
);
1583 // up goes to the previous sibling or to the last of its children if
1587 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1590 prev
= GetParent( m_key_current
);
1594 wxTreeItemId current
= m_key_current
;
1595 if (current
== GetFirstChild( prev
, cockie
))
1597 // otherwise we return to where we came from
1598 SelectItem( prev
, unselect_others
, extended_select
);
1599 m_key_current
=prev
.m_pItem
;
1600 EnsureVisible( prev
);
1607 while ( IsExpanded(prev
) && HasChildren(prev
) )
1609 wxTreeItemId child
= GetLastChild(prev
);
1616 SelectItem( prev
, unselect_others
, extended_select
);
1617 m_key_current
=prev
.m_pItem
;
1618 EnsureVisible( prev
);
1623 // left arrow goes to the parent
1626 wxTreeItemId prev
= GetParent( m_current
);
1629 EnsureVisible( prev
);
1630 SelectItem( prev
, unselect_others
, extended_select
);
1636 // this works the same as the down arrow except that we also expand the
1637 // item if it wasn't expanded yet
1643 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1646 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1647 SelectItem( child
, unselect_others
, extended_select
);
1648 m_key_current
=child
.m_pItem
;
1649 EnsureVisible( child
);
1653 wxTreeItemId next
= GetNextSibling( m_key_current
);
1657 wxTreeItemId current
= m_key_current
;
1658 while (current
&& !next
)
1660 current
= GetParent( current
);
1661 if (current
) next
= GetNextSibling( current
);
1667 SelectItem( next
, unselect_others
, extended_select
);
1668 m_key_current
=next
.m_pItem
;
1669 EnsureVisible( next
);
1675 // <End> selects the last visible tree item
1678 wxTreeItemId last
= GetRootItem();
1680 while ( last
.IsOk() && IsExpanded(last
) )
1682 wxTreeItemId lastChild
= GetLastChild(last
);
1684 // it may happen if the item was expanded but then all of
1685 // its children have been deleted - so IsExpanded() returned
1686 // TRUE, but GetLastChild() returned invalid item
1695 EnsureVisible( last
);
1696 SelectItem( last
, unselect_others
, extended_select
);
1701 // <Home> selects the root item
1704 wxTreeItemId prev
= GetRootItem();
1707 EnsureVisible( prev
);
1708 SelectItem( prev
, unselect_others
, extended_select
);
1718 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1720 wxClientDC
dc(this);
1722 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1723 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1728 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1729 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1730 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1731 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1733 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1736 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1738 if (!event
.LeftIsDown()) m_dragCount
= 0;
1740 if ( !(event
.LeftUp() || event
.LeftDClick() || event
.Dragging()) ) return;
1742 if ( !m_anchor
) return;
1744 wxClientDC
dc(this);
1746 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1747 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1750 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
1751 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
1753 if (item
== NULL
) return; /* we hit the blank area */
1755 if (event
.Dragging())
1757 if (m_dragCount
== 2) /* small drag latency (3?) */
1761 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG
, GetId());
1762 nevent
.m_item
= m_current
;
1763 nevent
.SetEventObject(this);
1764 GetEventHandler()->ProcessEvent(nevent
);
1773 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1774 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1775 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1784 SelectItem(item
, unselect_others
, extended_select
);
1786 if (event
.LeftDClick())
1788 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1789 event
.m_item
= item
;
1791 event
.SetEventObject( this );
1792 GetEventHandler()->ProcessEvent( event
);
1796 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1798 /* after all changes have been done to the tree control,
1799 * we actually redraw the tree when everything is over */
1806 CalculatePositions();
1808 AdjustMyScrollbars();
1811 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
1815 // TODO : check for boldness. Here with suppose that font normal and bold
1816 // have the same height !
1817 // TODO : bug here, text_w is sometime not the correct answer !!!
1818 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1823 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1825 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1828 else if (item
->GetImage() != -1)
1830 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1834 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
1836 if (total_h
<40) total_h
+=4; // at least 4 pixels
1837 else total_h
+=total_h
/10; // otherwise 10% extra spacing
1839 item
->SetHeight(total_h
);
1840 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
1842 item
->SetWidth(image_w
+text_w
+2);
1845 // -----------------------------------------------------------------------------
1846 // for developper : y is now the top of the level
1847 // not the middle of it !
1848 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1850 int horizX
= level
*m_indent
;
1852 CalculateSize( item
, dc
);
1855 item
->SetX( horizX
+m_indent
+m_spacing
);
1857 y
+=GetLineHeight(item
);
1859 if ( !item
->IsExpanded() )
1861 // we dont need to calculate collapsed branches
1865 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1866 size_t n
, count
= children
.Count();
1867 for (n
= 0; n
< count
; ++n
)
1868 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
1871 void wxTreeCtrl::CalculatePositions()
1873 if ( !m_anchor
) return;
1875 wxClientDC
dc(this);
1878 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1880 dc
.SetPen( m_dottedPen
);
1881 //if(GetImageList() == NULL)
1882 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1884 int y
= 2; //GetLineHeight(m_anchor) / 2 + 2;
1885 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
1888 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1890 wxClientDC
dc(this);
1895 GetClientSize( &cw
, &ch
);
1898 rect
.x
= dc
.LogicalToDeviceX( 0 );
1900 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1903 Refresh( TRUE
, &rect
);
1905 AdjustMyScrollbars();
1908 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1910 wxClientDC
dc(this);
1914 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1915 rect
.y
= dc
.LogicalToDeviceY( item
->GetY());
1917 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
1919 Refresh( TRUE
, &rect
);