1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TREECTRL_H_
13 #define _WX_TREECTRL_H_
15 #include "wx/textctrl.h"
16 #include "wx/dynarray.h"
18 // the type for "untyped" data
19 typedef long wxDataType
;
22 class WXDLLEXPORT wxImageList
;
23 struct WXDLLEXPORT wxTreeViewItem
;
25 // a callback function used for sorting tree items, it should return -1 if the
26 // first item precedes the second, +1 if the second precedes the first or 0 if
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
35 // where exactly the specified point is situated:
36 // above the client area.
37 static const int wxTREE_HITTEST_ABOVE
= 0x0001;
38 // below the client area.
39 static const int wxTREE_HITTEST_BELOW
= 0x0002;
40 // in the client area but below the last item.
41 static const int wxTREE_HITTEST_NOWHERE
= 0x0004;
42 // on the button associated with an item.
43 static const int wxTREE_HITTEST_ONITEMBUTTON
= 0x0010;
44 // on the bitmap associated with an item.
45 static const int wxTREE_HITTEST_ONITEMICON
= 0x0020;
46 // in the indentation associated with an item.
47 static const int wxTREE_HITTEST_ONITEMINDENT
= 0x0040;
48 // on the label (string) associated with an item.
49 static const int wxTREE_HITTEST_ONITEMLABEL
= 0x0080;
50 // in the area to the right of an item.
51 static const int wxTREE_HITTEST_ONITEMRIGHT
= 0x0100;
52 // on the state icon for a tree view item that is in a user-defined state.
53 static const int wxTREE_HITTEST_ONITEMSTATEICON
= 0x0200;
54 // to the right of the client area.
55 static const int wxTREE_HITTEST_TOLEFT
= 0x0400;
56 // to the left of the client area.
57 static const int wxTREE_HITTEST_TORIGHT
= 0x0800;
58 // anywhere on the item
59 static const int wxTREE_HITTEST_ONITEM
= wxTREE_HITTEST_ONITEMICON
|
60 wxTREE_HITTEST_ONITEMLABEL
|
61 wxTREE_HITTEST_ONITEMSTATEICON
;
63 // NB: all the following flags are for compatbility only and will be removed in the
66 // flags for deprecated `Expand(int action)'
70 wxTREE_EXPAND_COLLAPSE
,
71 wxTREE_EXPAND_COLLAPSE_RESET
,
75 // flags for deprecated InsertItem() variant
76 #define wxTREE_INSERT_FIRST 0xFFFF0001
77 #define wxTREE_INSERT_LAST 0xFFFF0002
79 // ----------------------------------------------------------------------------
80 // wxTreeItemId identifies an element of the tree. In this implementation, it's
81 // just a trivial wrapper around Win32 HTREEITEM. It's opaque for the
83 // ----------------------------------------------------------------------------
84 class WXDLLEXPORT wxTreeItemId
88 // 0 is invalid value for HTREEITEM
89 wxTreeItemId() { m_itemId
= 0; }
91 // default copy ctor/assignment operator are ok for us
94 // is this a valid tree item?
95 bool IsOk() const { return m_itemId
!= 0; }
97 // conversion to/from either real (system-dependent) tree item id or
98 // to "long" which used to be the type for tree item ids in previous
99 // versions of wxWindows
101 // for wxTreeCtrl usage only
102 wxTreeItemId(WXHTREEITEM itemId
) { m_itemId
= (long)itemId
; }
103 operator WXHTREEITEM() const { return (WXHTREEITEM
)m_itemId
; }
105 void operator=(WXHTREEITEM item
) { m_itemId
= (long) item
; }
111 WX_DEFINE_EXPORTED_ARRAY(wxTreeItemId
, wxArrayTreeItemIds
);
113 // ----------------------------------------------------------------------------
114 // wxTreeItemData is some (arbitrary) user class associated with some item. The
115 // main advantage of having this class (compared to old untyped interface) is
116 // that wxTreeItemData's are destroyed automatically by the tree and, as this
117 // class has virtual dtor, it means that the memory will be automatically
118 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
119 // the size of this class is critical: in any real application, each tree leaf
120 // will have wxTreeItemData associated with it and number of leaves may be
123 // Because the objects of this class are deleted by the tree, they should
124 // always be allocated on the heap!
125 // ----------------------------------------------------------------------------
126 class WXDLLEXPORT wxTreeItemData
: private wxTreeItemId
129 // default ctor/copy ctor/assignment operator are ok
131 // dtor is virtual and all the items are deleted by the tree control when
132 // it's deleted, so you normally don't have to care about freeing memory
133 // allocated in your wxTreeItemData-derived class
134 virtual ~wxTreeItemData() { }
136 // accessors: set/get the item associated with this node
137 void SetId(const wxTreeItemId
& id
) { m_itemId
= id
; }
138 const wxTreeItemId
GetId() const { return *this; }
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
144 class WXDLLEXPORT wxTreeCtrl
: public wxControl
149 wxTreeCtrl() { Init(); }
151 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
152 const wxPoint
& pos
= wxDefaultPosition
,
153 const wxSize
& size
= wxDefaultSize
,
154 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
155 const wxValidator
& validator
= wxDefaultValidator
,
156 const wxString
& name
= "wxTreeCtrl")
158 Create(parent
, id
, pos
, size
, style
, validator
, name
);
161 virtual ~wxTreeCtrl();
163 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
164 const wxPoint
& pos
= wxDefaultPosition
,
165 const wxSize
& size
= wxDefaultSize
,
166 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
167 const wxValidator
& validator
= wxDefaultValidator
,
168 const wxString
& name
= "wxTreeCtrl");
173 // get the total number of items in the control
174 size_t GetCount() const;
176 // indent is the number of pixels the children are indented relative to
177 // the parents position. SetIndent() also redraws the control
179 unsigned int GetIndent() const;
180 void SetIndent(unsigned int indent
);
182 // spacing is the number of pixels between the start and the Text
183 // not implemented under wxMSW
184 unsigned int GetSpacing() const { return 18; } // return wxGTK default
185 void SetSpacing(unsigned int WXUNUSED(spacing
)) { }
187 // image list: these functions allow to associate an image list with
188 // the control and retrieve it. Note that the control does _not_ delete
189 // the associated image list when it's deleted in order to allow image
190 // lists to be shared between different controls.
192 // The normal image list is for the icons which correspond to the
193 // normal tree item state (whether it is selected or not).
194 // Additionally, the application might choose to show a state icon
195 // which corresponds to an app-defined item state (for example,
196 // checked/unchecked) which are taken from the state image list.
197 wxImageList
*GetImageList() const;
198 wxImageList
*GetStateImageList() const;
200 void SetImageList(wxImageList
*imageList
);
201 void SetStateImageList(wxImageList
*imageList
);
203 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
204 // member functions of wxTreeItem because they must know the tree the item
205 // belongs to for Windows implementation and storing the pointer to
206 // wxTreeCtrl in each wxTreeItem is just too much waste.
211 // retrieve items label
212 wxString
GetItemText(const wxTreeItemId
& item
) const;
213 // get one of the images associated with the item (normal by default)
214 int GetItemImage(const wxTreeItemId
& item
,
215 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
216 // get the data associated with the item
217 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
223 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
224 // get one of the images associated with the item (normal by default)
225 void SetItemImage(const wxTreeItemId
& item
, int image
,
226 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
227 // associate some data with the item
228 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
230 // force appearance of [+] button near the item. This is useful to
231 // allow the user to expand the items which don't have any children now
232 // - but instead add them only when needed, thus minimizing memory
233 // usage and loading time.
234 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
236 // the item will be shown in bold
237 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
239 // the item will be shown with a drop highlight
240 void SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
= TRUE
);
242 // item status inquiries
243 // ---------------------
245 // is the item visible (it might be outside the view or not expanded)?
246 bool IsVisible(const wxTreeItemId
& item
) const;
247 // does the item has any children?
248 bool ItemHasChildren(const wxTreeItemId
& item
) const;
249 // is the item expanded (only makes sense if HasChildren())?
250 bool IsExpanded(const wxTreeItemId
& item
) const;
251 // is this item currently selected (the same as has focus)?
252 bool IsSelected(const wxTreeItemId
& item
) const;
253 // is item text in bold font?
254 bool IsBold(const wxTreeItemId
& item
) const;
256 // number of children
257 // ------------------
259 // if 'recursively' is FALSE, only immediate children count, otherwise
260 // the returned number is the number of all items in this branch
261 size_t GetChildrenCount(const wxTreeItemId
& item
,
262 bool recursively
= TRUE
) const;
267 // wxTreeItemId.IsOk() will return FALSE if there is no such item
269 // get the root tree item
270 wxTreeItemId
GetRootItem() const;
272 // get the item currently selected (may return NULL if no selection)
273 wxTreeItemId
GetSelection() const;
275 // get the items currently selected, return the number of such item
277 // NB: this operation is expensive and can take a long time for a
278 // control with a lot of items (~ O(number of items)).
279 size_t GetSelections(wxArrayTreeItemIds
& selections
) const;
281 // get the parent of this item (may return NULL if root)
282 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
284 // for this enumeration function you must pass in a "cookie" parameter
285 // which is opaque for the application but is necessary for the library
286 // to make these functions reentrant (i.e. allow more than one
287 // enumeration on one and the same object simultaneously). Of course,
288 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
291 // get the first child of this item
292 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& _cookie
) const;
293 // get the next child
294 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& _cookie
) const;
295 // get the last child of this item - this method doesn't use cookies
296 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
298 // get the next sibling of this item
299 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
300 // get the previous sibling
301 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
303 // get first visible item
304 wxTreeItemId
GetFirstVisibleItem() const;
305 // get the next visible item: item must be visible itself!
306 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
307 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
308 // get the previous visible item: item must be visible itself!
309 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
314 // add the root node to the tree
315 wxTreeItemId
AddRoot(const wxString
& text
,
316 int image
= -1, int selectedImage
= -1,
317 wxTreeItemData
*data
= NULL
);
319 // insert a new item in as the first child of the parent
320 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
321 const wxString
& text
,
322 int image
= -1, int selectedImage
= -1,
323 wxTreeItemData
*data
= NULL
);
325 // insert a new item after a given one
326 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
327 const wxTreeItemId
& idPrevious
,
328 const wxString
& text
,
329 int image
= -1, int selectedImage
= -1,
330 wxTreeItemData
*data
= NULL
);
332 // insert a new item in as the last child of the parent
333 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
334 const wxString
& text
,
335 int image
= -1, int selectedImage
= -1,
336 wxTreeItemData
*data
= NULL
);
338 // delete this item and associated data if any
339 void Delete(const wxTreeItemId
& item
);
340 // delete all children (but don't delete the item itself)
341 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
342 void DeleteChildren(const wxTreeItemId
& item
);
343 // delete all items from the tree
344 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
345 void DeleteAllItems();
348 void Expand(const wxTreeItemId
& item
);
349 // collapse the item without removing its children
350 void Collapse(const wxTreeItemId
& item
);
351 // collapse the item and remove all children
352 void CollapseAndReset(const wxTreeItemId
& item
);
353 // toggles the current state
354 void Toggle(const wxTreeItemId
& item
);
356 // remove the selection from currently selected item (if any)
358 // unselect all items (only makes sense for multiple selection control)
361 void SelectItem(const wxTreeItemId
& item
);
362 // make sure this item is visible (expanding the parent item and/or
363 // scrolling to this item if necessary)
364 void EnsureVisible(const wxTreeItemId
& item
);
365 // scroll to this item (but don't expand its parent)
366 void ScrollTo(const wxTreeItemId
& item
);
368 // start editing the item label: this (temporarily) replaces the item
369 // with a one line edit control. The item will be selected if it hadn't
370 // been before. textCtrlClass parameter allows you to create an edit
371 // control of arbitrary user-defined class deriving from wxTextCtrl.
372 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
373 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
374 // returns the same pointer as StartEdit() if the item is being edited,
375 // NULL otherwise (it's assumed that no more than one item may be
376 // edited simultaneously)
377 wxTextCtrl
* GetEditControl() const;
378 // end editing and accept or discard the changes to item label
379 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
382 // this function is called to compare 2 items and should return -1, 0
383 // or +1 if the first item is less than, equal to or greater than the
384 // second one. The base class version performs alphabetic comparaison
385 // of item labels (GetText)
386 virtual int OnCompareItems(const wxTreeItemId
& item1
,
387 const wxTreeItemId
& item2
);
388 // sort the children of this item using OnCompareItems
390 // NB: this function is not reentrant and not MT-safe (FIXME)!
391 void SortChildren(const wxTreeItemId
& item
);
396 // determine to which item (if any) belongs the given point (the
397 // coordinates specified are relative to the client area of tree ctrl)
398 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
401 // The first function is more portable (because easier to implement
402 // on other platforms), but the second one returns some extra info.
403 wxTreeItemId
HitTest(const wxPoint
& point
)
404 { int dummy
; return HitTest(point
, dummy
); }
405 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
407 // get the bounding rectangle of the item (or of its label only)
408 bool GetBoundingRect(const wxTreeItemId
& item
,
410 bool textOnly
= FALSE
) const;
415 // these methods are deprecated and will be removed in future versions of
416 // wxWindows, they're here for compatibility only, don't use them in new
417 // code (the comments indicate why these methods are now useless and how to
420 // use Expand, Collapse, CollapseAndReset or Toggle
421 void ExpandItem(const wxTreeItemId
& item
, int action
);
423 // use AddRoot, PrependItem or AppendItem
424 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
425 const wxString
& text
,
426 int image
= -1, int selImage
= -1,
427 long insertAfter
= wxTREE_INSERT_LAST
);
429 // use Set/GetImageList and Set/GetStateImageList
430 wxImageList
*GetImageList(int) const
431 { return GetImageList(); }
432 void SetImageList(wxImageList
*imageList
, int)
433 { SetImageList(imageList
); }
435 // use Set/GetItemImage directly
436 // get the selected item image
437 int GetItemSelectedImage(const wxTreeItemId
& item
) const
438 { return GetItemImage(item
, wxTreeItemIcon_Selected
); }
439 // set the selected item image
440 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
441 { SetItemImage(item
, image
, wxTreeItemIcon_Selected
); }
445 virtual bool OS2Command(WXUINT param
, WXWORD id
);
446 virtual bool OS2OnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
448 // get/set the check state for the item (only for wxTR_MULTIPLE)
449 bool IsItemChecked(const wxTreeItemId
& item
) const;
450 void SetItemCheck(const wxTreeItemId
& item
, bool check
= TRUE
);
453 // SetImageList helper
454 void SetAnyImageList(wxImageList
*imageList
, int which
);
456 wxTextCtrl
*m_textCtrl
; // used while editing the item label
457 wxImageList
*m_imageListNormal
, // images for tree elements
458 *m_imageListState
; // special images for app defined states
461 // the common part of all ctors
465 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
466 inline void DoSetItem(wxTreeViewItem
*tvItem
);
468 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
470 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
471 wxTreeItemId hInsertAfter
,
472 const wxString
& text
,
473 int image
, int selectedImage
,
474 wxTreeItemData
*data
);
476 int DoGetItemImageFromData(const wxTreeItemId
& item
,
477 wxTreeItemIcon which
) const;
478 void DoSetItemImageFromData(const wxTreeItemId
& item
,
480 wxTreeItemIcon which
) const;
481 void DoSetItemImages(const wxTreeItemId
& item
, int image
, int imageSel
);
483 void DeleteTextCtrl();
485 // support for additional item images
486 friend class wxTreeItemIndirectData
;
487 void SetIndirectItemData(const wxTreeItemId
& item
,
488 wxTreeItemIndirectData
*data
);
489 bool HasIndirectData(const wxTreeItemId
& item
) const;
491 wxArrayTreeItemIds m_itemsWithIndirectData
;
493 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)