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
|
53 // ----------------------------------------------------------------------------
54 // wxTreeListItem: unique identifier of an item in wxTreeListCtrl.
55 // ----------------------------------------------------------------------------
57 // Make wxTreeListItem a forward-declarable class even though it's simple
58 // enough to possibly be declared as a simple typedef.
59 class wxTreeListItem
: public wxItemId
<wxTreeListModelNode
*>
62 wxTreeListItem(wxTreeListModelNode
* item
= NULL
)
63 : wxItemId
<wxTreeListModelNode
*>(item
)
68 // Container of multiple items.
69 typedef wxVector
<wxTreeListItem
> wxTreeListItems
;
71 // Some special "items" that can be used with InsertItem():
72 extern WXDLLIMPEXP_DATA_ADV(const wxTreeListItem
) wxTLI_FIRST
;
73 extern WXDLLIMPEXP_DATA_ADV(const wxTreeListItem
) wxTLI_LAST
;
75 // ----------------------------------------------------------------------------
76 // wxTreeListCtrl: a control combining wxTree- and wxListCtrl features.
77 // ----------------------------------------------------------------------------
79 // This control also provides easy to use high level interface. Although the
80 // implementation uses wxDataViewCtrl internally, this class is intentionally
81 // simpler than wxDataViewCtrl and doesn't provide all of its functionality.
83 // If you need extra features you can always use GetDataView() accessor to work
84 // with wxDataViewCtrl directly but doing this makes your unportable to possible
85 // future non-wxDataViewCtrl-based implementations of this class.
87 class WXDLLIMPEXP_ADV wxTreeListCtrl
: public wxWindow
,
91 // Constructors and such
92 // ---------------------
94 wxTreeListCtrl() { Init(); }
95 wxTreeListCtrl(wxWindow
* parent
,
97 const wxPoint
& pos
= wxDefaultPosition
,
98 const wxSize
& size
= wxDefaultSize
,
99 long style
= wxTL_DEFAULT_STYLE
,
100 const wxString
& name
= wxTreeListCtrlNameStr
)
104 Create(parent
, id
, pos
, size
, style
, name
);
107 bool Create(wxWindow
* parent
,
109 const wxPoint
& pos
= wxDefaultPosition
,
110 const wxSize
& size
= wxDefaultSize
,
111 long style
= wxTL_DEFAULT_STYLE
,
112 const wxString
& name
= wxTreeListCtrlNameStr
);
115 virtual ~wxTreeListCtrl();
120 // Add a column with the given title and attributes, returns the index of
121 // the new column or -1 on failure.
122 int AppendColumn(const wxString
& title
,
123 int width
= wxCOL_WIDTH_AUTOSIZE
,
124 wxAlignment align
= wxALIGN_LEFT
,
125 int flags
= wxCOL_RESIZABLE
)
127 return DoInsertColumn(title
, -1, width
, align
, flags
);
130 // Return the total number of columns.
131 unsigned GetColumnCount() const;
133 // Delete the column with the given index, returns false if index is
134 // invalid or deleting the column failed for some other reason.
135 bool DeleteColumn(unsigned col
);
137 // Delete all columns.
140 // Set column width to either the given value in pixels or to the value
141 // large enough to fit all of the items if width == wxCOL_WIDTH_AUTOSIZE.
142 void SetColumnWidth(unsigned col
, int width
);
144 // Get the current width of the given column in pixels.
145 int GetColumnWidth(unsigned col
) const;
147 // Get the width appropriate for showing the given text. This is typically
148 // used as second argument for AppendColumn() or with SetColumnWidth().
149 int WidthFor(const wxString
& text
) const;
155 // Adding items. The parent and text of the first column of the new item
156 // must always be specified, the rest is optional.
158 // Each item can have two images: one used for closed state and another for
159 // opened one. Only the first one is ever used for the items that don't
160 // have children. And both are not set by default.
162 // It is also possible to associate arbitrary client data pointer with the
163 // new item. It will be deleted by the control when the item is deleted
164 // (either by an explicit DeleteItem() call or because the entire control
167 wxTreeListItem
AppendItem(wxTreeListItem parent
,
168 const wxString
& text
,
169 int imageClosed
= NO_IMAGE
,
170 int imageOpened
= NO_IMAGE
,
171 wxClientData
* data
= NULL
)
173 return DoInsertItem(parent
, wxTLI_LAST
, text
,
174 imageClosed
, imageOpened
, data
);
177 wxTreeListItem
InsertItem(wxTreeListItem parent
,
178 wxTreeListItem previous
,
179 const wxString
& text
,
180 int imageClosed
= NO_IMAGE
,
181 int imageOpened
= NO_IMAGE
,
182 wxClientData
* data
= NULL
)
184 return DoInsertItem(parent
, previous
, text
,
185 imageClosed
, imageOpened
, data
);
188 wxTreeListItem
PrependItem(wxTreeListItem parent
,
189 const wxString
& text
,
190 int imageClosed
= NO_IMAGE
,
191 int imageOpened
= NO_IMAGE
,
192 wxClientData
* data
= NULL
)
194 return DoInsertItem(parent
, wxTLI_FIRST
, text
,
195 imageClosed
, imageOpened
, data
);
199 void DeleteItem(wxTreeListItem item
);
200 void DeleteAllItems();
206 // Return the (never shown) root item.
207 wxTreeListItem
GetRootItem() const;
209 // The parent item may be invalid for the root-level items.
210 wxTreeListItem
GetItemParent(wxTreeListItem item
) const;
212 // Iterate over the given item children: start by calling GetFirstChild()
213 // and then call GetNextSibling() for as long as it returns valid item.
214 wxTreeListItem
GetFirstChild(wxTreeListItem item
) const;
215 wxTreeListItem
GetNextSibling(wxTreeListItem item
) const;
217 // Return the first child of the root item, which is also the first item of
218 // the tree in depth-first traversal order.
219 wxTreeListItem
GetFirstItem() const { return GetFirstChild(GetRootItem()); }
221 // Get item after the given one in the depth-first tree-traversal order.
222 // Calling this function starting with the result of GetFirstItem() allows
223 // iterating over all items in the tree.
224 wxTreeListItem
GetNextItem(wxTreeListItem item
) const;
230 const wxString
& GetItemText(wxTreeListItem item
, unsigned col
= 0) const;
232 // The convenience overload below sets the text for the first column.
233 void SetItemText(wxTreeListItem item
, unsigned col
, const wxString
& text
);
234 void SetItemText(wxTreeListItem item
, const wxString
& text
)
236 SetItemText(item
, 0, text
);
239 // By default the opened image is the same as the normal, closed one (if
240 // it's used at all).
241 void SetItemImage(wxTreeListItem item
, int closed
, int opened
= NO_IMAGE
);
243 // Retrieve or set the data associated with the item.
244 wxClientData
* GetItemData(wxTreeListItem item
) const;
245 void SetItemData(wxTreeListItem item
, wxClientData
* data
);
248 // Expanding and collapsing
249 // ------------------------
251 void Expand(wxTreeListItem item
);
252 void Collapse(wxTreeListItem item
);
253 bool IsExpanded(wxTreeListItem item
) const;
256 // Selection handling
257 // ------------------
259 // This function can be used with single selection controls, use
260 // GetSelections() with the multi-selection ones.
261 wxTreeListItem
GetSelection() const;
263 // This one can be used with either single or multi-selection controls.
264 unsigned GetSelections(wxTreeListItems
& selections
) const;
266 // In single selection mode Select() deselects any other selected items, in
267 // multi-selection case it adds to the selection.
268 void Select(wxTreeListItem item
);
270 // Can be used in multiple selection mode only, single selected item in the
271 // single selection mode can't be unselected.
272 void Unselect(wxTreeListItem item
);
274 // Return true if the item is selected, can be used in both single and
275 // multiple selection modes.
276 bool IsSelected(wxTreeListItem item
) const;
278 // Select or unselect all items, only valid in multiple selection mode.
286 // Methods in this section can only be used with the controls created with
287 // wxTL_CHECKBOX style.
289 // Simple set, unset or query the checked state.
290 void CheckItem(wxTreeListItem item
, wxCheckBoxState state
= wxCHK_CHECKED
);
291 void UncheckItem(wxTreeListItem item
) { CheckItem(item
, wxCHK_UNCHECKED
); }
293 // The same but do it recursively for this item itself and its children.
294 void CheckItemRecursively(wxTreeListItem item
,
295 wxCheckBoxState state
= wxCHK_CHECKED
);
297 // Update the parent of this item recursively: if this item and all its
298 // siblings are checked, the parent will become checked as well. If this
299 // item and all its siblings are unchecked, the parent will be unchecked.
300 // And if the siblings of this item are not all in the same state, the
301 // parent will be switched to indeterminate state. And then the same logic
302 // will be applied to the parents parent and so on recursively.
304 // This is typically called when the state of the given item has changed
305 // from EVT_TREELIST_ITEM_CHECKED() handler in the controls which have
306 // wxTL_3STATE flag. Notice that without this flag this function can't work
307 // as it would be unable to set the state of a parent with both checked and
308 // unchecked items so it's only allowed to call it when this flag is set.
309 void UpdateItemParentStateRecursively(wxTreeListItem item
);
311 // Return the current state.
312 wxCheckBoxState
GetCheckedState(wxTreeListItem item
) const;
314 // Return true if all item children (if any) are in the given state.
315 bool AreAllChildrenInState(wxTreeListItem item
,
316 wxCheckBoxState state
) const;
320 // Common part of all ctors.
323 // Implementation of AppendColumn().
324 int DoInsertColumn(const wxString
& title
,
325 int pos
, // May be -1 meaning "append".
330 // Common part of {Append,Insert,Prepend}Item().
331 wxTreeListItem
DoInsertItem(wxTreeListItem parent
,
332 wxTreeListItem previous
,
333 const wxString
& text
,
338 // Send wxTreeListEvent corresponding to the given wxDataViewEvent.
340 // Also updates the original event "skipped" and "vetoed" flags.
341 void SendEvent(wxEventType evt
, wxDataViewEvent
& event
);
344 // Called by wxTreeListModel when an item is toggled by the user.
345 void OnItemToggled(wxTreeListItem item
, wxCheckBoxState stateOld
);
348 void OnSelectionChanged(wxDataViewEvent
& event
);
349 void OnItemExpanding(wxDataViewEvent
& event
);
350 void OnItemExpanded(wxDataViewEvent
& event
);
351 void OnItemActivated(wxDataViewEvent
& event
);
352 void OnItemContextMenu(wxDataViewEvent
& event
);
353 void OnSize(wxSizeEvent
& event
);
355 wxDECLARE_EVENT_TABLE();
358 wxDataViewCtrl
* m_view
;
359 wxTreeListModel
* m_model
;
362 // It calls our inherited protected wxWithImages::GetImage() method.
363 friend class wxTreeListModel
;
365 wxDECLARE_NO_COPY_CLASS(wxTreeListCtrl
);
368 // ----------------------------------------------------------------------------
369 // wxTreeListEvent: event generated by wxTreeListCtrl.
370 // ----------------------------------------------------------------------------
372 class wxTreeListEvent
: public wxNotifyEvent
375 // The item affected by the event.
376 wxTreeListItem
GetItem() const { return m_item
; }
378 // The previous state of the item checkbox for ITEM_CHECKED events only.
379 wxCheckBoxState
GetOldCheckedState() const { return m_oldCheckedState
; }
382 virtual wxEvent
* Clone() const { return new wxTreeListEvent(*this); }
385 // Ctor is private, only wxTreeListCtrl can create events of this type.
386 wxTreeListEvent(wxEventType evtType
,
387 wxTreeListCtrl
* treelist
,
389 : wxNotifyEvent(evtType
, treelist
->GetId()),
392 SetEventObject(treelist
);
395 // Set the checkbox state before this event for ITEM_CHECKED events.
396 void SetOldCheckedState(wxCheckBoxState state
)
398 m_oldCheckedState
= state
;
402 const wxTreeListItem m_item
;
404 wxCheckBoxState m_oldCheckedState
;
406 friend class wxTreeListCtrl
;
408 wxDECLARE_ABSTRACT_CLASS(wxTreeListEvent
);
411 // Event types and event table macros.
413 typedef void (wxEvtHandler::*wxTreeListEventFunction
)(wxTreeListEvent
&);
415 #define wxTreeListEventHandler(func) \
416 wxEVENT_HANDLER_CAST(wxTreeListEventFunction, func)
418 #define wxEVT_TREELIST_GENERIC(name, id, fn) \
419 wx__DECLARE_EVT1(wxEVT_COMMAND_TREELIST_##name, id, wxTreeListEventHandler(fn))
421 #define wxDECLARE_TREELIST_EVENT(name) \
422 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, \
423 wxEVT_COMMAND_TREELIST_##name, \
426 wxDECLARE_TREELIST_EVENT(SELECTION_CHANGED
);
427 #define EVT_TREELIST_SELECTION_CHANGED(id, fn) \
428 wxEVT_TREELIST_GENERIC(SELECTION_CHANGED, id, fn)
430 wxDECLARE_TREELIST_EVENT(ITEM_EXPANDING
);
431 #define EVT_TREELIST_ITEM_EXPANDING(id, fn) \
432 wxEVT_TREELIST_GENERIC(ITEM_EXPANDING, id, fn)
434 wxDECLARE_TREELIST_EVENT(ITEM_EXPANDED
);
435 #define EVT_TREELIST_ITEM_EXPANDED(id, fn) \
436 wxEVT_TREELIST_GENERIC(ITEM_EXPANDED, id, fn)
438 wxDECLARE_TREELIST_EVENT(ITEM_CHECKED
);
439 #define EVT_TREELIST_ITEM_CHECKED(id, fn) \
440 wxEVT_TREELIST_GENERIC(ITEM_CHECKED, id, fn)
442 wxDECLARE_TREELIST_EVENT(ITEM_ACTIVATED
);
443 #define EVT_TREELIST_ITEM_ACTIVATED(id, fn) \
444 wxEVT_TREELIST_GENERIC(ITEM_ACTIVATED, id, fn)
446 wxDECLARE_TREELIST_EVENT(ITEM_CONTEXT_MENU
);
447 #define EVT_TREELIST_ITEM_CONTEXT_MENU(id, fn) \
448 wxEVT_TREELIST_GENERIC(ITEM_CONTEXT_MENU, id, fn)
450 #undef wxDECLARE_TREELIST_EVENT
452 #endif // wxUSE_TREELISTCTRL
454 #endif // _WX_TREELIST_H_