]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/treectrl.h
A little black magic... When the C++ object (for a window or
[wxWidgets.git] / include / wx / msw / treectrl.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/msw/treectrl.h
3 // Purpose: wxTreeCtrl class
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10/10/98
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_TREECTRL_H_
13 #define _WX_TREECTRL_H_
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #ifdef __GNUG__
20 #pragma interface "treectrl.h"
21 #endif
22
23 #if wxUSE_TREECTRL
24
25 #include "wx/textctrl.h"
26 #include "wx/dynarray.h"
27 #include "wx/treebase.h"
28
29 #ifdef __GNUWIN32__
30 // Cygwin windows.h defines these identifiers
31 #undef GetFirstChild
32 #undef GetNextSibling
33 #endif // Cygwin
34
35 // the type for "untyped" data
36 typedef long wxDataType;
37
38 // fwd decl
39 class WXDLLEXPORT wxImageList;
40 class WXDLLEXPORT wxDragImage;
41 struct WXDLLEXPORT wxTreeViewItem;
42
43 // a callback function used for sorting tree items, it should return -1 if the
44 // first item precedes the second, +1 if the second precedes the first or 0 if
45 // they're equivalent
46 class wxTreeItemData;
47
48 // NB: all the following flags are for compatbility only and will be removed in the
49 // next versions
50
51 // flags for deprecated `Expand(int action)'
52 enum
53 {
54 wxTREE_EXPAND_EXPAND,
55 wxTREE_EXPAND_COLLAPSE,
56 wxTREE_EXPAND_COLLAPSE_RESET,
57 wxTREE_EXPAND_TOGGLE
58 };
59
60 // flags for deprecated InsertItem() variant
61 #define wxTREE_INSERT_FIRST 0xFFFF0001
62 #define wxTREE_INSERT_LAST 0xFFFF0002
63
64 // ----------------------------------------------------------------------------
65 // wxTreeCtrl
66 // ----------------------------------------------------------------------------
67 class WXDLLEXPORT wxTreeCtrl : public wxControl
68 {
69 public:
70 // creation
71 // --------
72 wxTreeCtrl() { Init(); }
73
74 wxTreeCtrl(wxWindow *parent, wxWindowID id = -1,
75 const wxPoint& pos = wxDefaultPosition,
76 const wxSize& size = wxDefaultSize,
77 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
78 const wxValidator& validator = wxDefaultValidator,
79 const wxString& name = "wxTreeCtrl")
80 {
81 Create(parent, id, pos, size, style, validator, name);
82 }
83
84 virtual ~wxTreeCtrl();
85
86 bool Create(wxWindow *parent, wxWindowID id = -1,
87 const wxPoint& pos = wxDefaultPosition,
88 const wxSize& size = wxDefaultSize,
89 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
90 const wxValidator& validator = wxDefaultValidator,
91 const wxString& name = "wxTreeCtrl");
92
93 // accessors
94 // ---------
95
96 // get the total number of items in the control
97 size_t GetCount() const;
98
99 // indent is the number of pixels the children are indented relative to
100 // the parents position. SetIndent() also redraws the control
101 // immediately.
102 unsigned int GetIndent() const;
103 void SetIndent(unsigned int indent);
104
105 // spacing is the number of pixels between the start and the Text
106 // not implemented under wxMSW
107 unsigned int GetSpacing() const { return 18; } // return wxGTK default
108 void SetSpacing(unsigned int WXUNUSED(spacing)) { }
109
110 // image list: these functions allow to associate an image list with
111 // the control and retrieve it. Note that the control does _not_ delete
112 // the associated image list when it's deleted in order to allow image
113 // lists to be shared between different controls.
114 //
115 // The normal image list is for the icons which correspond to the
116 // normal tree item state (whether it is selected or not).
117 // Additionally, the application might choose to show a state icon
118 // which corresponds to an app-defined item state (for example,
119 // checked/unchecked) which are taken from the state image list.
120 wxImageList *GetImageList() const;
121 wxImageList *GetStateImageList() const;
122
123 void SetImageList(wxImageList *imageList);
124 void SetStateImageList(wxImageList *imageList);
125 void AssignImageList(wxImageList *imageList);
126 void AssignStateImageList(wxImageList *imageList);
127
128 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
129 // member functions of wxTreeItem because they must know the tree the item
130 // belongs to for Windows implementation and storing the pointer to
131 // wxTreeCtrl in each wxTreeItem is just too much waste.
132
133 // accessors
134 // ---------
135
136 // retrieve items label
137 wxString GetItemText(const wxTreeItemId& item) const;
138 // get one of the images associated with the item (normal by default)
139 int GetItemImage(const wxTreeItemId& item,
140 wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
141 // get the data associated with the item
142 wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
143
144 // modifiers
145 // ---------
146
147 // set items label
148 void SetItemText(const wxTreeItemId& item, const wxString& text);
149 // get one of the images associated with the item (normal by default)
150 void SetItemImage(const wxTreeItemId& item, int image,
151 wxTreeItemIcon which = wxTreeItemIcon_Normal);
152 // associate some data with the item
153 void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
154
155 // force appearance of [+] button near the item. This is useful to
156 // allow the user to expand the items which don't have any children now
157 // - but instead add them only when needed, thus minimizing memory
158 // usage and loading time.
159 void SetItemHasChildren(const wxTreeItemId& item, bool has = TRUE);
160
161 // the item will be shown in bold
162 void SetItemBold(const wxTreeItemId& item, bool bold = TRUE);
163
164 // the item will be shown with a drop highlight
165 void SetItemDropHighlight(const wxTreeItemId& item, bool highlight = TRUE);
166
167 // set the items text colour
168 void SetItemTextColour(const wxTreeItemId& item, const wxColour& col);
169
170 // set the items background colour
171 void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col);
172
173 // set the items font (should be of the same height for all items)
174 void SetItemFont(const wxTreeItemId& item, const wxFont& font);
175
176 // item status inquiries
177 // ---------------------
178
179 // is the item visible (it might be outside the view or not expanded)?
180 bool IsVisible(const wxTreeItemId& item) const;
181 // does the item has any children?
182 bool ItemHasChildren(const wxTreeItemId& item) const;
183 // is the item expanded (only makes sense if HasChildren())?
184 bool IsExpanded(const wxTreeItemId& item) const;
185 // is this item currently selected (the same as has focus)?
186 bool IsSelected(const wxTreeItemId& item) const;
187 // is item text in bold font?
188 bool IsBold(const wxTreeItemId& item) const;
189
190 // number of children
191 // ------------------
192
193 // if 'recursively' is FALSE, only immediate children count, otherwise
194 // the returned number is the number of all items in this branch
195 size_t GetChildrenCount(const wxTreeItemId& item,
196 bool recursively = TRUE) const;
197
198 // navigation
199 // ----------
200
201 // wxTreeItemId.IsOk() will return FALSE if there is no such item
202
203 // get the root tree item
204 wxTreeItemId GetRootItem() const;
205
206 // get the item currently selected (may return NULL if no selection)
207 wxTreeItemId GetSelection() const;
208
209 // get the items currently selected, return the number of such item
210 //
211 // NB: this operation is expensive and can take a long time for a
212 // control with a lot of items (~ O(number of items)).
213 size_t GetSelections(wxArrayTreeItemIds& selections) const;
214
215 // get the parent of this item (may return NULL if root)
216 wxTreeItemId GetParent(const wxTreeItemId& item) const;
217
218 // for this enumeration function you must pass in a "cookie" parameter
219 // which is opaque for the application but is necessary for the library
220 // to make these functions reentrant (i.e. allow more than one
221 // enumeration on one and the same object simultaneously). Of course,
222 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
223 // the same!
224
225 // get the first child of this item
226 wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& _cookie) const;
227 // get the next child
228 wxTreeItemId GetNextChild(const wxTreeItemId& item, long& _cookie) const;
229 // get the last child of this item - this method doesn't use cookies
230 wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
231
232 // get the next sibling of this item
233 wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
234 // get the previous sibling
235 wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
236
237 // get first visible item
238 wxTreeItemId GetFirstVisibleItem() const;
239 // get the next visible item: item must be visible itself!
240 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
241 wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
242 // get the previous visible item: item must be visible itself!
243 wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
244
245 // operations
246 // ----------
247
248 // add the root node to the tree
249 wxTreeItemId AddRoot(const wxString& text,
250 int image = -1, int selectedImage = -1,
251 wxTreeItemData *data = NULL);
252
253 // insert a new item in as the first child of the parent
254 wxTreeItemId PrependItem(const wxTreeItemId& parent,
255 const wxString& text,
256 int image = -1, int selectedImage = -1,
257 wxTreeItemData *data = NULL);
258
259 // insert a new item after a given one
260 wxTreeItemId InsertItem(const wxTreeItemId& parent,
261 const wxTreeItemId& idPrevious,
262 const wxString& text,
263 int image = -1, int selectedImage = -1,
264 wxTreeItemData *data = NULL);
265
266 // insert a new item before the one with the given index
267 wxTreeItemId InsertItem(const wxTreeItemId& parent,
268 size_t index,
269 const wxString& text,
270 int image = -1, int selectedImage = -1,
271 wxTreeItemData *data = NULL);
272
273 // insert a new item in as the last child of the parent
274 wxTreeItemId AppendItem(const wxTreeItemId& parent,
275 const wxString& text,
276 int image = -1, int selectedImage = -1,
277 wxTreeItemData *data = NULL);
278
279 // delete this item and associated data if any
280 void Delete(const wxTreeItemId& item);
281 // delete all children (but don't delete the item itself)
282 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
283 void DeleteChildren(const wxTreeItemId& item);
284 // delete all items from the tree
285 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
286 void DeleteAllItems();
287
288 // expand this item
289 void Expand(const wxTreeItemId& item);
290 // collapse the item without removing its children
291 void Collapse(const wxTreeItemId& item);
292 // collapse the item and remove all children
293 void CollapseAndReset(const wxTreeItemId& item);
294 // toggles the current state
295 void Toggle(const wxTreeItemId& item);
296
297 // remove the selection from currently selected item (if any)
298 void Unselect();
299 // unselect all items (only makes sense for multiple selection control)
300 void UnselectAll();
301 // select this item
302 void SelectItem(const wxTreeItemId& item);
303 // make sure this item is visible (expanding the parent item and/or
304 // scrolling to this item if necessary)
305 void EnsureVisible(const wxTreeItemId& item);
306 // scroll to this item (but don't expand its parent)
307 void ScrollTo(const wxTreeItemId& item);
308
309 // start editing the item label: this (temporarily) replaces the item
310 // with a one line edit control. The item will be selected if it hadn't
311 // been before. textCtrlClass parameter allows you to create an edit
312 // control of arbitrary user-defined class deriving from wxTextCtrl.
313 wxTextCtrl* EditLabel(const wxTreeItemId& item,
314 wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl));
315 // returns the same pointer as StartEdit() if the item is being edited,
316 // NULL otherwise (it's assumed that no more than one item may be
317 // edited simultaneously)
318 wxTextCtrl* GetEditControl() const;
319 // end editing and accept or discard the changes to item label
320 void EndEditLabel(const wxTreeItemId& item, bool discardChanges = FALSE);
321
322 // sorting
323 // this function is called to compare 2 items and should return -1, 0
324 // or +1 if the first item is less than, equal to or greater than the
325 // second one. The base class version performs alphabetic comparaison
326 // of item labels (GetText)
327 virtual int OnCompareItems(const wxTreeItemId& item1,
328 const wxTreeItemId& item2);
329 // sort the children of this item using OnCompareItems
330 //
331 // NB: this function is not reentrant and not MT-safe (FIXME)!
332 void SortChildren(const wxTreeItemId& item);
333
334 // helpers
335 // -------
336
337 // determine to which item (if any) belongs the given point (the
338 // coordinates specified are relative to the client area of tree ctrl)
339 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
340 // constants.
341 //
342 // The first function is more portable (because easier to implement
343 // on other platforms), but the second one returns some extra info.
344 wxTreeItemId HitTest(const wxPoint& point)
345 { int dummy; return HitTest(point, dummy); }
346 wxTreeItemId HitTest(const wxPoint& point, int& flags);
347
348 // get the bounding rectangle of the item (or of its label only)
349 bool GetBoundingRect(const wxTreeItemId& item,
350 wxRect& rect,
351 bool textOnly = FALSE) const;
352
353 // deprecated
354 // ----------
355
356 // these methods are deprecated and will be removed in future versions of
357 // wxWindows, they're here for compatibility only, don't use them in new
358 // code (the comments indicate why these methods are now useless and how to
359 // replace them)
360
361 // use Expand, Collapse, CollapseAndReset or Toggle
362 void ExpandItem(const wxTreeItemId& item, int action);
363
364 // use AddRoot, PrependItem or AppendItem
365 wxTreeItemId InsertItem(const wxTreeItemId& parent,
366 const wxString& text,
367 int image = -1, int selImage = -1,
368 long insertAfter = wxTREE_INSERT_LAST);
369
370 // use Set/GetImageList and Set/GetStateImageList
371 wxImageList *GetImageList(int) const
372 { return GetImageList(); }
373 void SetImageList(wxImageList *imageList, int)
374 { SetImageList(imageList); }
375
376 // use Set/GetItemImage directly
377 // get the selected item image
378 int GetItemSelectedImage(const wxTreeItemId& item) const
379 { return GetItemImage(item, wxTreeItemIcon_Selected); }
380 // set the selected item image
381 void SetItemSelectedImage(const wxTreeItemId& item, int image)
382 { SetItemImage(item, image, wxTreeItemIcon_Selected); }
383
384 // implementation
385 // --------------
386
387 virtual long MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
388 virtual bool MSWCommand(WXUINT param, WXWORD id);
389 virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
390
391 // override some base class virtuals
392 virtual bool SetBackgroundColour(const wxColour &colour);
393 virtual bool SetForegroundColour(const wxColour &colour);
394
395 // get/set the check state for the item (only for wxTR_MULTIPLE)
396 bool IsItemChecked(const wxTreeItemId& item) const;
397 void SetItemCheck(const wxTreeItemId& item, bool check = TRUE);
398
399 protected:
400 // SetImageList helper
401 void SetAnyImageList(wxImageList *imageList, int which);
402
403 // refresh a single item
404 void RefreshItem(const wxTreeItemId& item);
405
406 wxTextCtrl *m_textCtrl; // used while editing the item label
407 wxImageList *m_imageListNormal, // images for tree elements
408 *m_imageListState; // special images for app defined states
409 bool m_ownsImageListNormal, m_ownsImageListState;
410
411 private:
412 // the common part of all ctors
413 void Init();
414
415 // helper functions
416 inline bool DoGetItem(wxTreeViewItem *tvItem) const;
417 inline void DoSetItem(wxTreeViewItem *tvItem);
418
419 inline void DoExpand(const wxTreeItemId& item, int flag);
420
421 wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
422 wxTreeItemId hInsertAfter,
423 const wxString& text,
424 int image, int selectedImage,
425 wxTreeItemData *data);
426
427 int DoGetItemImageFromData(const wxTreeItemId& item,
428 wxTreeItemIcon which) const;
429 void DoSetItemImageFromData(const wxTreeItemId& item,
430 int image,
431 wxTreeItemIcon which) const;
432 void DoSetItemImages(const wxTreeItemId& item, int image, int imageSel);
433
434 void DeleteTextCtrl();
435
436 // support for additional item images which we implement using
437 // wxTreeItemIndirectData technique - see the comments in msw/treectrl.cpp
438 void SetIndirectItemData(const wxTreeItemId& item,
439 class wxTreeItemIndirectData *data);
440 bool HasIndirectData(const wxTreeItemId& item) const;
441 bool IsDataIndirect(wxTreeItemData *data) const
442 { return data && data->GetId().m_pItem == 0; }
443
444 // the hash storing the items attributes (indexed by items ids)
445 wxHashTable m_attrs;
446
447 // TRUE if the hash above is not empty
448 bool m_hasAnyAttr;
449
450 // used for dragging
451 wxDragImage *m_dragImage;
452
453 // Virtual root item, if wxTR_HIDE_ROOT is set.
454 void* m_pVirtualRoot;
455
456 // the starting item for selection with Shift
457 WXHTREEITEM m_htSelStart;
458
459 friend class wxTreeItemIndirectData;
460 friend class wxTreeSortHelper;
461
462 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
463 };
464
465 #endif // wxUSE_TREECTRL
466
467 #endif
468 // _WX_TREECTRL_H_