1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/treelist.cpp
3 // Purpose: Generic wxTreeListCtrl implementation.
4 // Author: Vadim Zeitlin
6 // RCS-ID: $Id: wxhead.cpp,v 1.11 2010-04-22 12:44:51 zeitlin Exp $
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
30 #include "wx/treelist.h"
32 #include "wx/dataview.h"
33 #include "wx/renderer.h"
34 #include "wx/scopedarray.h"
35 #include "wx/scopedptr.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 const char wxTreeListCtrlNameStr
[] = "wxTreeListCtrl";
43 const wxTreeListItem
wxTLI_FIRST(reinterpret_cast<wxTreeListModelNode
*>(-1));
44 const wxTreeListItem
wxTLI_LAST(reinterpret_cast<wxTreeListModelNode
*>(-2));
46 // ----------------------------------------------------------------------------
47 // wxTreeListModelNode: a node in the internal tree representation.
48 // ----------------------------------------------------------------------------
50 class wxTreeListModelNode
53 wxTreeListModelNode(wxTreeListModelNode
* parent
,
54 const wxString
& text
= wxString(),
55 int imageClosed
= wxWithImages::NO_IMAGE
,
56 int imageOpened
= wxWithImages::NO_IMAGE
,
57 wxClientData
* data
= NULL
)
64 m_imageClosed
= imageClosed
;
65 m_imageOpened
= imageOpened
;
67 m_checkedState
= wxCHK_UNCHECKED
;
71 m_columnsTexts
= NULL
;
74 // Destroying the node also (recursively) destroys its children.
75 ~wxTreeListModelNode()
77 for ( wxTreeListModelNode
* node
= m_child
; node
; )
79 wxTreeListModelNode
* child
= node
;
86 delete [] m_columnsTexts
;
90 // Public fields for the first column text and other simple attributes:
91 // there is no need to have accessors/mutators for those as there is no
92 // encapsulation anyhow, all of those are exposed in our public API.
98 wxCheckBoxState m_checkedState
;
101 // Accessors for the fields that are not directly exposed.
103 // Client data is owned by us so delete the old value when setting the new
105 wxClientData
* GetClientData() const { return m_data
; }
106 void SetClientData(wxClientData
* data
) { delete m_data
; m_data
= data
; }
108 // Setting or getting the non-first column text. Getting is simple but you
109 // need to call HasColumnsTexts() first as the column data is only
110 // allocated on demand. And when setting the text we require to be given
111 // the total number of columns as we allocate the entire array at once,
112 // this is more efficient than using dynamically-expandable wxVector that
113 // we know won't be needed as the number of columns is usually fixed. But
114 // if it does change, our OnInsertColumn() must be called.
116 // Notice the presence of -1 everywhere in these methods: this is because
117 // the text for the first column is always stored in m_text and so we don't
118 // store it in m_columnsTexts.
120 bool HasColumnsTexts() const { return m_columnsTexts
!= NULL
; }
121 const wxString
& GetColumnText(unsigned col
) const
123 return m_columnsTexts
[col
- 1];
126 void SetColumnText(const wxString
& text
, unsigned col
, unsigned numColumns
)
128 if ( !m_columnsTexts
)
129 m_columnsTexts
= new wxString
[numColumns
- 1];
131 m_columnsTexts
[col
- 1] = text
;
134 void OnInsertColumn(unsigned col
, unsigned numColumns
)
136 wxASSERT_MSG( col
, "Shouldn't be called for the first column" );
138 // Nothing to do if we don't have any text.
139 if ( !m_columnsTexts
)
142 wxScopedArray
<wxString
> oldTexts(m_columnsTexts
);
143 m_columnsTexts
= new wxString
[numColumns
- 1];
145 // In the loop below n is the index in the new column texts array and m
146 // is the index in the old one.
147 for ( unsigned n
= 1, m
= 1; n
< numColumns
- 1; n
++, m
++ )
151 // Leave the new array text initially empty and just adjust the
152 // index (to compensate for "m++" done by the loop anyhow).
155 else // Not the newly inserted column.
157 // Copy the old text value.
158 m_columnsTexts
[n
- 1] = oldTexts
[m
- 1];
164 // Functions for modifying the tree.
166 // Insert the given item as the first child of this one. The parent pointer
167 // must have been already set correctly at creation and we take ownership
168 // of the pointer and will delete it later.
169 void InsertChild(wxTreeListModelNode
* child
)
171 wxASSERT( child
->m_parent
== this );
173 // Our previous first child becomes the next sibling of the new child.
174 child
->m_next
= m_child
;
178 // Insert the given item as our next sibling. As above, the item must have
179 // the correct parent pointer and we take ownership of it.
180 void InsertNext(wxTreeListModelNode
* next
)
182 wxASSERT( next
->m_parent
== m_parent
);
184 next
->m_next
= m_next
;
188 // Remove the first child of this item from the tree and delete it.
191 wxTreeListModelNode
* const oldChild
= m_child
;
192 m_child
= m_child
->m_next
;
196 // Remove the next sibling of this item from the tree and deletes it.
199 wxTreeListModelNode
* const oldNext
= m_next
;
200 m_next
= m_next
->m_next
;
205 // Functions for tree traversal. All of them can return NULL.
207 // Only returns NULL when called on the root item.
208 wxTreeListModelNode
* GetParent() const { return m_parent
; }
210 // Returns the first child of this item.
211 wxTreeListModelNode
* GetChild() const { return m_child
; }
213 // Returns the next sibling of this item.
214 wxTreeListModelNode
* GetNext() const { return m_next
; }
216 // Unlike the previous two functions, this one is not a simple accessor
217 // (hence it's not called "GetSomething") but computes the next node after
218 // this one in tree order.
219 wxTreeListModelNode
* NextInTree() const
227 // Recurse upwards until we find the next sibling.
228 for ( wxTreeListModelNode
* node
= m_parent
; node
; node
= node
->m_parent
)
239 // The (never changing after creation) parent of this node and the possibly
240 // NULL pointers to its first child and next sibling.
241 wxTreeListModelNode
* const m_parent
;
242 wxTreeListModelNode
* m_child
;
243 wxTreeListModelNode
* m_next
;
245 // Client data pointer owned by the control. May be NULL.
246 wxClientData
* m_data
;
248 // Array of column values for all the columns except the first one. May be
249 // NULL if no values had been set for them.
250 wxString
* m_columnsTexts
;
253 // ----------------------------------------------------------------------------
254 // wxTreeListModel: wxDataViewModel implementation used by wxTreeListCtrl.
255 // ----------------------------------------------------------------------------
257 class wxTreeListModel
: public wxDataViewModel
260 typedef wxTreeListModelNode Node
;
262 // Unlike a general wxDataViewModel, this model can only be used with a
263 // single control at once. The main reason for this is that we need to
264 // support different icons for opened and closed items and the item state
265 // is associated with the control, not the model, so our GetValue() is also
266 // bound to it (otherwise, what would it return for an item expanded in one
267 // associated control and collapsed in another one?).
268 wxTreeListModel(wxTreeListCtrl
* treelist
);
269 virtual ~wxTreeListModel();
272 // Helpers for converting between wxDataViewItem and wxTreeListItem. These
273 // methods simply cast the pointer to/from wxDataViewItem except for the
274 // root node that we handle specially unless explicitly disabled.
276 // The advantage of using them is that they're greppable and stand out
277 // better, hopefully making the code more clear.
278 Node
* FromNonRootDVI(wxDataViewItem dvi
) const
280 return static_cast<Node
*>(dvi
.GetID());
283 Node
* FromDVI(wxDataViewItem dvi
) const
288 return FromNonRootDVI(dvi
);
291 wxDataViewItem
ToNonRootDVI(Node
* node
) const
293 return wxDataViewItem(node
);
296 wxDataViewItem
ToDVI(Node
* node
) const
298 // Our root item must be represented as NULL at wxDVC level to map to
299 // its own invisible root.
300 if ( !node
->GetParent() )
301 return wxDataViewItem();
303 return ToNonRootDVI(node
);
307 // Methods called by wxTreeListCtrl.
308 void InsertColumn(unsigned col
);
310 Node
* InsertItem(Node
* parent
,
312 const wxString
& text
,
316 void DeleteItem(Node
* item
);
317 void DeleteAllItems();
319 Node
* GetRootItem() const { return m_root
; }
321 const wxString
& GetItemText(Node
* item
, unsigned col
) const;
322 void SetItemText(Node
* item
, unsigned col
, const wxString
& text
);
323 void SetItemImage(Node
* item
, int closed
, int opened
);
324 wxClientData
* GetItemData(Node
* item
) const;
325 void SetItemData(Node
* item
, wxClientData
* data
);
327 void CheckItem(Node
* item
, wxCheckBoxState checkedState
);
328 void ToggleItem(wxDataViewItem item
);
331 // Implement the base class pure virtual methods.
332 virtual unsigned GetColumnCount() const;
333 virtual wxString
GetColumnType(unsigned col
) const;
334 virtual void GetValue(wxVariant
& variant
,
335 const wxDataViewItem
& item
,
337 virtual bool SetValue(const wxVariant
& variant
,
338 const wxDataViewItem
& item
,
340 virtual wxDataViewItem
GetParent(const wxDataViewItem
& item
) const;
341 virtual bool IsContainer(const wxDataViewItem
& item
) const;
342 virtual bool HasContainerColumns(const wxDataViewItem
& item
) const;
343 virtual unsigned GetChildren(const wxDataViewItem
& item
,
344 wxDataViewItemArray
& children
) const;
347 // The control we're associated with.
348 wxTreeListCtrl
* const m_treelist
;
350 // The unique invisible root element.
353 // Number of columns we maintain.
354 unsigned m_numColumns
;
357 // ============================================================================
358 // wxDataViewCheckIconText[Renderer]: special renderer for our first column.
359 // ============================================================================
361 // Currently this class is private but it could be extracted and made part of
362 // public API later as could be used directly with wxDataViewCtrl as well.
366 const char* CHECK_ICON_TEXT_TYPE
= "wxDataViewCheckIconText";
368 // The value used by wxDataViewCheckIconTextRenderer
369 class wxDataViewCheckIconText
: public wxDataViewIconText
372 wxDataViewCheckIconText(const wxString
& text
= wxString(),
373 const wxIcon
& icon
= wxNullIcon
,
374 wxCheckBoxState checkedState
= wxCHK_UNDETERMINED
)
375 : wxDataViewIconText(text
, icon
),
376 m_checkedState(checkedState
)
380 wxDataViewCheckIconText(const wxDataViewCheckIconText
& other
)
381 : wxDataViewIconText(other
),
382 m_checkedState(other
.m_checkedState
)
386 bool IsSameAs(const wxDataViewCheckIconText
& other
) const
388 return wxDataViewIconText::IsSameAs(other
) &&
389 m_checkedState
== other
.m_checkedState
;
392 // There is no encapsulation anyhow, so just expose this field directly.
393 wxCheckBoxState m_checkedState
;
397 wxDECLARE_DYNAMIC_CLASS(wxDataViewCheckIconText
);
400 wxIMPLEMENT_DYNAMIC_CLASS(wxDataViewCheckIconText
, wxDataViewIconText
);
402 DECLARE_VARIANT_OBJECT(wxDataViewCheckIconText
)
403 IMPLEMENT_VARIANT_OBJECT(wxDataViewCheckIconText
)
406 class wxDataViewCheckIconTextRenderer
: public wxDataViewCustomRenderer
409 wxDataViewCheckIconTextRenderer()
410 : wxDataViewCustomRenderer(CHECK_ICON_TEXT_TYPE
,
411 wxDATAVIEW_CELL_ACTIVATABLE
)
415 virtual bool SetValue(const wxVariant
& value
)
421 virtual bool GetValue(wxVariant
& WXUNUSED(value
)) const
426 wxSize
GetSize() const
428 wxSize size
= GetCheckSize();
429 size
.x
+= MARGIN_CHECK_ICON
;
431 if ( m_value
.GetIcon().IsOk() )
433 const wxSize sizeIcon
= m_value
.GetIcon().GetSize();
434 if ( sizeIcon
.y
> size
.y
)
437 size
.x
+= sizeIcon
.x
+ MARGIN_ICON_TEXT
;
440 wxString text
= m_value
.GetText();
444 const wxSize sizeText
= GetTextExtent(text
);
445 if ( sizeText
.y
> size
.y
)
448 size
.x
+= sizeText
.x
;
453 virtual bool Render(wxRect cell
, wxDC
* dc
, int state
)
455 // Draw the checkbox first.
457 switch ( m_value
.m_checkedState
)
459 case wxCHK_UNCHECKED
:
463 renderFlags
|= wxCONTROL_CHECKED
;
466 case wxCHK_UNDETERMINED
:
467 renderFlags
|= wxCONTROL_UNDETERMINED
;
471 if ( state
& wxDATAVIEW_CELL_PRELIT
)
472 renderFlags
|= wxCONTROL_CURRENT
;
474 const wxSize sizeCheck
= GetCheckSize();
476 wxRect
rectCheck(cell
.GetPosition(), sizeCheck
);
477 rectCheck
= rectCheck
.CentreIn(cell
, wxVERTICAL
);
479 wxRendererNative::Get().DrawCheckBox
481 GetView(), *dc
, rectCheck
, renderFlags
484 // Then the icon, if any.
485 int xoffset
= sizeCheck
.x
+ MARGIN_CHECK_ICON
;
487 const wxIcon
& icon
= m_value
.GetIcon();
490 const wxSize sizeIcon
= icon
.GetSize();
491 wxRect
rectIcon(cell
.GetPosition(), sizeIcon
);
492 rectIcon
.x
+= xoffset
;
493 rectIcon
= rectIcon
.CentreIn(cell
, wxVERTICAL
);
495 dc
->DrawIcon(icon
, rectIcon
.GetPosition());
497 xoffset
+= sizeIcon
.x
+ MARGIN_ICON_TEXT
;
501 RenderText(m_value
.GetText(), xoffset
, cell
, dc
, state
);
506 // Event handlers toggling the items checkbox if it was clicked.
507 virtual bool Activate(const wxRect
& WXUNUSED(cell
),
508 wxDataViewModel
* model
,
509 const wxDataViewItem
& item
,
510 unsigned int WXUNUSED(col
))
512 static_cast<wxTreeListModel
*>(model
)->ToggleItem(item
);
516 virtual bool LeftClick(const wxPoint
& pos
,
517 const wxRect
& WXUNUSED(cell
),
518 wxDataViewModel
* model
,
519 const wxDataViewItem
& item
,
520 unsigned int WXUNUSED(col
))
522 if ( !wxRect(GetCheckSize()).Contains(pos
) )
525 static_cast<wxTreeListModel
*>(model
)->ToggleItem(item
);
530 wxSize
GetCheckSize() const
532 return wxRendererNative::Get().GetCheckBoxSize(GetView());
536 // Just some arbitrary constants defining margins, in pixels.
539 MARGIN_CHECK_ICON
= 3,
543 wxDataViewCheckIconText m_value
;
546 } // anonymous namespace
548 // ============================================================================
549 // wxTreeListModel implementation
550 // ============================================================================
552 wxTreeListModel::wxTreeListModel(wxTreeListCtrl
* treelist
)
553 : m_treelist(treelist
),
554 m_root(new Node(NULL
))
559 wxTreeListModel::~wxTreeListModel()
564 void wxTreeListModel::InsertColumn(unsigned col
)
568 // There is no need to update anything when inserting the first column.
569 if ( m_numColumns
== 1 )
572 // Update all the items as they may have texts for the old columns.
573 for ( Node
* node
= m_root
->GetChild(); node
; node
= node
->NextInTree() )
575 node
->OnInsertColumn(col
, m_numColumns
);
580 wxTreeListModel::InsertItem(Node
* parent
,
582 const wxString
& text
,
587 wxCHECK_MSG( parent
, NULL
,
588 "Must have a valid parent (maybe GetRootItem()?)" );
590 wxCHECK_MSG( previous
, NULL
,
591 "Must have a valid previous item (maybe wxTLI_FIRST/LAST?)" );
594 newItem(new Node(parent
, text
, imageClosed
, imageOpened
, data
));
596 // If we have no children at all, then inserting as last child is the same
597 // as inserting as the first one so check for it here too.
598 if ( previous
== wxTLI_FIRST
||
599 (previous
== wxTLI_LAST
&& !parent
->GetChild()) )
601 parent
->InsertChild(newItem
.get());
603 else // Not the first item, find the previous one.
605 if ( previous
== wxTLI_LAST
)
607 previous
= parent
->GetChild();
609 // Find the last child.
612 Node
* const next
= previous
->GetNext();
619 else // We already have the previous item.
621 // Just check it's under the correct parent.
622 wxCHECK_MSG( previous
->GetParent() == parent
, NULL
,
623 "Previous item is not under the right parent" );
626 previous
->InsertNext(newItem
.get());
629 ItemAdded(ToDVI(parent
), ToDVI(newItem
.get()));
631 // The item was successfully inserted in the tree and so will be deleted by
632 // it, we can detach it now.
633 return newItem
.release();
636 void wxTreeListModel::DeleteItem(Node
* item
)
638 wxCHECK_RET( item
, "Invalid item" );
640 wxCHECK_RET( item
!= m_root
, "Can't delete the root item" );
642 Node
* const parent
= item
->GetParent();
644 ItemDeleted(ToDVI(parent
), ToDVI(item
));
646 Node
* previous
= parent
->GetChild();
647 if ( previous
== item
)
649 parent
->DeleteChild();
651 else // Not the first child of its parent.
653 // Find the sibling just before it.
656 Node
* const next
= previous
->GetNext();
660 wxCHECK_RET( next
, "Item not a child of its parent?" );
665 previous
->DeleteNext();
669 void wxTreeListModel::DeleteAllItems()
671 while ( m_root
->GetChild() )
673 m_root
->DeleteChild();
679 const wxString
& wxTreeListModel::GetItemText(Node
* item
, unsigned col
) const
681 // Returning root item text here is bogus, it just happens to be an always
682 // empty string we can return reference to.
683 wxCHECK_MSG( item
, m_root
->m_text
, "Invalid item" );
685 return col
== 0 ? item
->m_text
: item
->GetColumnText(col
);
688 void wxTreeListModel::SetItemText(Node
* item
, unsigned col
, const wxString
& text
)
690 wxCHECK_RET( item
, "Invalid item" );
695 item
->SetColumnText(text
, col
, m_numColumns
);
697 ValueChanged(ToDVI(item
), col
);
700 void wxTreeListModel::SetItemImage(Node
* item
, int closed
, int opened
)
702 wxCHECK_RET( item
, "Invalid item" );
704 item
->m_imageClosed
= closed
;
705 item
->m_imageOpened
= opened
;
707 ValueChanged(ToDVI(item
), 0);
710 wxClientData
* wxTreeListModel::GetItemData(Node
* item
) const
712 wxCHECK_MSG( item
, NULL
, "Invalid item" );
714 return item
->GetClientData();
717 void wxTreeListModel::SetItemData(Node
* item
, wxClientData
* data
)
719 wxCHECK_RET( item
, "Invalid item" );
721 item
->SetClientData(data
);
724 void wxTreeListModel::CheckItem(Node
* item
, wxCheckBoxState checkedState
)
726 wxCHECK_RET( item
, "Invalid item" );
728 item
->m_checkedState
= checkedState
;
730 ItemChanged(ToDVI(item
));
733 void wxTreeListModel::ToggleItem(wxDataViewItem dvi
)
735 Node
* const item
= FromDVI(dvi
);
737 wxCHECK_RET( item
, "Invalid item" );
739 const wxCheckBoxState stateOld
= item
->m_checkedState
;
741 // If the 3rd state is user-settable then the cycle is
742 // unchecked->checked->undetermined.
746 item
->m_checkedState
= m_treelist
->HasFlag(wxTL_USER_3STATE
)
751 case wxCHK_UNDETERMINED
:
752 // Whether 3rd state is user-settable or not, the next state is
754 item
->m_checkedState
= wxCHK_UNCHECKED
;
757 case wxCHK_UNCHECKED
:
758 item
->m_checkedState
= wxCHK_CHECKED
;
762 ItemChanged(ToDVI(item
));
764 m_treelist
->OnItemToggled(item
, stateOld
);
767 unsigned wxTreeListModel::GetColumnCount() const
772 wxString
wxTreeListModel::GetColumnType(unsigned col
) const
776 return m_treelist
->HasFlag(wxTL_CHECKBOX
)
777 ? wxS("wxDataViewCheckIconText")
778 : wxS("wxDataViewIconText");
780 else // All the other columns contain just text.
782 return wxS("string");
787 wxTreeListModel::GetValue(wxVariant
& variant
,
788 const wxDataViewItem
& item
,
791 Node
* const node
= FromDVI(item
);
795 // Determine the correct image to use depending on the item state.
796 int image
= wxWithImages::NO_IMAGE
;
797 if ( m_treelist
->IsExpanded(node
) )
798 image
= node
->m_imageOpened
;
800 if ( image
== wxWithImages::NO_IMAGE
)
801 image
= node
->m_imageClosed
;
803 wxIcon icon
= m_treelist
->GetImage(image
);
805 if ( m_treelist
->HasFlag(wxTL_CHECKBOX
) )
806 variant
<< wxDataViewCheckIconText(node
->m_text
, icon
,
807 node
->m_checkedState
);
809 variant
<< wxDataViewIconText(node
->m_text
, icon
);
813 // Notice that we must still assign wxString to wxVariant to ensure
814 // that it at least has the correct type.
816 if ( node
->HasColumnsTexts() )
817 text
= node
->GetColumnText(col
);
824 wxTreeListModel::SetValue(const wxVariant
& WXUNUSED(variant
),
825 const wxDataViewItem
& WXUNUSED(item
),
826 unsigned WXUNUSED(col
))
828 // We are not editable currently.
832 wxDataViewItem
wxTreeListModel::GetParent(const wxDataViewItem
& item
) const
834 Node
* const node
= FromDVI(item
);
836 return ToDVI(node
->GetParent());
839 bool wxTreeListModel::IsContainer(const wxDataViewItem
& item
) const
841 // FIXME: In the generic (and native OS X) versions we implement this
842 // method normally, i.e. only items with children are containers.
843 // But for the native GTK version we must pretend that all items are
844 // containers because otherwise adding children to them later would
845 // fail because wxGTK code calls IsContainer() too early (when
846 // adding the item itself) and we can't know whether we're container
847 // or not by then. Luckily, always returning true doesn't have any
848 // serious drawbacks for us.
854 Node
* const node
= FromDVI(item
);
856 return node
->GetChild() != NULL
;
861 wxTreeListModel::HasContainerColumns(const wxDataViewItem
& WXUNUSED(item
)) const
867 wxTreeListModel::GetChildren(const wxDataViewItem
& item
,
868 wxDataViewItemArray
& children
) const
870 Node
* const node
= FromDVI(item
);
872 unsigned numChildren
= 0;
873 for ( Node
* child
= node
->GetChild(); child
; child
= child
->GetNext() )
875 children
.push_back(ToDVI(child
));
882 // ============================================================================
883 // wxTreeListCtrl implementation
884 // ============================================================================
886 BEGIN_EVENT_TABLE(wxTreeListCtrl
, wxWindow
)
887 EVT_DATAVIEW_SELECTION_CHANGED(wxID_ANY
, wxTreeListCtrl::OnSelectionChanged
)
888 EVT_DATAVIEW_ITEM_EXPANDING(wxID_ANY
, wxTreeListCtrl::OnItemExpanding
)
889 EVT_DATAVIEW_ITEM_EXPANDED(wxID_ANY
, wxTreeListCtrl::OnItemExpanded
)
890 EVT_DATAVIEW_ITEM_ACTIVATED(wxID_ANY
, wxTreeListCtrl::OnItemActivated
)
891 EVT_DATAVIEW_ITEM_CONTEXT_MENU(wxID_ANY
, wxTreeListCtrl::OnItemContextMenu
)
893 EVT_SIZE(wxTreeListCtrl::OnSize
)
896 // ----------------------------------------------------------------------------
898 // ----------------------------------------------------------------------------
900 void wxTreeListCtrl::Init()
906 bool wxTreeListCtrl::Create(wxWindow
* parent
,
911 const wxString
& name
)
913 if ( style
& wxTL_USER_3STATE
)
914 style
|= wxTL_3STATE
;
916 if ( style
& wxTL_3STATE
)
917 style
|= wxTL_CHECKBOX
;
919 // Create the window itself and wxDataViewCtrl used by it.
920 if ( !wxWindow::Create(parent
, id
,
927 m_view
= new wxDataViewCtrl
;
928 if ( !m_view
->Create(this, wxID_ANY
,
929 wxPoint(0, 0), GetClientSize(),
930 HasFlag(wxTL_MULTIPLE
) ? wxDV_MULTIPLE
940 // Set up the model for wxDataViewCtrl.
941 m_model
= new wxTreeListModel(this);
942 m_view
->AssociateModel(m_model
);
947 wxTreeListCtrl::~wxTreeListCtrl()
953 // ----------------------------------------------------------------------------
955 // ----------------------------------------------------------------------------
958 wxTreeListCtrl::DoInsertColumn(const wxString
& title
,
964 wxCHECK_MSG( m_view
, wxNOT_FOUND
, "Must Create() first" );
966 const unsigned oldNumColumns
= m_view
->GetColumnCount();
968 if ( pos
== wxNOT_FOUND
)
971 wxDataViewRenderer
* renderer
;
974 // Inserting the first column which is special as it uses a different
977 // Also, currently it can be done only once.
978 wxCHECK_MSG( !oldNumColumns
, wxNOT_FOUND
,
979 "Inserting column at position 0 currently not supported" );
981 if ( HasFlag(wxTL_CHECKBOX
) )
983 // Use our custom renderer to show the checkbox.
984 renderer
= new wxDataViewCheckIconTextRenderer
;
986 else // We still need a special renderer to show the icons.
988 renderer
= new wxDataViewIconTextRenderer
;
991 else // Not the first column.
993 // All the other ones use a simple text renderer.
994 renderer
= new wxDataViewTextRenderer
;
998 column
= new wxDataViewColumn(title
, renderer
, pos
, width
, align
, flags
);
1000 m_model
->InsertColumn(pos
);
1002 m_view
->InsertColumn(pos
, column
);
1007 unsigned wxTreeListCtrl::GetColumnCount() const
1009 return m_view
? m_view
->GetColumnCount() : 0u;
1012 bool wxTreeListCtrl::DeleteColumn(unsigned col
)
1014 wxCHECK_MSG( col
< GetColumnCount(), false, "Invalid column index" );
1016 return m_view
->DeleteColumn(m_view
->GetColumn(col
));
1019 void wxTreeListCtrl::ClearColumns()
1022 m_view
->ClearColumns();
1025 void wxTreeListCtrl::SetColumnWidth(unsigned col
, int width
)
1027 wxCHECK_RET( col
< GetColumnCount(), "Invalid column index" );
1029 wxDataViewColumn
* const column
= m_view
->GetColumn(col
);
1030 wxCHECK_RET( column
, "No such column?" );
1032 return column
->SetWidth(width
);
1035 int wxTreeListCtrl::GetColumnWidth(unsigned col
) const
1037 wxCHECK_MSG( col
< GetColumnCount(), -1, "Invalid column index" );
1039 wxDataViewColumn
* column
= m_view
->GetColumn(col
);
1040 wxCHECK_MSG( column
, -1, "No such column?" );
1042 return column
->GetWidth();
1045 int wxTreeListCtrl::WidthFor(const wxString
& text
) const
1047 return GetTextExtent(text
).x
;
1050 // ----------------------------------------------------------------------------
1052 // ----------------------------------------------------------------------------
1055 wxTreeListCtrl::DoInsertItem(wxTreeListItem parent
,
1056 wxTreeListItem previous
,
1057 const wxString
& text
,
1062 wxCHECK_MSG( m_model
, wxTreeListItem(), "Must create first" );
1064 return wxTreeListItem(m_model
->InsertItem(parent
, previous
, text
,
1065 imageClosed
, imageOpened
, data
));
1068 void wxTreeListCtrl::DeleteItem(wxTreeListItem item
)
1070 wxCHECK_RET( m_model
, "Must create first" );
1072 m_model
->DeleteItem(item
);
1075 void wxTreeListCtrl::DeleteAllItems()
1078 m_model
->DeleteAllItems();
1081 // ----------------------------------------------------------------------------
1083 // ----------------------------------------------------------------------------
1085 // The simple accessors in this section are implemented directly using
1086 // wxTreeListModelNode methods, without passing by the model. This is just a
1087 // shortcut and avoids us the trouble of defining more trivial methods in
1090 wxTreeListItem
wxTreeListCtrl::GetRootItem() const
1092 wxCHECK_MSG( m_model
, wxTreeListItem(), "Must create first" );
1094 return m_model
->GetRootItem();
1097 wxTreeListItem
wxTreeListCtrl::GetItemParent(wxTreeListItem item
) const
1099 wxCHECK_MSG( item
.IsOk(), wxTreeListItem(), "Invalid item" );
1101 return item
->GetParent();
1104 wxTreeListItem
wxTreeListCtrl::GetFirstChild(wxTreeListItem item
) const
1106 wxCHECK_MSG( item
.IsOk(), wxTreeListItem(), "Invalid item" );
1108 return item
->GetChild();
1112 wxTreeListCtrl::GetNextSibling(wxTreeListItem item
) const
1114 wxCHECK_MSG( item
.IsOk(), wxTreeListItem(), "Invalid item" );
1116 return item
->GetNext();
1119 wxTreeListItem
wxTreeListCtrl::GetNextItem(wxTreeListItem item
) const
1121 wxCHECK_MSG( item
.IsOk(), wxTreeListItem(), "Invalid item" );
1123 return item
->NextInTree();
1126 // ----------------------------------------------------------------------------
1128 // ----------------------------------------------------------------------------
1131 wxTreeListCtrl::GetItemText(wxTreeListItem item
, unsigned col
) const
1133 // We can't use wxCHECK_MSG() here because we don't have any empty string
1134 // reference to return so we use a static variable that exists just for the
1135 // purpose of this check -- and so we put it in its own scope so that it's
1136 // never even created during normal program execution.
1137 if ( !m_model
|| col
>= m_model
->GetColumnCount() )
1139 static wxString s_empty
;
1143 wxFAIL_MSG( "Must create first" );
1145 else if ( col
>= m_model
->GetColumnCount() )
1147 wxFAIL_MSG( "Invalid column index" );
1153 return m_model
->GetItemText(item
, col
);
1157 wxTreeListCtrl::SetItemText(wxTreeListItem item
,
1159 const wxString
& text
)
1161 wxCHECK_RET( m_model
, "Must create first" );
1162 wxCHECK_RET( col
< m_model
->GetColumnCount(), "Invalid column index" );
1164 m_model
->SetItemText(item
, col
, text
);
1167 void wxTreeListCtrl::SetItemImage(wxTreeListItem item
, int closed
, int opened
)
1169 wxCHECK_RET( m_model
, "Must create first" );
1171 if ( closed
!= NO_IMAGE
|| opened
!= NO_IMAGE
)
1173 wxImageList
* const imageList
= GetImageList();
1174 wxCHECK_RET( imageList
, "Can't set images without image list" );
1176 const int imageCount
= imageList
->GetImageCount();
1178 wxCHECK_RET( closed
< imageCount
, "Invalid image index" );
1179 wxCHECK_RET( opened
< imageCount
, "Invalid opened image index" );
1182 m_model
->SetItemImage(item
, closed
, opened
);
1185 wxClientData
* wxTreeListCtrl::GetItemData(wxTreeListItem item
) const
1187 wxCHECK_MSG( m_model
, NULL
, "Must create first" );
1189 return m_model
->GetItemData(item
);
1192 void wxTreeListCtrl::SetItemData(wxTreeListItem item
, wxClientData
* data
)
1194 wxCHECK_RET( m_model
, "Must create first" );
1196 m_model
->SetItemData(item
, data
);
1199 // ----------------------------------------------------------------------------
1200 // Expanding and collapsing
1201 // ----------------------------------------------------------------------------
1203 void wxTreeListCtrl::Expand(wxTreeListItem item
)
1205 wxCHECK_RET( m_view
, "Must create first" );
1207 m_view
->Expand(m_model
->ToDVI(item
));
1210 void wxTreeListCtrl::Collapse(wxTreeListItem item
)
1212 wxCHECK_RET( m_view
, "Must create first" );
1214 m_view
->Collapse(m_model
->ToDVI(item
));
1217 bool wxTreeListCtrl::IsExpanded(wxTreeListItem item
) const
1219 wxCHECK_MSG( m_view
, false, "Must create first" );
1221 return m_view
->IsExpanded(m_model
->ToDVI(item
));
1224 // ----------------------------------------------------------------------------
1226 // ----------------------------------------------------------------------------
1228 wxTreeListItem
wxTreeListCtrl::GetSelection() const
1230 wxCHECK_MSG( m_view
, wxTreeListItem(), "Must create first" );
1232 wxCHECK_MSG( !HasFlag(wxTL_MULTIPLE
), wxTreeListItem(),
1233 "Must use GetSelections() with multi-selection controls!" );
1235 const wxDataViewItem dvi
= m_view
->GetSelection();
1237 return m_model
->FromNonRootDVI(dvi
);
1240 unsigned wxTreeListCtrl::GetSelections(wxTreeListItems
& selections
) const
1242 wxCHECK_MSG( m_view
, 0, "Must create first" );
1244 wxDataViewItemArray selectionsDV
;
1245 const unsigned numSelected
= m_view
->GetSelections(selectionsDV
);
1246 selections
.resize(numSelected
);
1247 for ( unsigned n
= 0; n
< numSelected
; n
++ )
1248 selections
[n
] = m_model
->FromNonRootDVI(selectionsDV
[n
]);
1253 void wxTreeListCtrl::Select(wxTreeListItem item
)
1255 wxCHECK_RET( m_view
, "Must create first" );
1257 m_view
->Select(m_model
->ToNonRootDVI(item
));
1260 void wxTreeListCtrl::Unselect(wxTreeListItem item
)
1262 wxCHECK_RET( m_view
, "Must create first" );
1264 m_view
->Unselect(m_model
->ToNonRootDVI(item
));
1267 bool wxTreeListCtrl::IsSelected(wxTreeListItem item
) const
1269 wxCHECK_MSG( m_view
, false, "Must create first" );
1271 return m_view
->IsSelected(m_model
->ToNonRootDVI(item
));
1274 void wxTreeListCtrl::SelectAll()
1276 wxCHECK_RET( m_view
, "Must create first" );
1278 m_view
->SelectAll();
1281 void wxTreeListCtrl::UnselectAll()
1283 wxCHECK_RET( m_view
, "Must create first" );
1285 m_view
->UnselectAll();
1288 // ----------------------------------------------------------------------------
1289 // Checkbox handling
1290 // ----------------------------------------------------------------------------
1292 void wxTreeListCtrl::CheckItem(wxTreeListItem item
, wxCheckBoxState state
)
1294 wxCHECK_RET( m_model
, "Must create first" );
1296 m_model
->CheckItem(item
, state
);
1300 wxTreeListCtrl::CheckItemRecursively(wxTreeListItem item
, wxCheckBoxState state
)
1302 wxCHECK_RET( m_model
, "Must create first" );
1304 m_model
->CheckItem(item
, state
);
1306 for ( wxTreeListItem child
= GetFirstChild(item
);
1308 child
= GetNextSibling(child
) )
1310 CheckItemRecursively(child
, state
);
1314 void wxTreeListCtrl::UpdateItemParentStateRecursively(wxTreeListItem item
)
1316 wxCHECK_RET( item
.IsOk(), "Invalid item" );
1318 wxASSERT_MSG( HasFlag(wxTL_3STATE
), "Can only be used with wxTL_3STATE" );
1322 wxTreeListItem parent
= GetItemParent(item
);
1323 if ( parent
== GetRootItem() )
1325 // There is no checked state associated with the root item.
1329 // Set parent state to the state of this item if all the other children
1330 // have the same state too. Otherwise make it indeterminate.
1331 const wxCheckBoxState stateItem
= GetCheckedState(item
);
1332 CheckItem(parent
, AreAllChildrenInState(parent
, stateItem
)
1334 : wxCHK_UNDETERMINED
);
1336 // And do the same thing with the parent's parent too.
1341 wxCheckBoxState
wxTreeListCtrl::GetCheckedState(wxTreeListItem item
) const
1343 wxCHECK_MSG( item
.IsOk(), wxCHK_UNDETERMINED
, "Invalid item" );
1345 return item
->m_checkedState
;
1349 wxTreeListCtrl::AreAllChildrenInState(wxTreeListItem item
,
1350 wxCheckBoxState state
) const
1352 wxCHECK_MSG( item
.IsOk(), false, "Invalid item" );
1354 for ( wxTreeListItem child
= GetFirstChild(item
);
1356 child
= GetNextSibling(child
) )
1358 if ( GetCheckedState(child
) != state
)
1365 // ----------------------------------------------------------------------------
1367 // ----------------------------------------------------------------------------
1369 void wxTreeListCtrl::SendEvent(wxEventType evt
, wxDataViewEvent
& eventDV
)
1371 wxTreeListEvent
eventTL(evt
, this, m_model
->FromDVI(eventDV
.GetItem()));
1373 if ( !ProcessWindowEvent(eventTL
) )
1379 if ( !eventTL
.IsAllowed() )
1386 wxTreeListCtrl::OnItemToggled(wxTreeListItem item
, wxCheckBoxState stateOld
)
1388 wxTreeListEvent
event(wxEVT_COMMAND_TREELIST_ITEM_CHECKED
, this, item
);
1389 event
.SetOldCheckedState(stateOld
);
1391 ProcessWindowEvent(event
);
1394 void wxTreeListCtrl::OnSelectionChanged(wxDataViewEvent
& event
)
1396 SendEvent(wxEVT_COMMAND_TREELIST_SELECTION_CHANGED
, event
);
1399 void wxTreeListCtrl::OnItemExpanding(wxDataViewEvent
& event
)
1401 SendEvent(wxEVT_COMMAND_TREELIST_ITEM_EXPANDING
, event
);
1404 void wxTreeListCtrl::OnItemExpanded(wxDataViewEvent
& event
)
1406 SendEvent(wxEVT_COMMAND_TREELIST_ITEM_EXPANDED
, event
);
1409 void wxTreeListCtrl::OnItemActivated(wxDataViewEvent
& event
)
1411 SendEvent(wxEVT_COMMAND_TREELIST_ITEM_ACTIVATED
, event
);
1414 void wxTreeListCtrl::OnItemContextMenu(wxDataViewEvent
& event
)
1416 SendEvent(wxEVT_COMMAND_TREELIST_ITEM_CONTEXT_MENU
, event
);
1419 // ----------------------------------------------------------------------------
1421 // ----------------------------------------------------------------------------
1423 void wxTreeListCtrl::OnSize(wxSizeEvent
& event
)
1429 // Resize the real control to cover our entire client area.
1430 const wxRect rect
= GetClientRect();
1431 m_view
->SetSize(rect
);
1433 // Resize the first column to take the remaining available space, if
1435 const unsigned numColumns
= GetColumnCount();
1439 // There is a bug in generic wxDataViewCtrl: if the column width sums
1440 // up to the total size, horizontal scrollbar (unnecessarily) appears,
1441 // so subtract 10 pixels to ensure this doesn't happen.
1442 int remainingWidth
= rect
.width
- 10;
1443 for ( unsigned n
= 1; n
< GetColumnCount(); n
++ )
1445 remainingWidth
-= GetColumnWidth(n
);
1446 if ( remainingWidth
< 0 )
1450 // We don't decrease the width of the first column, even if we had
1451 // increased it ourselves, because we want to avoid changing its size
1452 // if the user resized it. We might want to remember if this was the
1453 // case or if we only ever adjusted it automatically in the future.
1454 if ( remainingWidth
> GetColumnWidth(0) )
1455 SetColumnWidth(0, remainingWidth
);
1459 // ============================================================================
1460 // wxTreeListEvent implementation
1461 // ============================================================================
1463 wxIMPLEMENT_ABSTRACT_CLASS(wxTreeListEvent
, wxNotifyEvent
)
1465 #define wxDEFINE_TREELIST_EVENT(name) \
1466 wxDEFINE_EVENT(wxEVT_COMMAND_TREELIST_##name, wxTreeListEvent)
1468 wxDEFINE_TREELIST_EVENT(SELECTION_CHANGED
);
1469 wxDEFINE_TREELIST_EVENT(ITEM_EXPANDING
);
1470 wxDEFINE_TREELIST_EVENT(ITEM_EXPANDED
);
1471 wxDEFINE_TREELIST_EVENT(ITEM_CHECKED
);
1472 wxDEFINE_TREELIST_EVENT(ITEM_ACTIVATED
);
1473 wxDEFINE_TREELIST_EVENT(ITEM_CONTEXT_MENU
);
1475 #undef wxDEFINE_TREELIST_EVENT