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