]> git.saurik.com Git - wxWidgets.git/blob - include/wx/treelist.h
4e82b3f229ef3b9e393ed4108e8b496c02b31e8d
[wxWidgets.git] / include / wx / treelist.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/treelist.h
3 // Purpose: wxTreeListCtrl class declaration.
4 // Author: Vadim Zeitlin
5 // Created: 2011-08-17
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 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_TREELIST_H_
12 #define _WX_TREELIST_H_
13
14 #include "wx/defs.h"
15
16 #if wxUSE_TREELISTCTRL
17
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"
23
24 class WXDLLIMPEXP_FWD_ADV wxDataViewCtrl;
25 class WXDLLIMPEXP_FWD_ADV wxDataViewEvent;
26
27 extern WXDLLIMPEXP_DATA_CORE(const char) wxTreeListCtrlNameStr[];
28
29 class wxTreeListModel;
30 class wxTreeListModelNode;
31
32 // ----------------------------------------------------------------------------
33 // Constants.
34 // ----------------------------------------------------------------------------
35
36 // wxTreeListCtrl styles.
37 //
38 // Notice that using wxTL_USER_3STATE implies wxTL_3STATE and wxTL_3STATE in
39 // turn implies wxTL_CHECKBOX.
40 enum
41 {
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.
47
48 wxTL_DEFAULT_STYLE = wxTL_SINGLE,
49 wxTL_STYLE_MASK = wxTL_SINGLE |
50 wxTL_CHECKBOX
51 };
52
53 // ----------------------------------------------------------------------------
54 // wxTreeListItem: unique identifier of an item in wxTreeListCtrl.
55 // ----------------------------------------------------------------------------
56
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*>
60 {
61 public:
62 wxTreeListItem(wxTreeListModelNode* item = NULL)
63 : wxItemId<wxTreeListModelNode*>(item)
64 {
65 }
66 };
67
68 // Container of multiple items.
69 typedef wxVector<wxTreeListItem> wxTreeListItems;
70
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;
74
75 // ----------------------------------------------------------------------------
76 // wxTreeListCtrl: a control combining wxTree- and wxListCtrl features.
77 // ----------------------------------------------------------------------------
78
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.
82 //
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.
86
87 class WXDLLIMPEXP_ADV wxTreeListCtrl : public wxWindow,
88 public wxWithImages
89 {
90 public:
91 // Constructors and such
92 // ---------------------
93
94 wxTreeListCtrl() { Init(); }
95 wxTreeListCtrl(wxWindow* parent,
96 wxWindowID id,
97 const wxPoint& pos = wxDefaultPosition,
98 const wxSize& size = wxDefaultSize,
99 long style = wxTL_DEFAULT_STYLE,
100 const wxString& name = wxTreeListCtrlNameStr)
101 {
102 Init();
103
104 Create(parent, id, pos, size, style, name);
105 }
106
107 bool Create(wxWindow* parent,
108 wxWindowID id,
109 const wxPoint& pos = wxDefaultPosition,
110 const wxSize& size = wxDefaultSize,
111 long style = wxTL_DEFAULT_STYLE,
112 const wxString& name = wxTreeListCtrlNameStr);
113
114
115 virtual ~wxTreeListCtrl();
116
117 // Columns methods
118 // ---------------
119
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)
126 {
127 return DoInsertColumn(title, -1, width, align, flags);
128 }
129
130 // Return the total number of columns.
131 unsigned GetColumnCount() const;
132
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);
136
137 // Delete all columns.
138 void ClearColumns();
139
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);
143
144 // Get the current width of the given column in pixels.
145 int GetColumnWidth(unsigned col) const;
146
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;
150
151
152 // Item methods
153 // ------------
154
155 // Adding items. The parent and text of the first column of the new item
156 // must always be specified, the rest is optional.
157 //
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.
161 //
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
165 // is destroyed).
166
167 wxTreeListItem AppendItem(wxTreeListItem parent,
168 const wxString& text,
169 int imageClosed = NO_IMAGE,
170 int imageOpened = NO_IMAGE,
171 wxClientData* data = NULL)
172 {
173 return DoInsertItem(parent, wxTLI_LAST, text,
174 imageClosed, imageOpened, data);
175 }
176
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)
183 {
184 return DoInsertItem(parent, previous, text,
185 imageClosed, imageOpened, data);
186 }
187
188 wxTreeListItem PrependItem(wxTreeListItem parent,
189 const wxString& text,
190 int imageClosed = NO_IMAGE,
191 int imageOpened = NO_IMAGE,
192 wxClientData* data = NULL)
193 {
194 return DoInsertItem(parent, wxTLI_FIRST, text,
195 imageClosed, imageOpened, data);
196 }
197
198 // Deleting items.
199 void DeleteItem(wxTreeListItem item);
200 void DeleteAllItems();
201
202
203 // Tree navigation
204 // ---------------
205
206 // Return the (never shown) root item.
207 wxTreeListItem GetRootItem() const;
208
209 // The parent item may be invalid for the root-level items.
210 wxTreeListItem GetItemParent(wxTreeListItem item) const;
211
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;
216
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()); }
220
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;
225
226
227 // Items attributes
228 // ----------------
229
230 const wxString& GetItemText(wxTreeListItem item, unsigned col = 0) const;
231
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)
235 {
236 SetItemText(item, 0, text);
237 }
238
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);
242
243 // Retrieve or set the data associated with the item.
244 wxClientData* GetItemData(wxTreeListItem item) const;
245 void SetItemData(wxTreeListItem item, wxClientData* data);
246
247
248 // Expanding and collapsing
249 // ------------------------
250
251 void Expand(wxTreeListItem item);
252 void Collapse(wxTreeListItem item);
253 bool IsExpanded(wxTreeListItem item) const;
254
255
256 // Selection handling
257 // ------------------
258
259 // This function can be used with single selection controls, use
260 // GetSelections() with the multi-selection ones.
261 wxTreeListItem GetSelection() const;
262
263 // This one can be used with either single or multi-selection controls.
264 unsigned GetSelections(wxTreeListItems& selections) const;
265
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);
269
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);
273
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;
277
278 // Select or unselect all items, only valid in multiple selection mode.
279 void SelectAll();
280 void UnselectAll();
281
282
283 // Checkbox handling
284 // -----------------
285
286 // Methods in this section can only be used with the controls created with
287 // wxTL_CHECKBOX style.
288
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); }
292
293 // The same but do it recursively for this item itself and its children.
294 void CheckItemRecursively(wxTreeListItem item,
295 wxCheckBoxState state = wxCHK_CHECKED);
296
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.
303 //
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);
310
311 // Return the current state.
312 wxCheckBoxState GetCheckedState(wxTreeListItem item) const;
313
314 // Return true if all item children (if any) are in the given state.
315 bool AreAllChildrenInState(wxTreeListItem item,
316 wxCheckBoxState state) const;
317
318
319 private:
320 // Common part of all ctors.
321 void Init();
322
323 // Implementation of AppendColumn().
324 int DoInsertColumn(const wxString& title,
325 int pos, // May be -1 meaning "append".
326 int width,
327 wxAlignment align,
328 int flags);
329
330 // Common part of {Append,Insert,Prepend}Item().
331 wxTreeListItem DoInsertItem(wxTreeListItem parent,
332 wxTreeListItem previous,
333 const wxString& text,
334 int imageClosed,
335 int imageOpened,
336 wxClientData* data);
337
338 // Send wxTreeListEvent corresponding to the given wxDataViewEvent.
339 //
340 // Also updates the original event "skipped" and "vetoed" flags.
341 void SendEvent(wxEventType evt, wxDataViewEvent& event);
342
343
344 // Called by wxTreeListModel when an item is toggled by the user.
345 void OnItemToggled(wxTreeListItem item, wxCheckBoxState stateOld);
346
347 // Event handlers.
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);
354
355 wxDECLARE_EVENT_TABLE();
356
357
358 wxDataViewCtrl* m_view;
359 wxTreeListModel* m_model;
360
361
362 // It calls our inherited protected wxWithImages::GetImage() method.
363 friend class wxTreeListModel;
364
365 wxDECLARE_NO_COPY_CLASS(wxTreeListCtrl);
366 };
367
368 // ----------------------------------------------------------------------------
369 // wxTreeListEvent: event generated by wxTreeListCtrl.
370 // ----------------------------------------------------------------------------
371
372 class wxTreeListEvent : public wxNotifyEvent
373 {
374 public:
375 // The item affected by the event.
376 wxTreeListItem GetItem() const { return m_item; }
377
378 // The previous state of the item checkbox for ITEM_CHECKED events only.
379 wxCheckBoxState GetOldCheckedState() const { return m_oldCheckedState; }
380
381
382 virtual wxEvent* Clone() const { return new wxTreeListEvent(*this); }
383
384 private:
385 // Ctor is private, only wxTreeListCtrl can create events of this type.
386 wxTreeListEvent(wxEventType evtType,
387 wxTreeListCtrl* treelist,
388 wxTreeListItem item)
389 : wxNotifyEvent(evtType, treelist->GetId()),
390 m_item(item)
391 {
392 SetEventObject(treelist);
393 }
394
395 // Set the checkbox state before this event for ITEM_CHECKED events.
396 void SetOldCheckedState(wxCheckBoxState state)
397 {
398 m_oldCheckedState = state;
399 }
400
401
402 const wxTreeListItem m_item;
403
404 wxCheckBoxState m_oldCheckedState;
405
406 friend class wxTreeListCtrl;
407
408 wxDECLARE_ABSTRACT_CLASS(wxTreeListEvent);
409 };
410
411 // Event types and event table macros.
412
413 typedef void (wxEvtHandler::*wxTreeListEventFunction)(wxTreeListEvent&);
414
415 #define wxTreeListEventHandler(func) \
416 wxEVENT_HANDLER_CAST(wxTreeListEventFunction, func)
417
418 #define wxEVT_TREELIST_GENERIC(name, id, fn) \
419 wx__DECLARE_EVT1(wxEVT_COMMAND_TREELIST_##name, id, wxTreeListEventHandler(fn))
420
421 #define wxDECLARE_TREELIST_EVENT(name) \
422 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, \
423 wxEVT_COMMAND_TREELIST_##name, \
424 wxTreeListEvent)
425
426 wxDECLARE_TREELIST_EVENT(SELECTION_CHANGED);
427 #define EVT_TREELIST_SELECTION_CHANGED(id, fn) \
428 wxEVT_TREELIST_GENERIC(SELECTION_CHANGED, id, fn)
429
430 wxDECLARE_TREELIST_EVENT(ITEM_EXPANDING);
431 #define EVT_TREELIST_ITEM_EXPANDING(id, fn) \
432 wxEVT_TREELIST_GENERIC(ITEM_EXPANDING, id, fn)
433
434 wxDECLARE_TREELIST_EVENT(ITEM_EXPANDED);
435 #define EVT_TREELIST_ITEM_EXPANDED(id, fn) \
436 wxEVT_TREELIST_GENERIC(ITEM_EXPANDED, id, fn)
437
438 wxDECLARE_TREELIST_EVENT(ITEM_CHECKED);
439 #define EVT_TREELIST_ITEM_CHECKED(id, fn) \
440 wxEVT_TREELIST_GENERIC(ITEM_CHECKED, id, fn)
441
442 wxDECLARE_TREELIST_EVENT(ITEM_ACTIVATED);
443 #define EVT_TREELIST_ITEM_ACTIVATED(id, fn) \
444 wxEVT_TREELIST_GENERIC(ITEM_ACTIVATED, id, fn)
445
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)
449
450 #undef wxDECLARE_TREELIST_EVENT
451
452 #endif // wxUSE_TREELISTCTRL
453
454 #endif // _WX_TREELIST_H_