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];
163 void OnDeleteColumn(unsigned col
, unsigned numColumns
)
165 wxASSERT_MSG( col
, "Shouldn't be called for the first column" );
167 if ( !m_columnsTexts
)
170 wxScopedArray
<wxString
> oldTexts(m_columnsTexts
);
171 m_columnsTexts
= new wxString
[numColumns
- 2];
172 for ( unsigned n
= 1, m
= 1; n
< numColumns
- 1; n
++, m
++ )
178 else // Not the deleted column.
180 m_columnsTexts
[n
- 1] = oldTexts
[m
- 1];
185 void OnClearColumns()
187 if ( m_columnsTexts
)
189 delete [] m_columnsTexts
;
190 m_columnsTexts
= NULL
;
195 // Functions for modifying the tree.
197 // Insert the given item as the first child of this one. The parent pointer
198 // must have been already set correctly at creation and we take ownership
199 // of the pointer and will delete it later.
200 void InsertChild(wxTreeListModelNode
* child
)
202 wxASSERT( child
->m_parent
== this );
204 // Our previous first child becomes the next sibling of the new child.
205 child
->m_next
= m_child
;
209 // Insert the given item as our next sibling. As above, the item must have
210 // the correct parent pointer and we take ownership of it.
211 void InsertNext(wxTreeListModelNode
* next
)
213 wxASSERT( next
->m_parent
== m_parent
);
215 next
->m_next
= m_next
;
219 // Remove the first child of this item from the tree and delete it.
222 wxTreeListModelNode
* const oldChild
= m_child
;
223 m_child
= m_child
->m_next
;
227 // Remove the next sibling of this item from the tree and deletes it.
230 wxTreeListModelNode
* const oldNext
= m_next
;
231 m_next
= m_next
->m_next
;
236 // Functions for tree traversal. All of them can return NULL.
238 // Only returns NULL when called on the root item.
239 wxTreeListModelNode
* GetParent() const { return m_parent
; }
241 // Returns the first child of this item.
242 wxTreeListModelNode
* GetChild() const { return m_child
; }
244 // Returns the next sibling of this item.
245 wxTreeListModelNode
* GetNext() const { return m_next
; }
247 // Unlike the previous two functions, this one is not a simple accessor
248 // (hence it's not called "GetSomething") but computes the next node after
249 // this one in tree order.
250 wxTreeListModelNode
* NextInTree() const
258 // Recurse upwards until we find the next sibling.
259 for ( wxTreeListModelNode
* node
= m_parent
; node
; node
= node
->m_parent
)
270 // The (never changing after creation) parent of this node and the possibly
271 // NULL pointers to its first child and next sibling.
272 wxTreeListModelNode
* const m_parent
;
273 wxTreeListModelNode
* m_child
;
274 wxTreeListModelNode
* m_next
;
276 // Client data pointer owned by the control. May be NULL.
277 wxClientData
* m_data
;
279 // Array of column values for all the columns except the first one. May be
280 // NULL if no values had been set for them.
281 wxString
* m_columnsTexts
;
284 // ----------------------------------------------------------------------------
285 // wxTreeListModel: wxDataViewModel implementation used by wxTreeListCtrl.
286 // ----------------------------------------------------------------------------
288 class wxTreeListModel
: public wxDataViewModel
291 typedef wxTreeListModelNode Node
;
293 // Unlike a general wxDataViewModel, this model can only be used with a
294 // single control at once. The main reason for this is that we need to
295 // support different icons for opened and closed items and the item state
296 // is associated with the control, not the model, so our GetValue() is also
297 // bound to it (otherwise, what would it return for an item expanded in one
298 // associated control and collapsed in another one?).
299 wxTreeListModel(wxTreeListCtrl
* treelist
);
300 virtual ~wxTreeListModel();
303 // Helpers for converting between wxDataViewItem and wxTreeListItem. These
304 // methods simply cast the pointer to/from wxDataViewItem except for the
305 // root node that we handle specially unless explicitly disabled.
307 // The advantage of using them is that they're greppable and stand out
308 // better, hopefully making the code more clear.
309 Node
* FromNonRootDVI(wxDataViewItem dvi
) const
311 return static_cast<Node
*>(dvi
.GetID());
314 Node
* FromDVI(wxDataViewItem dvi
) const
319 return FromNonRootDVI(dvi
);
322 wxDataViewItem
ToNonRootDVI(Node
* node
) const
324 return wxDataViewItem(node
);
327 wxDataViewItem
ToDVI(Node
* node
) const
329 // Our root item must be represented as NULL at wxDVC level to map to
330 // its own invisible root.
331 if ( !node
->GetParent() )
332 return wxDataViewItem();
334 return ToNonRootDVI(node
);
338 // Methods called by wxTreeListCtrl.
339 void InsertColumn(unsigned col
);
340 void DeleteColumn(unsigned col
);
343 Node
* InsertItem(Node
* parent
,
345 const wxString
& text
,
349 void DeleteItem(Node
* item
);
350 void DeleteAllItems();
352 Node
* GetRootItem() const { return m_root
; }
354 const wxString
& GetItemText(Node
* item
, unsigned col
) const;
355 void SetItemText(Node
* item
, unsigned col
, const wxString
& text
);
356 void SetItemImage(Node
* item
, int closed
, int opened
);
357 wxClientData
* GetItemData(Node
* item
) const;
358 void SetItemData(Node
* item
, wxClientData
* data
);
360 void CheckItem(Node
* item
, wxCheckBoxState checkedState
);
361 void ToggleItem(wxDataViewItem item
);
364 // Implement the base class pure virtual methods.
365 virtual unsigned GetColumnCount() const;
366 virtual wxString
GetColumnType(unsigned col
) const;
367 virtual void GetValue(wxVariant
& variant
,
368 const wxDataViewItem
& item
,
370 virtual bool SetValue(const wxVariant
& variant
,
371 const wxDataViewItem
& item
,
373 virtual wxDataViewItem
GetParent(const wxDataViewItem
& item
) const;
374 virtual bool IsContainer(const wxDataViewItem
& item
) const;
375 virtual bool HasContainerColumns(const wxDataViewItem
& item
) const;
376 virtual unsigned GetChildren(const wxDataViewItem
& item
,
377 wxDataViewItemArray
& children
) const;
378 virtual bool IsListModel() const { return m_isFlat
; }
381 // The control we're associated with.
382 wxTreeListCtrl
* const m_treelist
;
384 // The unique invisible root element.
387 // Number of columns we maintain.
388 unsigned m_numColumns
;
390 // Set to false as soon as we have more than one level, i.e. as soon as any
391 // items with non-root item as parent are added (and currently never reset
396 // ============================================================================
397 // wxDataViewCheckIconText[Renderer]: special renderer for our first column.
398 // ============================================================================
400 // Currently this class is private but it could be extracted and made part of
401 // public API later as could be used directly with wxDataViewCtrl as well.
405 const char* CHECK_ICON_TEXT_TYPE
= "wxDataViewCheckIconText";
407 // The value used by wxDataViewCheckIconTextRenderer
408 class wxDataViewCheckIconText
: public wxDataViewIconText
411 wxDataViewCheckIconText(const wxString
& text
= wxString(),
412 const wxIcon
& icon
= wxNullIcon
,
413 wxCheckBoxState checkedState
= wxCHK_UNDETERMINED
)
414 : wxDataViewIconText(text
, icon
),
415 m_checkedState(checkedState
)
419 wxDataViewCheckIconText(const wxDataViewCheckIconText
& other
)
420 : wxDataViewIconText(other
),
421 m_checkedState(other
.m_checkedState
)
425 bool IsSameAs(const wxDataViewCheckIconText
& other
) const
427 return wxDataViewIconText::IsSameAs(other
) &&
428 m_checkedState
== other
.m_checkedState
;
431 // There is no encapsulation anyhow, so just expose this field directly.
432 wxCheckBoxState m_checkedState
;
436 wxDECLARE_DYNAMIC_CLASS(wxDataViewCheckIconText
);
439 wxIMPLEMENT_DYNAMIC_CLASS(wxDataViewCheckIconText
, wxDataViewIconText
);
441 DECLARE_VARIANT_OBJECT(wxDataViewCheckIconText
)
442 IMPLEMENT_VARIANT_OBJECT(wxDataViewCheckIconText
)
445 class wxDataViewCheckIconTextRenderer
: public wxDataViewCustomRenderer
448 wxDataViewCheckIconTextRenderer()
449 : wxDataViewCustomRenderer(CHECK_ICON_TEXT_TYPE
,
450 wxDATAVIEW_CELL_ACTIVATABLE
)
454 virtual bool SetValue(const wxVariant
& value
)
460 virtual bool GetValue(wxVariant
& WXUNUSED(value
)) const
465 wxSize
GetSize() const
467 wxSize size
= GetCheckSize();
468 size
.x
+= MARGIN_CHECK_ICON
;
470 if ( m_value
.GetIcon().IsOk() )
472 const wxSize sizeIcon
= m_value
.GetIcon().GetSize();
473 if ( sizeIcon
.y
> size
.y
)
476 size
.x
+= sizeIcon
.x
+ MARGIN_ICON_TEXT
;
479 wxString text
= m_value
.GetText();
483 const wxSize sizeText
= GetTextExtent(text
);
484 if ( sizeText
.y
> size
.y
)
487 size
.x
+= sizeText
.x
;
492 virtual bool Render(wxRect cell
, wxDC
* dc
, int state
)
494 // Draw the checkbox first.
496 switch ( m_value
.m_checkedState
)
498 case wxCHK_UNCHECKED
:
502 renderFlags
|= wxCONTROL_CHECKED
;
505 case wxCHK_UNDETERMINED
:
506 renderFlags
|= wxCONTROL_UNDETERMINED
;
510 if ( state
& wxDATAVIEW_CELL_PRELIT
)
511 renderFlags
|= wxCONTROL_CURRENT
;
513 const wxSize sizeCheck
= GetCheckSize();
515 wxRect
rectCheck(cell
.GetPosition(), sizeCheck
);
516 rectCheck
= rectCheck
.CentreIn(cell
, wxVERTICAL
);
518 wxRendererNative::Get().DrawCheckBox
520 GetView(), *dc
, rectCheck
, renderFlags
523 // Then the icon, if any.
524 int xoffset
= sizeCheck
.x
+ MARGIN_CHECK_ICON
;
526 const wxIcon
& icon
= m_value
.GetIcon();
529 const wxSize sizeIcon
= icon
.GetSize();
530 wxRect
rectIcon(cell
.GetPosition(), sizeIcon
);
531 rectIcon
.x
+= xoffset
;
532 rectIcon
= rectIcon
.CentreIn(cell
, wxVERTICAL
);
534 dc
->DrawIcon(icon
, rectIcon
.GetPosition());
536 xoffset
+= sizeIcon
.x
+ MARGIN_ICON_TEXT
;
540 RenderText(m_value
.GetText(), xoffset
, cell
, dc
, state
);
545 // Event handlers toggling the items checkbox if it was clicked.
546 virtual bool Activate(const wxRect
& WXUNUSED(cell
),
547 wxDataViewModel
* model
,
548 const wxDataViewItem
& item
,
549 unsigned int WXUNUSED(col
))
551 static_cast<wxTreeListModel
*>(model
)->ToggleItem(item
);
555 virtual bool LeftClick(const wxPoint
& pos
,
556 const wxRect
& WXUNUSED(cell
),
557 wxDataViewModel
* model
,
558 const wxDataViewItem
& item
,
559 unsigned int WXUNUSED(col
))
561 if ( !wxRect(GetCheckSize()).Contains(pos
) )
564 static_cast<wxTreeListModel
*>(model
)->ToggleItem(item
);
569 wxSize
GetCheckSize() const
571 return wxRendererNative::Get().GetCheckBoxSize(GetView());
575 // Just some arbitrary constants defining margins, in pixels.
578 MARGIN_CHECK_ICON
= 3,
582 wxDataViewCheckIconText m_value
;
585 } // anonymous namespace
587 // ============================================================================
588 // wxTreeListModel implementation
589 // ============================================================================
591 wxTreeListModel::wxTreeListModel(wxTreeListCtrl
* treelist
)
592 : m_treelist(treelist
),
593 m_root(new Node(NULL
))
599 wxTreeListModel::~wxTreeListModel()
604 void wxTreeListModel::InsertColumn(unsigned col
)
608 // There is no need to update anything when inserting the first column.
609 if ( m_numColumns
== 1 )
612 // Update all the items as they may have texts for the old columns.
613 for ( Node
* node
= m_root
->GetChild(); node
; node
= node
->NextInTree() )
615 node
->OnInsertColumn(col
, m_numColumns
);
619 void wxTreeListModel::DeleteColumn(unsigned col
)
621 wxCHECK_RET( col
< m_numColumns
, "Invalid column index" );
623 // Update all the items to remove the text for the non first columns.
626 for ( Node
* node
= m_root
->GetChild(); node
; node
= node
->NextInTree() )
628 node
->OnDeleteColumn(col
, m_numColumns
);
635 void wxTreeListModel::ClearColumns()
639 for ( Node
* node
= m_root
->GetChild(); node
; node
= node
->NextInTree() )
641 node
->OnClearColumns();
646 wxTreeListModel::InsertItem(Node
* parent
,
648 const wxString
& text
,
653 wxCHECK_MSG( parent
, NULL
,
654 "Must have a valid parent (maybe GetRootItem()?)" );
656 wxCHECK_MSG( previous
, NULL
,
657 "Must have a valid previous item (maybe wxTLI_FIRST/LAST?)" );
659 if ( m_isFlat
&& parent
!= m_root
)
661 // Not flat any more, this is a second level child.
666 newItem(new Node(parent
, text
, imageClosed
, imageOpened
, data
));
668 // If we have no children at all, then inserting as last child is the same
669 // as inserting as the first one so check for it here too.
670 if ( previous
== wxTLI_FIRST
||
671 (previous
== wxTLI_LAST
&& !parent
->GetChild()) )
673 parent
->InsertChild(newItem
.get());
675 else // Not the first item, find the previous one.
677 if ( previous
== wxTLI_LAST
)
679 previous
= parent
->GetChild();
681 // Find the last child.
684 Node
* const next
= previous
->GetNext();
691 else // We already have the previous item.
693 // Just check it's under the correct parent.
694 wxCHECK_MSG( previous
->GetParent() == parent
, NULL
,
695 "Previous item is not under the right parent" );
698 previous
->InsertNext(newItem
.get());
701 ItemAdded(ToDVI(parent
), ToDVI(newItem
.get()));
703 // The item was successfully inserted in the tree and so will be deleted by
704 // it, we can detach it now.
705 return newItem
.release();
708 void wxTreeListModel::DeleteItem(Node
* item
)
710 wxCHECK_RET( item
, "Invalid item" );
712 wxCHECK_RET( item
!= m_root
, "Can't delete the root item" );
714 Node
* const parent
= item
->GetParent();
716 ItemDeleted(ToDVI(parent
), ToDVI(item
));
718 Node
* previous
= parent
->GetChild();
719 if ( previous
== item
)
721 parent
->DeleteChild();
723 else // Not the first child of its parent.
725 // Find the sibling just before it.
728 Node
* const next
= previous
->GetNext();
732 wxCHECK_RET( next
, "Item not a child of its parent?" );
737 previous
->DeleteNext();
741 void wxTreeListModel::DeleteAllItems()
743 while ( m_root
->GetChild() )
745 m_root
->DeleteChild();
751 const wxString
& wxTreeListModel::GetItemText(Node
* item
, unsigned col
) const
753 // Returning root item text here is bogus, it just happens to be an always
754 // empty string we can return reference to.
755 wxCHECK_MSG( item
, m_root
->m_text
, "Invalid item" );
757 return col
== 0 ? item
->m_text
: item
->GetColumnText(col
);
760 void wxTreeListModel::SetItemText(Node
* item
, unsigned col
, const wxString
& text
)
762 wxCHECK_RET( item
, "Invalid item" );
767 item
->SetColumnText(text
, col
, m_numColumns
);
769 ValueChanged(ToDVI(item
), col
);
772 void wxTreeListModel::SetItemImage(Node
* item
, int closed
, int opened
)
774 wxCHECK_RET( item
, "Invalid item" );
776 item
->m_imageClosed
= closed
;
777 item
->m_imageOpened
= opened
;
779 ValueChanged(ToDVI(item
), 0);
782 wxClientData
* wxTreeListModel::GetItemData(Node
* item
) const
784 wxCHECK_MSG( item
, NULL
, "Invalid item" );
786 return item
->GetClientData();
789 void wxTreeListModel::SetItemData(Node
* item
, wxClientData
* data
)
791 wxCHECK_RET( item
, "Invalid item" );
793 item
->SetClientData(data
);
796 void wxTreeListModel::CheckItem(Node
* item
, wxCheckBoxState checkedState
)
798 wxCHECK_RET( item
, "Invalid item" );
800 item
->m_checkedState
= checkedState
;
802 ItemChanged(ToDVI(item
));
805 void wxTreeListModel::ToggleItem(wxDataViewItem dvi
)
807 Node
* const item
= FromDVI(dvi
);
809 wxCHECK_RET( item
, "Invalid item" );
811 const wxCheckBoxState stateOld
= item
->m_checkedState
;
813 // If the 3rd state is user-settable then the cycle is
814 // unchecked->checked->undetermined.
818 item
->m_checkedState
= m_treelist
->HasFlag(wxTL_USER_3STATE
)
823 case wxCHK_UNDETERMINED
:
824 // Whether 3rd state is user-settable or not, the next state is
826 item
->m_checkedState
= wxCHK_UNCHECKED
;
829 case wxCHK_UNCHECKED
:
830 item
->m_checkedState
= wxCHK_CHECKED
;
834 ItemChanged(ToDVI(item
));
836 m_treelist
->OnItemToggled(item
, stateOld
);
839 unsigned wxTreeListModel::GetColumnCount() const
844 wxString
wxTreeListModel::GetColumnType(unsigned col
) const
848 return m_treelist
->HasFlag(wxTL_CHECKBOX
)
849 ? wxS("wxDataViewCheckIconText")
850 : wxS("wxDataViewIconText");
852 else // All the other columns contain just text.
854 return wxS("string");
859 wxTreeListModel::GetValue(wxVariant
& variant
,
860 const wxDataViewItem
& item
,
863 Node
* const node
= FromDVI(item
);
867 // Determine the correct image to use depending on the item state.
868 int image
= wxWithImages::NO_IMAGE
;
869 if ( m_treelist
->IsExpanded(node
) )
870 image
= node
->m_imageOpened
;
872 if ( image
== wxWithImages::NO_IMAGE
)
873 image
= node
->m_imageClosed
;
875 wxIcon icon
= m_treelist
->GetImage(image
);
877 if ( m_treelist
->HasFlag(wxTL_CHECKBOX
) )
878 variant
<< wxDataViewCheckIconText(node
->m_text
, icon
,
879 node
->m_checkedState
);
881 variant
<< wxDataViewIconText(node
->m_text
, icon
);
885 // Notice that we must still assign wxString to wxVariant to ensure
886 // that it at least has the correct type.
888 if ( node
->HasColumnsTexts() )
889 text
= node
->GetColumnText(col
);
896 wxTreeListModel::SetValue(const wxVariant
& WXUNUSED(variant
),
897 const wxDataViewItem
& WXUNUSED(item
),
898 unsigned WXUNUSED(col
))
900 // We are not editable currently.
904 wxDataViewItem
wxTreeListModel::GetParent(const wxDataViewItem
& item
) const
906 Node
* const node
= FromDVI(item
);
908 return ToDVI(node
->GetParent());
911 bool wxTreeListModel::IsContainer(const wxDataViewItem
& item
) const
913 // FIXME: In the generic (and native OS X) versions we implement this
914 // method normally, i.e. only items with children are containers.
915 // But for the native GTK version we must pretend that all items are
916 // containers because otherwise adding children to them later would
917 // fail because wxGTK code calls IsContainer() too early (when
918 // adding the item itself) and we can't know whether we're container
919 // or not by then. Luckily, always returning true doesn't have any
920 // serious drawbacks for us.
926 Node
* const node
= FromDVI(item
);
928 return node
->GetChild() != NULL
;
933 wxTreeListModel::HasContainerColumns(const wxDataViewItem
& WXUNUSED(item
)) const
939 wxTreeListModel::GetChildren(const wxDataViewItem
& item
,
940 wxDataViewItemArray
& children
) const
942 Node
* const node
= FromDVI(item
);
944 unsigned numChildren
= 0;
945 for ( Node
* child
= node
->GetChild(); child
; child
= child
->GetNext() )
947 children
.push_back(ToDVI(child
));
954 // ============================================================================
955 // wxTreeListCtrl implementation
956 // ============================================================================
958 BEGIN_EVENT_TABLE(wxTreeListCtrl
, wxWindow
)
959 EVT_DATAVIEW_SELECTION_CHANGED(wxID_ANY
, wxTreeListCtrl::OnSelectionChanged
)
960 EVT_DATAVIEW_ITEM_EXPANDING(wxID_ANY
, wxTreeListCtrl::OnItemExpanding
)
961 EVT_DATAVIEW_ITEM_EXPANDED(wxID_ANY
, wxTreeListCtrl::OnItemExpanded
)
962 EVT_DATAVIEW_ITEM_ACTIVATED(wxID_ANY
, wxTreeListCtrl::OnItemActivated
)
963 EVT_DATAVIEW_ITEM_CONTEXT_MENU(wxID_ANY
, wxTreeListCtrl::OnItemContextMenu
)
965 EVT_SIZE(wxTreeListCtrl::OnSize
)
968 // ----------------------------------------------------------------------------
970 // ----------------------------------------------------------------------------
972 void wxTreeListCtrl::Init()
978 bool wxTreeListCtrl::Create(wxWindow
* parent
,
983 const wxString
& name
)
985 if ( style
& wxTL_USER_3STATE
)
986 style
|= wxTL_3STATE
;
988 if ( style
& wxTL_3STATE
)
989 style
|= wxTL_CHECKBOX
;
991 // Create the window itself and wxDataViewCtrl used by it.
992 if ( !wxWindow::Create(parent
, id
,
999 m_view
= new wxDataViewCtrl
;
1000 if ( !m_view
->Create(this, wxID_ANY
,
1001 wxPoint(0, 0), GetClientSize(),
1002 HasFlag(wxTL_MULTIPLE
) ? wxDV_MULTIPLE
1012 // Set up the model for wxDataViewCtrl.
1013 m_model
= new wxTreeListModel(this);
1014 m_view
->AssociateModel(m_model
);
1019 wxTreeListCtrl::~wxTreeListCtrl()
1025 // ----------------------------------------------------------------------------
1027 // ----------------------------------------------------------------------------
1030 wxTreeListCtrl::DoInsertColumn(const wxString
& title
,
1036 wxCHECK_MSG( m_view
, wxNOT_FOUND
, "Must Create() first" );
1038 const unsigned oldNumColumns
= m_view
->GetColumnCount();
1040 if ( pos
== wxNOT_FOUND
)
1041 pos
= oldNumColumns
;
1043 wxDataViewRenderer
* renderer
;
1046 // Inserting the first column which is special as it uses a different
1049 // Also, currently it can be done only once.
1050 wxCHECK_MSG( !oldNumColumns
, wxNOT_FOUND
,
1051 "Inserting column at position 0 currently not supported" );
1053 if ( HasFlag(wxTL_CHECKBOX
) )
1055 // Use our custom renderer to show the checkbox.
1056 renderer
= new wxDataViewCheckIconTextRenderer
;
1058 else // We still need a special renderer to show the icons.
1060 renderer
= new wxDataViewIconTextRenderer
;
1063 else // Not the first column.
1065 // All the other ones use a simple text renderer.
1066 renderer
= new wxDataViewTextRenderer
;
1070 column
= new wxDataViewColumn(title
, renderer
, pos
, width
, align
, flags
);
1072 m_model
->InsertColumn(pos
);
1074 m_view
->InsertColumn(pos
, column
);
1079 unsigned wxTreeListCtrl::GetColumnCount() const
1081 return m_view
? m_view
->GetColumnCount() : 0u;
1084 bool wxTreeListCtrl::DeleteColumn(unsigned col
)
1086 wxCHECK_MSG( col
< GetColumnCount(), false, "Invalid column index" );
1088 if ( !m_view
->DeleteColumn(m_view
->GetColumn(col
)) )
1091 m_model
->DeleteColumn(col
);
1096 void wxTreeListCtrl::ClearColumns()
1098 // Don't assert here, clearing columns of the control before it's created
1099 // can be considered valid (just useless).
1103 m_view
->ClearColumns();
1105 m_model
->ClearColumns();
1108 void wxTreeListCtrl::SetColumnWidth(unsigned col
, int width
)
1110 wxCHECK_RET( col
< GetColumnCount(), "Invalid column index" );
1112 wxDataViewColumn
* const column
= m_view
->GetColumn(col
);
1113 wxCHECK_RET( column
, "No such column?" );
1115 return column
->SetWidth(width
);
1118 int wxTreeListCtrl::GetColumnWidth(unsigned col
) const
1120 wxCHECK_MSG( col
< GetColumnCount(), -1, "Invalid column index" );
1122 wxDataViewColumn
* column
= m_view
->GetColumn(col
);
1123 wxCHECK_MSG( column
, -1, "No such column?" );
1125 return column
->GetWidth();
1128 int wxTreeListCtrl::WidthFor(const wxString
& text
) const
1130 return GetTextExtent(text
).x
;
1133 // ----------------------------------------------------------------------------
1135 // ----------------------------------------------------------------------------
1138 wxTreeListCtrl::DoInsertItem(wxTreeListItem parent
,
1139 wxTreeListItem previous
,
1140 const wxString
& text
,
1145 wxCHECK_MSG( m_model
, wxTreeListItem(), "Must create first" );
1147 return wxTreeListItem(m_model
->InsertItem(parent
, previous
, text
,
1148 imageClosed
, imageOpened
, data
));
1151 void wxTreeListCtrl::DeleteItem(wxTreeListItem item
)
1153 wxCHECK_RET( m_model
, "Must create first" );
1155 m_model
->DeleteItem(item
);
1158 void wxTreeListCtrl::DeleteAllItems()
1161 m_model
->DeleteAllItems();
1164 // ----------------------------------------------------------------------------
1166 // ----------------------------------------------------------------------------
1168 // The simple accessors in this section are implemented directly using
1169 // wxTreeListModelNode methods, without passing by the model. This is just a
1170 // shortcut and avoids us the trouble of defining more trivial methods in
1173 wxTreeListItem
wxTreeListCtrl::GetRootItem() const
1175 wxCHECK_MSG( m_model
, wxTreeListItem(), "Must create first" );
1177 return m_model
->GetRootItem();
1180 wxTreeListItem
wxTreeListCtrl::GetItemParent(wxTreeListItem item
) const
1182 wxCHECK_MSG( item
.IsOk(), wxTreeListItem(), "Invalid item" );
1184 return item
->GetParent();
1187 wxTreeListItem
wxTreeListCtrl::GetFirstChild(wxTreeListItem item
) const
1189 wxCHECK_MSG( item
.IsOk(), wxTreeListItem(), "Invalid item" );
1191 return item
->GetChild();
1195 wxTreeListCtrl::GetNextSibling(wxTreeListItem item
) const
1197 wxCHECK_MSG( item
.IsOk(), wxTreeListItem(), "Invalid item" );
1199 return item
->GetNext();
1202 wxTreeListItem
wxTreeListCtrl::GetNextItem(wxTreeListItem item
) const
1204 wxCHECK_MSG( item
.IsOk(), wxTreeListItem(), "Invalid item" );
1206 return item
->NextInTree();
1209 // ----------------------------------------------------------------------------
1211 // ----------------------------------------------------------------------------
1214 wxTreeListCtrl::GetItemText(wxTreeListItem item
, unsigned col
) const
1216 // We can't use wxCHECK_MSG() here because we don't have any empty string
1217 // reference to return so we use a static variable that exists just for the
1218 // purpose of this check -- and so we put it in its own scope so that it's
1219 // never even created during normal program execution.
1220 if ( !m_model
|| col
>= m_model
->GetColumnCount() )
1222 static wxString s_empty
;
1226 wxFAIL_MSG( "Must create first" );
1228 else if ( col
>= m_model
->GetColumnCount() )
1230 wxFAIL_MSG( "Invalid column index" );
1236 return m_model
->GetItemText(item
, col
);
1240 wxTreeListCtrl::SetItemText(wxTreeListItem item
,
1242 const wxString
& text
)
1244 wxCHECK_RET( m_model
, "Must create first" );
1245 wxCHECK_RET( col
< m_model
->GetColumnCount(), "Invalid column index" );
1247 m_model
->SetItemText(item
, col
, text
);
1250 void wxTreeListCtrl::SetItemImage(wxTreeListItem item
, int closed
, int opened
)
1252 wxCHECK_RET( m_model
, "Must create first" );
1254 if ( closed
!= NO_IMAGE
|| opened
!= NO_IMAGE
)
1256 wxImageList
* const imageList
= GetImageList();
1257 wxCHECK_RET( imageList
, "Can't set images without image list" );
1259 const int imageCount
= imageList
->GetImageCount();
1261 wxCHECK_RET( closed
< imageCount
, "Invalid image index" );
1262 wxCHECK_RET( opened
< imageCount
, "Invalid opened image index" );
1265 m_model
->SetItemImage(item
, closed
, opened
);
1268 wxClientData
* wxTreeListCtrl::GetItemData(wxTreeListItem item
) const
1270 wxCHECK_MSG( m_model
, NULL
, "Must create first" );
1272 return m_model
->GetItemData(item
);
1275 void wxTreeListCtrl::SetItemData(wxTreeListItem item
, wxClientData
* data
)
1277 wxCHECK_RET( m_model
, "Must create first" );
1279 m_model
->SetItemData(item
, data
);
1282 // ----------------------------------------------------------------------------
1283 // Expanding and collapsing
1284 // ----------------------------------------------------------------------------
1286 void wxTreeListCtrl::Expand(wxTreeListItem item
)
1288 wxCHECK_RET( m_view
, "Must create first" );
1290 m_view
->Expand(m_model
->ToDVI(item
));
1293 void wxTreeListCtrl::Collapse(wxTreeListItem item
)
1295 wxCHECK_RET( m_view
, "Must create first" );
1297 m_view
->Collapse(m_model
->ToDVI(item
));
1300 bool wxTreeListCtrl::IsExpanded(wxTreeListItem item
) const
1302 wxCHECK_MSG( m_view
, false, "Must create first" );
1304 return m_view
->IsExpanded(m_model
->ToDVI(item
));
1307 // ----------------------------------------------------------------------------
1309 // ----------------------------------------------------------------------------
1311 wxTreeListItem
wxTreeListCtrl::GetSelection() const
1313 wxCHECK_MSG( m_view
, wxTreeListItem(), "Must create first" );
1315 wxCHECK_MSG( !HasFlag(wxTL_MULTIPLE
), wxTreeListItem(),
1316 "Must use GetSelections() with multi-selection controls!" );
1318 const wxDataViewItem dvi
= m_view
->GetSelection();
1320 return m_model
->FromNonRootDVI(dvi
);
1323 unsigned wxTreeListCtrl::GetSelections(wxTreeListItems
& selections
) const
1325 wxCHECK_MSG( m_view
, 0, "Must create first" );
1327 wxDataViewItemArray selectionsDV
;
1328 const unsigned numSelected
= m_view
->GetSelections(selectionsDV
);
1329 selections
.resize(numSelected
);
1330 for ( unsigned n
= 0; n
< numSelected
; n
++ )
1331 selections
[n
] = m_model
->FromNonRootDVI(selectionsDV
[n
]);
1336 void wxTreeListCtrl::Select(wxTreeListItem item
)
1338 wxCHECK_RET( m_view
, "Must create first" );
1340 m_view
->Select(m_model
->ToNonRootDVI(item
));
1343 void wxTreeListCtrl::Unselect(wxTreeListItem item
)
1345 wxCHECK_RET( m_view
, "Must create first" );
1347 m_view
->Unselect(m_model
->ToNonRootDVI(item
));
1350 bool wxTreeListCtrl::IsSelected(wxTreeListItem item
) const
1352 wxCHECK_MSG( m_view
, false, "Must create first" );
1354 return m_view
->IsSelected(m_model
->ToNonRootDVI(item
));
1357 void wxTreeListCtrl::SelectAll()
1359 wxCHECK_RET( m_view
, "Must create first" );
1361 m_view
->SelectAll();
1364 void wxTreeListCtrl::UnselectAll()
1366 wxCHECK_RET( m_view
, "Must create first" );
1368 m_view
->UnselectAll();
1371 // ----------------------------------------------------------------------------
1372 // Checkbox handling
1373 // ----------------------------------------------------------------------------
1375 void wxTreeListCtrl::CheckItem(wxTreeListItem item
, wxCheckBoxState state
)
1377 wxCHECK_RET( m_model
, "Must create first" );
1379 m_model
->CheckItem(item
, state
);
1383 wxTreeListCtrl::CheckItemRecursively(wxTreeListItem item
, wxCheckBoxState state
)
1385 wxCHECK_RET( m_model
, "Must create first" );
1387 m_model
->CheckItem(item
, state
);
1389 for ( wxTreeListItem child
= GetFirstChild(item
);
1391 child
= GetNextSibling(child
) )
1393 CheckItemRecursively(child
, state
);
1397 void wxTreeListCtrl::UpdateItemParentStateRecursively(wxTreeListItem item
)
1399 wxCHECK_RET( item
.IsOk(), "Invalid item" );
1401 wxASSERT_MSG( HasFlag(wxTL_3STATE
), "Can only be used with wxTL_3STATE" );
1405 wxTreeListItem parent
= GetItemParent(item
);
1406 if ( parent
== GetRootItem() )
1408 // There is no checked state associated with the root item.
1412 // Set parent state to the state of this item if all the other children
1413 // have the same state too. Otherwise make it indeterminate.
1414 const wxCheckBoxState stateItem
= GetCheckedState(item
);
1415 CheckItem(parent
, AreAllChildrenInState(parent
, stateItem
)
1417 : wxCHK_UNDETERMINED
);
1419 // And do the same thing with the parent's parent too.
1424 wxCheckBoxState
wxTreeListCtrl::GetCheckedState(wxTreeListItem item
) const
1426 wxCHECK_MSG( item
.IsOk(), wxCHK_UNDETERMINED
, "Invalid item" );
1428 return item
->m_checkedState
;
1432 wxTreeListCtrl::AreAllChildrenInState(wxTreeListItem item
,
1433 wxCheckBoxState state
) const
1435 wxCHECK_MSG( item
.IsOk(), false, "Invalid item" );
1437 for ( wxTreeListItem child
= GetFirstChild(item
);
1439 child
= GetNextSibling(child
) )
1441 if ( GetCheckedState(child
) != state
)
1448 // ----------------------------------------------------------------------------
1450 // ----------------------------------------------------------------------------
1452 void wxTreeListCtrl::SendEvent(wxEventType evt
, wxDataViewEvent
& eventDV
)
1454 wxTreeListEvent
eventTL(evt
, this, m_model
->FromDVI(eventDV
.GetItem()));
1456 if ( !ProcessWindowEvent(eventTL
) )
1462 if ( !eventTL
.IsAllowed() )
1469 wxTreeListCtrl::OnItemToggled(wxTreeListItem item
, wxCheckBoxState stateOld
)
1471 wxTreeListEvent
event(wxEVT_COMMAND_TREELIST_ITEM_CHECKED
, this, item
);
1472 event
.SetOldCheckedState(stateOld
);
1474 ProcessWindowEvent(event
);
1477 void wxTreeListCtrl::OnSelectionChanged(wxDataViewEvent
& event
)
1479 SendEvent(wxEVT_COMMAND_TREELIST_SELECTION_CHANGED
, event
);
1482 void wxTreeListCtrl::OnItemExpanding(wxDataViewEvent
& event
)
1484 SendEvent(wxEVT_COMMAND_TREELIST_ITEM_EXPANDING
, event
);
1487 void wxTreeListCtrl::OnItemExpanded(wxDataViewEvent
& event
)
1489 SendEvent(wxEVT_COMMAND_TREELIST_ITEM_EXPANDED
, event
);
1492 void wxTreeListCtrl::OnItemActivated(wxDataViewEvent
& event
)
1494 SendEvent(wxEVT_COMMAND_TREELIST_ITEM_ACTIVATED
, event
);
1497 void wxTreeListCtrl::OnItemContextMenu(wxDataViewEvent
& event
)
1499 SendEvent(wxEVT_COMMAND_TREELIST_ITEM_CONTEXT_MENU
, event
);
1502 // ----------------------------------------------------------------------------
1504 // ----------------------------------------------------------------------------
1506 void wxTreeListCtrl::OnSize(wxSizeEvent
& event
)
1512 // Resize the real control to cover our entire client area.
1513 const wxRect rect
= GetClientRect();
1514 m_view
->SetSize(rect
);
1516 // Resize the first column to take the remaining available space, if
1518 const unsigned numColumns
= GetColumnCount();
1522 // There is a bug in generic wxDataViewCtrl: if the column width sums
1523 // up to the total size, horizontal scrollbar (unnecessarily) appears,
1524 // so subtract 10 pixels to ensure this doesn't happen.
1525 int remainingWidth
= rect
.width
- 10;
1526 for ( unsigned n
= 1; n
< GetColumnCount(); n
++ )
1528 remainingWidth
-= GetColumnWidth(n
);
1529 if ( remainingWidth
< 0 )
1533 // We don't decrease the width of the first column, even if we had
1534 // increased it ourselves, because we want to avoid changing its size
1535 // if the user resized it. We might want to remember if this was the
1536 // case or if we only ever adjusted it automatically in the future.
1537 if ( remainingWidth
> GetColumnWidth(0) )
1538 SetColumnWidth(0, remainingWidth
);
1542 // ============================================================================
1543 // wxTreeListEvent implementation
1544 // ============================================================================
1546 wxIMPLEMENT_ABSTRACT_CLASS(wxTreeListEvent
, wxNotifyEvent
)
1548 #define wxDEFINE_TREELIST_EVENT(name) \
1549 wxDEFINE_EVENT(wxEVT_COMMAND_TREELIST_##name, wxTreeListEvent)
1551 wxDEFINE_TREELIST_EVENT(SELECTION_CHANGED
);
1552 wxDEFINE_TREELIST_EVENT(ITEM_EXPANDING
);
1553 wxDEFINE_TREELIST_EVENT(ITEM_EXPANDED
);
1554 wxDEFINE_TREELIST_EVENT(ITEM_CHECKED
);
1555 wxDEFINE_TREELIST_EVENT(ITEM_ACTIVATED
);
1556 wxDEFINE_TREELIST_EVENT(ITEM_CONTEXT_MENU
);
1558 #undef wxDEFINE_TREELIST_EVENT