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/headercol.h"
19 #include "wx/itemid.h"
20 #include "wx/vector.h"
21 #include "wx/window.h"
22 #include "wx/withimages.h"
24 class WXDLLIMPEXP_FWD_ADV wxDataViewCtrl
;
25 class WXDLLIMPEXP_FWD_ADV wxDataViewEvent
;
27 extern WXDLLIMPEXP_DATA_CORE(const char) wxTreeListCtrlNameStr
[];
29 class wxTreeListModel
;
30 class wxTreeListModelNode
;
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 // wxTreeListCtrl styles.
38 // Notice that using wxTL_USER_3STATE implies wxTL_3STATE and wxTL_3STATE in
39 // turn implies wxTL_CHECKBOX.
42 wxTL_SINGLE
= 0x0000, // This is the default anyhow.
43 wxTL_MULTIPLE
= 0x0001, // Allow multiple selection.
44 wxTL_CHECKBOX
= 0x0002, // Show checkboxes in the first column.
45 wxTL_3STATE
= 0x0004, // Allow 3rd state in checkboxes.
46 wxTL_USER_3STATE
= 0x0008, // Allow user to set 3rd state.
48 wxTL_DEFAULT_STYLE
= wxTL_SINGLE
,
49 wxTL_STYLE_MASK
= wxTL_SINGLE
|
56 // ----------------------------------------------------------------------------
57 // wxTreeListItem: unique identifier of an item in wxTreeListCtrl.
58 // ----------------------------------------------------------------------------
60 // Make wxTreeListItem a forward-declarable class even though it's simple
61 // enough to possibly be declared as a simple typedef.
62 class wxTreeListItem
: public wxItemId
<wxTreeListModelNode
*>
65 wxTreeListItem(wxTreeListModelNode
* item
= NULL
)
66 : wxItemId
<wxTreeListModelNode
*>(item
)
71 // Container of multiple items.
72 typedef wxVector
<wxTreeListItem
> wxTreeListItems
;
74 // Some special "items" that can be used with InsertItem():
75 extern WXDLLIMPEXP_DATA_ADV(const wxTreeListItem
) wxTLI_FIRST
;
76 extern WXDLLIMPEXP_DATA_ADV(const wxTreeListItem
) wxTLI_LAST
;
78 // ----------------------------------------------------------------------------
79 // wxTreeListCtrl: a control combining wxTree- and wxListCtrl features.
80 // ----------------------------------------------------------------------------
82 // This control also provides easy to use high level interface. Although the
83 // implementation uses wxDataViewCtrl internally, this class is intentionally
84 // simpler than wxDataViewCtrl and doesn't provide all of its functionality.
86 // If you need extra features you can always use GetDataView() accessor to work
87 // with wxDataViewCtrl directly but doing this makes your unportable to possible
88 // future non-wxDataViewCtrl-based implementations of this class.
90 class WXDLLIMPEXP_ADV wxTreeListCtrl
: public wxWindow
,
94 // Constructors and such
95 // ---------------------
97 wxTreeListCtrl() { Init(); }
98 wxTreeListCtrl(wxWindow
* parent
,
100 const wxPoint
& pos
= wxDefaultPosition
,
101 const wxSize
& size
= wxDefaultSize
,
102 long style
= wxTL_DEFAULT_STYLE
,
103 const wxString
& name
= wxTreeListCtrlNameStr
)
107 Create(parent
, id
, pos
, size
, style
, name
);
110 bool Create(wxWindow
* parent
,
112 const wxPoint
& pos
= wxDefaultPosition
,
113 const wxSize
& size
= wxDefaultSize
,
114 long style
= wxTL_DEFAULT_STYLE
,
115 const wxString
& name
= wxTreeListCtrlNameStr
);
118 virtual ~wxTreeListCtrl();
123 // Add a column with the given title and attributes, returns the index of
124 // the new column or -1 on failure.
125 int AppendColumn(const wxString
& title
,
126 int width
= wxCOL_WIDTH_AUTOSIZE
,
127 wxAlignment align
= wxALIGN_LEFT
,
128 int flags
= wxCOL_RESIZABLE
)
130 return DoInsertColumn(title
, -1, width
, align
, flags
);
133 // Return the total number of columns.
134 unsigned GetColumnCount() const;
136 // Delete the column with the given index, returns false if index is
137 // invalid or deleting the column failed for some other reason.
138 bool DeleteColumn(unsigned col
);
140 // Delete all columns.
143 // Set column width to either the given value in pixels or to the value
144 // large enough to fit all of the items if width == wxCOL_WIDTH_AUTOSIZE.
145 void SetColumnWidth(unsigned col
, int width
);
147 // Get the current width of the given column in pixels.
148 int GetColumnWidth(unsigned col
) const;
150 // Get the width appropriate for showing the given text. This is typically
151 // used as second argument for AppendColumn() or with SetColumnWidth().
152 int WidthFor(const wxString
& text
) const;
158 // Adding items. The parent and text of the first column of the new item
159 // must always be specified, the rest is optional.
161 // Each item can have two images: one used for closed state and another for
162 // opened one. Only the first one is ever used for the items that don't
163 // have children. And both are not set by default.
165 // It is also possible to associate arbitrary client data pointer with the
166 // new item. It will be deleted by the control when the item is deleted
167 // (either by an explicit DeleteItem() call or because the entire control
170 wxTreeListItem
AppendItem(wxTreeListItem parent
,
171 const wxString
& text
,
172 int imageClosed
= NO_IMAGE
,
173 int imageOpened
= NO_IMAGE
,
174 wxClientData
* data
= NULL
)
176 return DoInsertItem(parent
, wxTLI_LAST
, text
,
177 imageClosed
, imageOpened
, data
);
180 wxTreeListItem
InsertItem(wxTreeListItem parent
,
181 wxTreeListItem previous
,
182 const wxString
& text
,
183 int imageClosed
= NO_IMAGE
,
184 int imageOpened
= NO_IMAGE
,
185 wxClientData
* data
= NULL
)
187 return DoInsertItem(parent
, previous
, text
,
188 imageClosed
, imageOpened
, data
);
191 wxTreeListItem
PrependItem(wxTreeListItem parent
,
192 const wxString
& text
,
193 int imageClosed
= NO_IMAGE
,
194 int imageOpened
= NO_IMAGE
,
195 wxClientData
* data
= NULL
)
197 return DoInsertItem(parent
, wxTLI_FIRST
, text
,
198 imageClosed
, imageOpened
, data
);
202 void DeleteItem(wxTreeListItem item
);
203 void DeleteAllItems();
209 // Return the (never shown) root item.
210 wxTreeListItem
GetRootItem() const;
212 // The parent item may be invalid for the root-level items.
213 wxTreeListItem
GetItemParent(wxTreeListItem item
) const;
215 // Iterate over the given item children: start by calling GetFirstChild()
216 // and then call GetNextSibling() for as long as it returns valid item.
217 wxTreeListItem
GetFirstChild(wxTreeListItem item
) const;
218 wxTreeListItem
GetNextSibling(wxTreeListItem item
) const;
220 // Return the first child of the root item, which is also the first item of
221 // the tree in depth-first traversal order.
222 wxTreeListItem
GetFirstItem() const { return GetFirstChild(GetRootItem()); }
224 // Get item after the given one in the depth-first tree-traversal order.
225 // Calling this function starting with the result of GetFirstItem() allows
226 // iterating over all items in the tree.
227 wxTreeListItem
GetNextItem(wxTreeListItem item
) const;
233 const wxString
& GetItemText(wxTreeListItem item
, unsigned col
= 0) const;
235 // The convenience overload below sets the text for the first column.
236 void SetItemText(wxTreeListItem item
, unsigned col
, const wxString
& text
);
237 void SetItemText(wxTreeListItem item
, const wxString
& text
)
239 SetItemText(item
, 0, text
);
242 // By default the opened image is the same as the normal, closed one (if
243 // it's used at all).
244 void SetItemImage(wxTreeListItem item
, int closed
, int opened
= NO_IMAGE
);
246 // Retrieve or set the data associated with the item.
247 wxClientData
* GetItemData(wxTreeListItem item
) const;
248 void SetItemData(wxTreeListItem item
, wxClientData
* data
);
251 // Expanding and collapsing
252 // ------------------------
254 void Expand(wxTreeListItem item
);
255 void Collapse(wxTreeListItem item
);
256 bool IsExpanded(wxTreeListItem item
) const;
259 // Selection handling
260 // ------------------
262 // This function can be used with single selection controls, use
263 // GetSelections() with the multi-selection ones.
264 wxTreeListItem
GetSelection() const;
266 // This one can be used with either single or multi-selection controls.
267 unsigned GetSelections(wxTreeListItems
& selections
) const;
269 // In single selection mode Select() deselects any other selected items, in
270 // multi-selection case it adds to the selection.
271 void Select(wxTreeListItem item
);
273 // Can be used in multiple selection mode only, single selected item in the
274 // single selection mode can't be unselected.
275 void Unselect(wxTreeListItem item
);
277 // Return true if the item is selected, can be used in both single and
278 // multiple selection modes.
279 bool IsSelected(wxTreeListItem item
) const;
281 // Select or unselect all items, only valid in multiple selection mode.
289 // Methods in this section can only be used with the controls created with
290 // wxTL_CHECKBOX style.
292 // Simple set, unset or query the checked state.
293 void CheckItem(wxTreeListItem item
, wxCheckBoxState state
= wxCHK_CHECKED
);
294 void UncheckItem(wxTreeListItem item
) { CheckItem(item
, wxCHK_UNCHECKED
); }
296 // The same but do it recursively for this item itself and its children.
297 void CheckItemRecursively(wxTreeListItem item
,
298 wxCheckBoxState state
= wxCHK_CHECKED
);
300 // Update the parent of this item recursively: if this item and all its
301 // siblings are checked, the parent will become checked as well. If this
302 // item and all its siblings are unchecked, the parent will be unchecked.
303 // And if the siblings of this item are not all in the same state, the
304 // parent will be switched to indeterminate state. And then the same logic
305 // will be applied to the parents parent and so on recursively.
307 // This is typically called when the state of the given item has changed
308 // from EVT_TREELIST_ITEM_CHECKED() handler in the controls which have
309 // wxTL_3STATE flag. Notice that without this flag this function can't work
310 // as it would be unable to set the state of a parent with both checked and
311 // unchecked items so it's only allowed to call it when this flag is set.
312 void UpdateItemParentStateRecursively(wxTreeListItem item
);
314 // Return the current state.
315 wxCheckBoxState
GetCheckedState(wxTreeListItem item
) const;
317 // Return true if all item children (if any) are in the given state.
318 bool AreAllChildrenInState(wxTreeListItem item
,
319 wxCheckBoxState state
) const;
323 // Common part of all ctors.
326 // Implementation of AppendColumn().
327 int DoInsertColumn(const wxString
& title
,
328 int pos
, // May be -1 meaning "append".
333 // Common part of {Append,Insert,Prepend}Item().
334 wxTreeListItem
DoInsertItem(wxTreeListItem parent
,
335 wxTreeListItem previous
,
336 const wxString
& text
,
341 // Send wxTreeListEvent corresponding to the given wxDataViewEvent.
343 // Also updates the original event "skipped" and "vetoed" flags.
344 void SendEvent(wxEventType evt
, wxDataViewEvent
& event
);
347 // Called by wxTreeListModel when an item is toggled by the user.
348 void OnItemToggled(wxTreeListItem item
, wxCheckBoxState stateOld
);
351 void OnSelectionChanged(wxDataViewEvent
& event
);
352 void OnItemExpanding(wxDataViewEvent
& event
);
353 void OnItemExpanded(wxDataViewEvent
& event
);
354 void OnItemActivated(wxDataViewEvent
& event
);
355 void OnItemContextMenu(wxDataViewEvent
& event
);
356 void OnSize(wxSizeEvent
& event
);
358 wxDECLARE_EVENT_TABLE();
361 wxDataViewCtrl
* m_view
;
362 wxTreeListModel
* m_model
;
365 // It calls our inherited protected wxWithImages::GetImage() method.
366 friend class wxTreeListModel
;
368 wxDECLARE_NO_COPY_CLASS(wxTreeListCtrl
);
371 // ----------------------------------------------------------------------------
372 // wxTreeListEvent: event generated by wxTreeListCtrl.
373 // ----------------------------------------------------------------------------
375 class wxTreeListEvent
: public wxNotifyEvent
378 // The item affected by the event.
379 wxTreeListItem
GetItem() const { return m_item
; }
381 // The previous state of the item checkbox for ITEM_CHECKED events only.
382 wxCheckBoxState
GetOldCheckedState() const { return m_oldCheckedState
; }
385 virtual wxEvent
* Clone() const { return new wxTreeListEvent(*this); }
388 // Ctor is private, only wxTreeListCtrl can create events of this type.
389 wxTreeListEvent(wxEventType evtType
,
390 wxTreeListCtrl
* treelist
,
392 : wxNotifyEvent(evtType
, treelist
->GetId()),
395 SetEventObject(treelist
);
398 // Set the checkbox state before this event for ITEM_CHECKED events.
399 void SetOldCheckedState(wxCheckBoxState state
)
401 m_oldCheckedState
= state
;
405 const wxTreeListItem m_item
;
407 wxCheckBoxState m_oldCheckedState
;
409 friend class wxTreeListCtrl
;
411 wxDECLARE_ABSTRACT_CLASS(wxTreeListEvent
);
414 // Event types and event table macros.
416 typedef void (wxEvtHandler::*wxTreeListEventFunction
)(wxTreeListEvent
&);
418 #define wxTreeListEventHandler(func) \
419 wxEVENT_HANDLER_CAST(wxTreeListEventFunction, func)
421 #define wxEVT_TREELIST_GENERIC(name, id, fn) \
422 wx__DECLARE_EVT1(wxEVT_COMMAND_TREELIST_##name, id, wxTreeListEventHandler(fn))
424 #define wxDECLARE_TREELIST_EVENT(name) \
425 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, \
426 wxEVT_COMMAND_TREELIST_##name, \
429 wxDECLARE_TREELIST_EVENT(SELECTION_CHANGED
);
430 #define EVT_TREELIST_SELECTION_CHANGED(id, fn) \
431 wxEVT_TREELIST_GENERIC(SELECTION_CHANGED, id, fn)
433 wxDECLARE_TREELIST_EVENT(ITEM_EXPANDING
);
434 #define EVT_TREELIST_ITEM_EXPANDING(id, fn) \
435 wxEVT_TREELIST_GENERIC(ITEM_EXPANDING, id, fn)
437 wxDECLARE_TREELIST_EVENT(ITEM_EXPANDED
);
438 #define EVT_TREELIST_ITEM_EXPANDED(id, fn) \
439 wxEVT_TREELIST_GENERIC(ITEM_EXPANDED, id, fn)
441 wxDECLARE_TREELIST_EVENT(ITEM_CHECKED
);
442 #define EVT_TREELIST_ITEM_CHECKED(id, fn) \
443 wxEVT_TREELIST_GENERIC(ITEM_CHECKED, id, fn)
445 wxDECLARE_TREELIST_EVENT(ITEM_ACTIVATED
);
446 #define EVT_TREELIST_ITEM_ACTIVATED(id, fn) \
447 wxEVT_TREELIST_GENERIC(ITEM_ACTIVATED, id, fn)
449 wxDECLARE_TREELIST_EVENT(ITEM_CONTEXT_MENU
);
450 #define EVT_TREELIST_ITEM_CONTEXT_MENU(id, fn) \
451 wxEVT_TREELIST_GENERIC(ITEM_CONTEXT_MENU, id, fn)
453 #undef wxDECLARE_TREELIST_EVENT
455 #endif // wxUSE_TREELISTCTRL
457 #endif // _WX_TREELIST_H_