1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl class
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10/10/98
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TREECTRL_H_
13 #define _WX_TREECTRL_H_
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
20 #pragma interface "treectrl.h"
23 #include "wx/textctrl.h"
24 #include "wx/dynarray.h"
27 // Cygwin windows.h defines these identifiers
32 // the type for "untyped" data
33 typedef long wxDataType
;
36 class WXDLLEXPORT wxImageList
;
37 struct WXDLLEXPORT wxTreeViewItem
;
39 // a callback function used for sorting tree items, it should return -1 if the
40 // first item precedes the second, +1 if the second precedes the first or 0 if
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
49 // where exactly the specified point is situated:
50 // above the client area.
51 static const int wxTREE_HITTEST_ABOVE
= 0x0001;
52 // below the client area.
53 static const int wxTREE_HITTEST_BELOW
= 0x0002;
54 // in the client area but below the last item.
55 static const int wxTREE_HITTEST_NOWHERE
= 0x0004;
56 // on the button associated with an item.
57 static const int wxTREE_HITTEST_ONITEMBUTTON
= 0x0010;
58 // on the bitmap associated with an item.
59 static const int wxTREE_HITTEST_ONITEMICON
= 0x0020;
60 // in the indentation associated with an item.
61 static const int wxTREE_HITTEST_ONITEMINDENT
= 0x0040;
62 // on the label (string) associated with an item.
63 static const int wxTREE_HITTEST_ONITEMLABEL
= 0x0080;
64 // in the area to the right of an item.
65 static const int wxTREE_HITTEST_ONITEMRIGHT
= 0x0100;
66 // on the state icon for a tree view item that is in a user-defined state.
67 static const int wxTREE_HITTEST_ONITEMSTATEICON
= 0x0200;
68 // to the right of the client area.
69 static const int wxTREE_HITTEST_TOLEFT
= 0x0400;
70 // to the left of the client area.
71 static const int wxTREE_HITTEST_TORIGHT
= 0x0800;
72 // anywhere on the item
73 static const int wxTREE_HITTEST_ONITEM
= wxTREE_HITTEST_ONITEMICON
|
74 wxTREE_HITTEST_ONITEMLABEL
|
75 wxTREE_HITTEST_ONITEMSTATEICON
;
77 // NB: all the following flags are for compatbility only and will be removed in the
80 // flags for deprecated `Expand(int action)'
84 wxTREE_EXPAND_COLLAPSE
,
85 wxTREE_EXPAND_COLLAPSE_RESET
,
89 // flags for deprecated InsertItem() variant
90 #define wxTREE_INSERT_FIRST 0xFFFF0001
91 #define wxTREE_INSERT_LAST 0xFFFF0002
93 // ----------------------------------------------------------------------------
94 // wxTreeItemId identifies an element of the tree. In this implementation, it's
95 // just a trivial wrapper around Win32 HTREEITEM. It's opaque for the
97 // ----------------------------------------------------------------------------
98 class WXDLLEXPORT wxTreeItemId
102 // 0 is invalid value for HTREEITEM
103 wxTreeItemId() { m_itemId
= 0; }
105 // default copy ctor/assignment operator are ok for us
108 // is this a valid tree item?
109 bool IsOk() const { return m_itemId
!= 0; }
111 // conversion to/from either real (system-dependent) tree item id or
112 // to "long" which used to be the type for tree item ids in previous
113 // versions of wxWindows
115 // for wxTreeCtrl usage only
116 wxTreeItemId(WXHTREEITEM itemId
) { m_itemId
= (long)itemId
; }
117 operator WXHTREEITEM() const { return (WXHTREEITEM
)m_itemId
; }
119 void operator=(WXHTREEITEM item
) { m_itemId
= (long) item
; }
125 WX_DEFINE_EXPORTED_ARRAY(wxTreeItemId
, wxArrayTreeItemIds
);
127 // ----------------------------------------------------------------------------
128 // wxTreeItemData is some (arbitrary) user class associated with some item. The
129 // main advantage of having this class (compared to old untyped interface) is
130 // that wxTreeItemData's are destroyed automatically by the tree and, as this
131 // class has virtual dtor, it means that the memory will be automatically
132 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
133 // the size of this class is critical: in any real application, each tree leaf
134 // will have wxTreeItemData associated with it and number of leaves may be
137 // Because the objects of this class are deleted by the tree, they should
138 // always be allocated on the heap!
139 // ----------------------------------------------------------------------------
140 class WXDLLEXPORT wxTreeItemData
: private wxTreeItemId
143 // default ctor/copy ctor/assignment operator are ok
145 // dtor is virtual and all the items are deleted by the tree control when
146 // it's deleted, so you normally don't have to care about freeing memory
147 // allocated in your wxTreeItemData-derived class
148 virtual ~wxTreeItemData() { }
150 // accessors: set/get the item associated with this node
151 void SetId(const wxTreeItemId
& id
) { m_itemId
= id
; }
152 const wxTreeItemId
GetId() const { return *this; }
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
158 class WXDLLEXPORT wxTreeCtrl
: public wxControl
163 wxTreeCtrl() { Init(); }
165 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
166 const wxPoint
& pos
= wxDefaultPosition
,
167 const wxSize
& size
= wxDefaultSize
,
168 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
169 const wxValidator
& validator
= wxDefaultValidator
,
170 const wxString
& name
= "wxTreeCtrl")
172 Create(parent
, id
, pos
, size
, style
, validator
, name
);
175 virtual ~wxTreeCtrl();
177 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
178 const wxPoint
& pos
= wxDefaultPosition
,
179 const wxSize
& size
= wxDefaultSize
,
180 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
181 const wxValidator
& validator
= wxDefaultValidator
,
182 const wxString
& name
= "wxTreeCtrl");
187 // get the total number of items in the control
188 size_t GetCount() const;
190 // indent is the number of pixels the children are indented relative to
191 // the parents position. SetIndent() also redraws the control
193 unsigned int GetIndent() const;
194 void SetIndent(unsigned int indent
);
196 // spacing is the number of pixels between the start and the Text
197 // not implemented under wxMSW
198 unsigned int GetSpacing() const { return 18; } // return wxGTK default
199 void SetSpacing(unsigned int WXUNUSED(spacing
)) { }
201 // image list: these functions allow to associate an image list with
202 // the control and retrieve it. Note that the control does _not_ delete
203 // the associated image list when it's deleted in order to allow image
204 // lists to be shared between different controls.
206 // The normal image list is for the icons which correspond to the
207 // normal tree item state (whether it is selected or not).
208 // Additionally, the application might choose to show a state icon
209 // which corresponds to an app-defined item state (for example,
210 // checked/unchecked) which are taken from the state image list.
211 wxImageList
*GetImageList() const;
212 wxImageList
*GetStateImageList() const;
214 void SetImageList(wxImageList
*imageList
);
215 void SetStateImageList(wxImageList
*imageList
);
217 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
218 // member functions of wxTreeItem because they must know the tree the item
219 // belongs to for Windows implementation and storing the pointer to
220 // wxTreeCtrl in each wxTreeItem is just too much waste.
225 // retrieve items label
226 wxString
GetItemText(const wxTreeItemId
& item
) const;
227 // get one of the images associated with the item (normal by default)
228 int GetItemImage(const wxTreeItemId
& item
,
229 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
230 // get the data associated with the item
231 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
237 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
238 // get one of the images associated with the item (normal by default)
239 void SetItemImage(const wxTreeItemId
& item
, int image
,
240 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
241 // associate some data with the item
242 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
244 // force appearance of [+] button near the item. This is useful to
245 // allow the user to expand the items which don't have any children now
246 // - but instead add them only when needed, thus minimizing memory
247 // usage and loading time.
248 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
250 // the item will be shown in bold
251 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
253 // the item will be shown with a drop highlight
254 void SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
= TRUE
);
256 // item status inquiries
257 // ---------------------
259 // is the item visible (it might be outside the view or not expanded)?
260 bool IsVisible(const wxTreeItemId
& item
) const;
261 // does the item has any children?
262 bool ItemHasChildren(const wxTreeItemId
& item
) const;
263 // is the item expanded (only makes sense if HasChildren())?
264 bool IsExpanded(const wxTreeItemId
& item
) const;
265 // is this item currently selected (the same as has focus)?
266 bool IsSelected(const wxTreeItemId
& item
) const;
267 // is item text in bold font?
268 bool IsBold(const wxTreeItemId
& item
) const;
270 // number of children
271 // ------------------
273 // if 'recursively' is FALSE, only immediate children count, otherwise
274 // the returned number is the number of all items in this branch
275 size_t GetChildrenCount(const wxTreeItemId
& item
,
276 bool recursively
= TRUE
) const;
281 // wxTreeItemId.IsOk() will return FALSE if there is no such item
283 // get the root tree item
284 wxTreeItemId
GetRootItem() const;
286 // get the item currently selected (may return NULL if no selection)
287 wxTreeItemId
GetSelection() const;
289 // get the items currently selected, return the number of such item
291 // NB: this operation is expensive and can take a long time for a
292 // control with a lot of items (~ O(number of items)).
293 size_t GetSelections(wxArrayTreeItemIds
& selections
) const;
295 // get the parent of this item (may return NULL if root)
296 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
298 // for this enumeration function you must pass in a "cookie" parameter
299 // which is opaque for the application but is necessary for the library
300 // to make these functions reentrant (i.e. allow more than one
301 // enumeration on one and the same object simultaneously). Of course,
302 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
305 // get the first child of this item
306 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& _cookie
) const;
307 // get the next child
308 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& _cookie
) const;
309 // get the last child of this item - this method doesn't use cookies
310 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
312 // get the next sibling of this item
313 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
314 // get the previous sibling
315 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
317 // get first visible item
318 wxTreeItemId
GetFirstVisibleItem() const;
319 // get the next visible item: item must be visible itself!
320 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
321 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
322 // get the previous visible item: item must be visible itself!
323 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
328 // add the root node to the tree
329 wxTreeItemId
AddRoot(const wxString
& text
,
330 int image
= -1, int selectedImage
= -1,
331 wxTreeItemData
*data
= NULL
);
333 // insert a new item in as the first child of the parent
334 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
335 const wxString
& text
,
336 int image
= -1, int selectedImage
= -1,
337 wxTreeItemData
*data
= NULL
);
339 // insert a new item after a given one
340 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
341 const wxTreeItemId
& idPrevious
,
342 const wxString
& text
,
343 int image
= -1, int selectedImage
= -1,
344 wxTreeItemData
*data
= NULL
);
346 // insert a new item in as the last child of the parent
347 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
348 const wxString
& text
,
349 int image
= -1, int selectedImage
= -1,
350 wxTreeItemData
*data
= NULL
);
352 // delete this item and associated data if any
353 void Delete(const wxTreeItemId
& item
);
354 // delete all children (but don't delete the item itself)
355 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
356 void DeleteChildren(const wxTreeItemId
& item
);
357 // delete all items from the tree
358 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
359 void DeleteAllItems();
362 void Expand(const wxTreeItemId
& item
);
363 // collapse the item without removing its children
364 void Collapse(const wxTreeItemId
& item
);
365 // collapse the item and remove all children
366 void CollapseAndReset(const wxTreeItemId
& item
);
367 // toggles the current state
368 void Toggle(const wxTreeItemId
& item
);
370 // remove the selection from currently selected item (if any)
372 // unselect all items (only makes sense for multiple selection control)
375 void SelectItem(const wxTreeItemId
& item
);
376 // make sure this item is visible (expanding the parent item and/or
377 // scrolling to this item if necessary)
378 void EnsureVisible(const wxTreeItemId
& item
);
379 // scroll to this item (but don't expand its parent)
380 void ScrollTo(const wxTreeItemId
& item
);
382 // start editing the item label: this (temporarily) replaces the item
383 // with a one line edit control. The item will be selected if it hadn't
384 // been before. textCtrlClass parameter allows you to create an edit
385 // control of arbitrary user-defined class deriving from wxTextCtrl.
386 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
387 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
388 // returns the same pointer as StartEdit() if the item is being edited,
389 // NULL otherwise (it's assumed that no more than one item may be
390 // edited simultaneously)
391 wxTextCtrl
* GetEditControl() const;
392 // end editing and accept or discard the changes to item label
393 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
396 // this function is called to compare 2 items and should return -1, 0
397 // or +1 if the first item is less than, equal to or greater than the
398 // second one. The base class version performs alphabetic comparaison
399 // of item labels (GetText)
400 virtual int OnCompareItems(const wxTreeItemId
& item1
,
401 const wxTreeItemId
& item2
);
402 // sort the children of this item using OnCompareItems
404 // NB: this function is not reentrant and not MT-safe (FIXME)!
405 void SortChildren(const wxTreeItemId
& item
);
410 // determine to which item (if any) belongs the given point (the
411 // coordinates specified are relative to the client area of tree ctrl)
412 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
415 // The first function is more portable (because easier to implement
416 // on other platforms), but the second one returns some extra info.
417 wxTreeItemId
HitTest(const wxPoint
& point
)
418 { int dummy
; return HitTest(point
, dummy
); }
419 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
421 // get the bounding rectangle of the item (or of its label only)
422 bool GetBoundingRect(const wxTreeItemId
& item
,
424 bool textOnly
= FALSE
) const;
429 // these methods are deprecated and will be removed in future versions of
430 // wxWindows, they're here for compatibility only, don't use them in new
431 // code (the comments indicate why these methods are now useless and how to
434 // use Expand, Collapse, CollapseAndReset or Toggle
435 void ExpandItem(const wxTreeItemId
& item
, int action
);
437 // use AddRoot, PrependItem or AppendItem
438 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
439 const wxString
& text
,
440 int image
= -1, int selImage
= -1,
441 long insertAfter
= wxTREE_INSERT_LAST
);
443 // use Set/GetImageList and Set/GetStateImageList
444 wxImageList
*GetImageList(int) const
445 { return GetImageList(); }
446 void SetImageList(wxImageList
*imageList
, int)
447 { SetImageList(imageList
); }
449 // use Set/GetItemImage directly
450 // get the selected item image
451 int GetItemSelectedImage(const wxTreeItemId
& item
) const
452 { return GetItemImage(item
, wxTreeItemIcon_Selected
); }
453 // set the selected item image
454 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
455 { SetItemImage(item
, image
, wxTreeItemIcon_Selected
); }
459 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
460 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
462 // get/set the check state for the item (only for wxTR_MULTIPLE)
463 bool IsItemChecked(const wxTreeItemId
& item
) const;
464 void SetItemCheck(const wxTreeItemId
& item
, bool check
= TRUE
);
467 // SetImageList helper
468 void SetAnyImageList(wxImageList
*imageList
, int which
);
470 wxTextCtrl
*m_textCtrl
; // used while editing the item label
471 wxImageList
*m_imageListNormal
, // images for tree elements
472 *m_imageListState
; // special images for app defined states
475 // the common part of all ctors
479 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
480 inline void DoSetItem(wxTreeViewItem
*tvItem
);
482 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
484 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
485 wxTreeItemId hInsertAfter
,
486 const wxString
& text
,
487 int image
, int selectedImage
,
488 wxTreeItemData
*data
);
490 int DoGetItemImageFromData(const wxTreeItemId
& item
,
491 wxTreeItemIcon which
) const;
492 void DoSetItemImageFromData(const wxTreeItemId
& item
,
494 wxTreeItemIcon which
) const;
495 void DoSetItemImages(const wxTreeItemId
& item
, int image
, int imageSel
);
497 void DeleteTextCtrl();
499 // support for additional item images
500 friend class wxTreeItemIndirectData
;
501 void SetIndirectItemData(const wxTreeItemId
& item
,
502 wxTreeItemIndirectData
*data
);
503 bool HasIndirectData(const wxTreeItemId
& item
) const;
505 wxArrayTreeItemIds m_itemsWithIndirectData
;
507 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)