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