1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeListCtrl class declaration.
4 // Author: Vadim Zeitlin
6 // RCS-ID: $Id: wxhead.h,v 1.12 2010-04-22 12:44:51 zeitlin Exp $
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_TREELIST_H_
12 #define _WX_TREELIST_H_
16 #if wxUSE_TREELISTCTRL
18 #include "wx/containr.h"
19 #include "wx/headercol.h"
20 #include "wx/itemid.h"
21 #include "wx/vector.h"
22 #include "wx/window.h"
23 #include "wx/withimages.h"
25 class WXDLLIMPEXP_FWD_ADV wxDataViewCtrl
;
26 class WXDLLIMPEXP_FWD_ADV wxDataViewEvent
;
28 extern WXDLLIMPEXP_DATA_CORE(const char) wxTreeListCtrlNameStr
[];
30 class wxTreeListModel
;
31 class wxTreeListModelNode
;
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // wxTreeListCtrl styles.
39 // Notice that using wxTL_USER_3STATE implies wxTL_3STATE and wxTL_3STATE in
40 // turn implies wxTL_CHECKBOX.
43 wxTL_SINGLE
= 0x0000, // This is the default anyhow.
44 wxTL_MULTIPLE
= 0x0001, // Allow multiple selection.
45 wxTL_CHECKBOX
= 0x0002, // Show checkboxes in the first column.
46 wxTL_3STATE
= 0x0004, // Allow 3rd state in checkboxes.
47 wxTL_USER_3STATE
= 0x0008, // Allow user to set 3rd state.
49 wxTL_DEFAULT_STYLE
= wxTL_SINGLE
,
50 wxTL_STYLE_MASK
= wxTL_SINGLE
|
57 // ----------------------------------------------------------------------------
58 // wxTreeListItem: unique identifier of an item in wxTreeListCtrl.
59 // ----------------------------------------------------------------------------
61 // Make wxTreeListItem a forward-declarable class even though it's simple
62 // enough to possibly be declared as a simple typedef.
63 class wxTreeListItem
: public wxItemId
<wxTreeListModelNode
*>
66 wxTreeListItem(wxTreeListModelNode
* item
= NULL
)
67 : wxItemId
<wxTreeListModelNode
*>(item
)
72 // Container of multiple items.
73 typedef wxVector
<wxTreeListItem
> wxTreeListItems
;
75 // Some special "items" that can be used with InsertItem():
76 extern WXDLLIMPEXP_DATA_ADV(const wxTreeListItem
) wxTLI_FIRST
;
77 extern WXDLLIMPEXP_DATA_ADV(const wxTreeListItem
) wxTLI_LAST
;
79 // ----------------------------------------------------------------------------
80 // wxTreeListCtrl: a control combining wxTree- and wxListCtrl features.
81 // ----------------------------------------------------------------------------
83 // This control also provides easy to use high level interface. Although the
84 // implementation uses wxDataViewCtrl internally, this class is intentionally
85 // simpler than wxDataViewCtrl and doesn't provide all of its functionality.
87 // If you need extra features you can always use GetDataView() accessor to work
88 // with wxDataViewCtrl directly but doing this makes your unportable to possible
89 // future non-wxDataViewCtrl-based implementations of this class.
91 class WXDLLIMPEXP_ADV wxTreeListCtrl
: public wxNavigationEnabled
<wxWindow
>,
95 // Constructors and such
96 // ---------------------
98 wxTreeListCtrl() { Init(); }
99 wxTreeListCtrl(wxWindow
* parent
,
101 const wxPoint
& pos
= wxDefaultPosition
,
102 const wxSize
& size
= wxDefaultSize
,
103 long style
= wxTL_DEFAULT_STYLE
,
104 const wxString
& name
= wxTreeListCtrlNameStr
)
108 Create(parent
, id
, pos
, size
, style
, name
);
111 bool Create(wxWindow
* parent
,
113 const wxPoint
& pos
= wxDefaultPosition
,
114 const wxSize
& size
= wxDefaultSize
,
115 long style
= wxTL_DEFAULT_STYLE
,
116 const wxString
& name
= wxTreeListCtrlNameStr
);
119 virtual ~wxTreeListCtrl();
124 // Add a column with the given title and attributes, returns the index of
125 // the new column or -1 on failure.
126 int AppendColumn(const wxString
& title
,
127 int width
= wxCOL_WIDTH_AUTOSIZE
,
128 wxAlignment align
= wxALIGN_LEFT
,
129 int flags
= wxCOL_RESIZABLE
)
131 return DoInsertColumn(title
, -1, width
, align
, flags
);
134 // Return the total number of columns.
135 unsigned GetColumnCount() const;
137 // Delete the column with the given index, returns false if index is
138 // invalid or deleting the column failed for some other reason.
139 bool DeleteColumn(unsigned col
);
141 // Delete all columns.
144 // Set column width to either the given value in pixels or to the value
145 // large enough to fit all of the items if width == wxCOL_WIDTH_AUTOSIZE.
146 void SetColumnWidth(unsigned col
, int width
);
148 // Get the current width of the given column in pixels.
149 int GetColumnWidth(unsigned col
) const;
151 // Get the width appropriate for showing the given text. This is typically
152 // used as second argument for AppendColumn() or with SetColumnWidth().
153 int WidthFor(const wxString
& text
) const;
159 // Adding items. The parent and text of the first column of the new item
160 // must always be specified, the rest is optional.
162 // Each item can have two images: one used for closed state and another for
163 // opened one. Only the first one is ever used for the items that don't
164 // have children. And both are not set by default.
166 // It is also possible to associate arbitrary client data pointer with the
167 // new item. It will be deleted by the control when the item is deleted
168 // (either by an explicit DeleteItem() call or because the entire control
171 wxTreeListItem
AppendItem(wxTreeListItem parent
,
172 const wxString
& text
,
173 int imageClosed
= NO_IMAGE
,
174 int imageOpened
= NO_IMAGE
,
175 wxClientData
* data
= NULL
)
177 return DoInsertItem(parent
, wxTLI_LAST
, text
,
178 imageClosed
, imageOpened
, data
);
181 wxTreeListItem
InsertItem(wxTreeListItem parent
,
182 wxTreeListItem previous
,
183 const wxString
& text
,
184 int imageClosed
= NO_IMAGE
,
185 int imageOpened
= NO_IMAGE
,
186 wxClientData
* data
= NULL
)
188 return DoInsertItem(parent
, previous
, text
,
189 imageClosed
, imageOpened
, data
);
192 wxTreeListItem
PrependItem(wxTreeListItem parent
,
193 const wxString
& text
,
194 int imageClosed
= NO_IMAGE
,
195 int imageOpened
= NO_IMAGE
,
196 wxClientData
* data
= NULL
)
198 return DoInsertItem(parent
, wxTLI_FIRST
, text
,
199 imageClosed
, imageOpened
, data
);
203 void DeleteItem(wxTreeListItem item
);
204 void DeleteAllItems();
210 // Return the (never shown) root item.
211 wxTreeListItem
GetRootItem() const;
213 // The parent item may be invalid for the root-level items.
214 wxTreeListItem
GetItemParent(wxTreeListItem item
) const;
216 // Iterate over the given item children: start by calling GetFirstChild()
217 // and then call GetNextSibling() for as long as it returns valid item.
218 wxTreeListItem
GetFirstChild(wxTreeListItem item
) const;
219 wxTreeListItem
GetNextSibling(wxTreeListItem item
) const;
221 // Return the first child of the root item, which is also the first item of
222 // the tree in depth-first traversal order.
223 wxTreeListItem
GetFirstItem() const { return GetFirstChild(GetRootItem()); }
225 // Get item after the given one in the depth-first tree-traversal order.
226 // Calling this function starting with the result of GetFirstItem() allows
227 // iterating over all items in the tree.
228 wxTreeListItem
GetNextItem(wxTreeListItem item
) const;
234 const wxString
& GetItemText(wxTreeListItem item
, unsigned col
= 0) const;
236 // The convenience overload below sets the text for the first column.
237 void SetItemText(wxTreeListItem item
, unsigned col
, const wxString
& text
);
238 void SetItemText(wxTreeListItem item
, const wxString
& text
)
240 SetItemText(item
, 0, text
);
243 // By default the opened image is the same as the normal, closed one (if
244 // it's used at all).
245 void SetItemImage(wxTreeListItem item
, int closed
, int opened
= NO_IMAGE
);
247 // Retrieve or set the data associated with the item.
248 wxClientData
* GetItemData(wxTreeListItem item
) const;
249 void SetItemData(wxTreeListItem item
, wxClientData
* data
);
252 // Expanding and collapsing
253 // ------------------------
255 void Expand(wxTreeListItem item
);
256 void Collapse(wxTreeListItem item
);
257 bool IsExpanded(wxTreeListItem item
) const;
260 // Selection handling
261 // ------------------
263 // This function can be used with single selection controls, use
264 // GetSelections() with the multi-selection ones.
265 wxTreeListItem
GetSelection() const;
267 // This one can be used with either single or multi-selection controls.
268 unsigned GetSelections(wxTreeListItems
& selections
) const;
270 // In single selection mode Select() deselects any other selected items, in
271 // multi-selection case it adds to the selection.
272 void Select(wxTreeListItem item
);
274 // Can be used in multiple selection mode only, single selected item in the
275 // single selection mode can't be unselected.
276 void Unselect(wxTreeListItem item
);
278 // Return true if the item is selected, can be used in both single and
279 // multiple selection modes.
280 bool IsSelected(wxTreeListItem item
) const;
282 // Select or unselect all items, only valid in multiple selection mode.
290 // Methods in this section can only be used with the controls created with
291 // wxTL_CHECKBOX style.
293 // Simple set, unset or query the checked state.
294 void CheckItem(wxTreeListItem item
, wxCheckBoxState state
= wxCHK_CHECKED
);
295 void UncheckItem(wxTreeListItem item
) { CheckItem(item
, wxCHK_UNCHECKED
); }
297 // The same but do it recursively for this item itself and its children.
298 void CheckItemRecursively(wxTreeListItem item
,
299 wxCheckBoxState state
= wxCHK_CHECKED
);
301 // Update the parent of this item recursively: if this item and all its
302 // siblings are checked, the parent will become checked as well. If this
303 // item and all its siblings are unchecked, the parent will be unchecked.
304 // And if the siblings of this item are not all in the same state, the
305 // parent will be switched to indeterminate state. And then the same logic
306 // will be applied to the parents parent and so on recursively.
308 // This is typically called when the state of the given item has changed
309 // from EVT_TREELIST_ITEM_CHECKED() handler in the controls which have
310 // wxTL_3STATE flag. Notice that without this flag this function can't work
311 // as it would be unable to set the state of a parent with both checked and
312 // unchecked items so it's only allowed to call it when this flag is set.
313 void UpdateItemParentStateRecursively(wxTreeListItem item
);
315 // Return the current state.
316 wxCheckBoxState
GetCheckedState(wxTreeListItem item
) const;
318 // Return true if all item children (if any) are in the given state.
319 bool AreAllChildrenInState(wxTreeListItem item
,
320 wxCheckBoxState state
) const;
324 // Common part of all ctors.
327 // Implementation of AppendColumn().
328 int DoInsertColumn(const wxString
& title
,
329 int pos
, // May be -1 meaning "append".
334 // Common part of {Append,Insert,Prepend}Item().
335 wxTreeListItem
DoInsertItem(wxTreeListItem parent
,
336 wxTreeListItem previous
,
337 const wxString
& text
,
342 // Send wxTreeListEvent corresponding to the given wxDataViewEvent.
344 // Also updates the original event "skipped" and "vetoed" flags.
345 void SendEvent(wxEventType evt
, wxDataViewEvent
& event
);
348 // Called by wxTreeListModel when an item is toggled by the user.
349 void OnItemToggled(wxTreeListItem item
, wxCheckBoxState stateOld
);
352 void OnSelectionChanged(wxDataViewEvent
& event
);
353 void OnItemExpanding(wxDataViewEvent
& event
);
354 void OnItemExpanded(wxDataViewEvent
& event
);
355 void OnItemActivated(wxDataViewEvent
& event
);
356 void OnItemContextMenu(wxDataViewEvent
& event
);
357 void OnSize(wxSizeEvent
& event
);
359 wxDECLARE_EVENT_TABLE();
362 wxDataViewCtrl
* m_view
;
363 wxTreeListModel
* m_model
;
366 // It calls our inherited protected wxWithImages::GetImage() method.
367 friend class wxTreeListModel
;
369 wxDECLARE_NO_COPY_CLASS(wxTreeListCtrl
);
372 // ----------------------------------------------------------------------------
373 // wxTreeListEvent: event generated by wxTreeListCtrl.
374 // ----------------------------------------------------------------------------
376 class wxTreeListEvent
: public wxNotifyEvent
379 // The item affected by the event.
380 wxTreeListItem
GetItem() const { return m_item
; }
382 // The previous state of the item checkbox for ITEM_CHECKED events only.
383 wxCheckBoxState
GetOldCheckedState() const { return m_oldCheckedState
; }
386 virtual wxEvent
* Clone() const { return new wxTreeListEvent(*this); }
389 // Ctor is private, only wxTreeListCtrl can create events of this type.
390 wxTreeListEvent(wxEventType evtType
,
391 wxTreeListCtrl
* treelist
,
393 : wxNotifyEvent(evtType
, treelist
->GetId()),
396 SetEventObject(treelist
);
399 // Set the checkbox state before this event for ITEM_CHECKED events.
400 void SetOldCheckedState(wxCheckBoxState state
)
402 m_oldCheckedState
= state
;
406 const wxTreeListItem m_item
;
408 wxCheckBoxState m_oldCheckedState
;
410 friend class wxTreeListCtrl
;
412 wxDECLARE_ABSTRACT_CLASS(wxTreeListEvent
);
415 // Event types and event table macros.
417 typedef void (wxEvtHandler::*wxTreeListEventFunction
)(wxTreeListEvent
&);
419 #define wxTreeListEventHandler(func) \
420 wxEVENT_HANDLER_CAST(wxTreeListEventFunction, func)
422 #define wxEVT_TREELIST_GENERIC(name, id, fn) \
423 wx__DECLARE_EVT1(wxEVT_COMMAND_TREELIST_##name, id, wxTreeListEventHandler(fn))
425 #define wxDECLARE_TREELIST_EVENT(name) \
426 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, \
427 wxEVT_COMMAND_TREELIST_##name, \
430 wxDECLARE_TREELIST_EVENT(SELECTION_CHANGED
);
431 #define EVT_TREELIST_SELECTION_CHANGED(id, fn) \
432 wxEVT_TREELIST_GENERIC(SELECTION_CHANGED, id, fn)
434 wxDECLARE_TREELIST_EVENT(ITEM_EXPANDING
);
435 #define EVT_TREELIST_ITEM_EXPANDING(id, fn) \
436 wxEVT_TREELIST_GENERIC(ITEM_EXPANDING, id, fn)
438 wxDECLARE_TREELIST_EVENT(ITEM_EXPANDED
);
439 #define EVT_TREELIST_ITEM_EXPANDED(id, fn) \
440 wxEVT_TREELIST_GENERIC(ITEM_EXPANDED, id, fn)
442 wxDECLARE_TREELIST_EVENT(ITEM_CHECKED
);
443 #define EVT_TREELIST_ITEM_CHECKED(id, fn) \
444 wxEVT_TREELIST_GENERIC(ITEM_CHECKED, id, fn)
446 wxDECLARE_TREELIST_EVENT(ITEM_ACTIVATED
);
447 #define EVT_TREELIST_ITEM_ACTIVATED(id, fn) \
448 wxEVT_TREELIST_GENERIC(ITEM_ACTIVATED, id, fn)
450 wxDECLARE_TREELIST_EVENT(ITEM_CONTEXT_MENU
);
451 #define EVT_TREELIST_ITEM_CONTEXT_MENU(id, fn) \
452 wxEVT_TREELIST_GENERIC(ITEM_CONTEXT_MENU, id, fn)
454 #undef wxDECLARE_TREELIST_EVENT
456 #endif // wxUSE_TREELISTCTRL
458 #endif // _WX_TREELIST_H_