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_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
; }
153 const wxTreeItemId
GetId() const { return m_itemId
; }
155 const wxTreeItemId
& GetId() const { return (wxTreeItemId
&) m_itemId
; }
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
162 class WXDLLEXPORT wxTreeCtrl
: public wxControl
167 wxTreeCtrl() { Init(); }
169 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
170 const wxPoint
& pos
= wxDefaultPosition
,
171 const wxSize
& size
= wxDefaultSize
,
172 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
173 const wxValidator
& validator
= wxDefaultValidator
,
174 const wxString
& name
= "wxTreeCtrl")
176 Create(parent
, id
, pos
, size
, style
, validator
, name
);
179 virtual ~wxTreeCtrl();
181 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
182 const wxPoint
& pos
= wxDefaultPosition
,
183 const wxSize
& size
= wxDefaultSize
,
184 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
185 const wxValidator
& validator
= wxDefaultValidator
,
186 const wxString
& name
= "wxTreeCtrl");
191 // get the total number of items in the control
192 size_t GetCount() const;
194 // indent is the number of pixels the children are indented relative to
195 // the parents position. SetIndent() also redraws the control
197 unsigned int GetIndent() const;
198 void SetIndent(unsigned int indent
);
200 // spacing is the number of pixels between the start and the Text
201 // not implemented under wxMSW
202 unsigned int GetSpacing() const { return 18; } // return wxGTK default
203 void SetSpacing(unsigned int ) {}
205 // image list: these functions allow to associate an image list with
206 // the control and retrieve it. Note that the control does _not_ delete
207 // the associated image list when it's deleted in order to allow image
208 // lists to be shared between different controls.
210 // The normal image list is for the icons which correspond to the
211 // normal tree item state (whether it is selected or not).
212 // Additionally, the application might choose to show a state icon
213 // which corresponds to an app-defined item state (for example,
214 // checked/unchecked) which are taken from the state image list.
215 wxImageList
*GetImageList() const;
216 wxImageList
*GetStateImageList() const;
218 void SetImageList(wxImageList
*imageList
);
219 void SetStateImageList(wxImageList
*imageList
);
221 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
222 // member functions of wxTreeItem because they must know the tree the item
223 // belongs to for Windows implementation and storing the pointer to
224 // wxTreeCtrl in each wxTreeItem is just too much waste.
229 // retrieve items label
230 wxString
GetItemText(const wxTreeItemId
& item
) const;
231 // get the normal item image
232 int GetItemImage(const wxTreeItemId
& item
) const;
233 // get the selected item image
234 int GetItemSelectedImage(const wxTreeItemId
& item
) const;
235 // get the data associated with the item
236 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
242 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
243 // set the normal item image
244 void SetItemImage(const wxTreeItemId
& item
, int image
);
245 // set the selected item image
246 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
);
247 // associate some data with the item
248 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
250 // force appearance of [+] button near the item. This is useful to
251 // allow the user to expand the items which don't have any children now
252 // - but instead add them only when needed, thus minimizing memory
253 // usage and loading time.
254 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
256 // the item will be shown in bold
257 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
259 // the item will be shown with a drop highlight
260 void SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
= TRUE
);
262 // item status inquiries
263 // ---------------------
265 // is the item visible (it might be outside the view or not expanded)?
266 bool IsVisible(const wxTreeItemId
& item
) const;
267 // does the item has any children?
268 bool ItemHasChildren(const wxTreeItemId
& item
) const;
269 // is the item expanded (only makes sense if HasChildren())?
270 bool IsExpanded(const wxTreeItemId
& item
) const;
271 // is this item currently selected (the same as has focus)?
272 bool IsSelected(const wxTreeItemId
& item
) const;
273 // is item text in bold font?
274 bool IsBold(const wxTreeItemId
& item
) const;
276 // number of children
277 // ------------------
279 // if 'recursively' is FALSE, only immediate children count, otherwise
280 // the returned number is the number of all items in this branch
281 size_t GetChildrenCount(const wxTreeItemId
& item
,
282 bool recursively
= TRUE
) const;
287 // wxTreeItemId.IsOk() will return FALSE if there is no such item
289 // get the root tree item
290 wxTreeItemId
GetRootItem() const;
292 // get the item currently selected (may return NULL if no selection)
293 wxTreeItemId
GetSelection() const;
295 // get the items currently selected, return the number of such item
297 // NB: this operation is expensive and can take a long time for a
298 // control with a lot of items (~ O(number of items)).
299 size_t GetSelections(wxArrayTreeItemIds
& selections
) const;
301 // get the parent of this item (may return NULL if root)
302 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
304 // for this enumeration function you must pass in a "cookie" parameter
305 // which is opaque for the application but is necessary for the library
306 // to make these functions reentrant (i.e. allow more than one
307 // enumeration on one and the same object simultaneously). Of course,
308 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
311 // get the first child of this item
312 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& _cookie
) const;
313 // get the next child
314 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& _cookie
) const;
315 // get the last child of this item - this method doesn't use cookies
316 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
318 // get the next sibling of this item
319 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
320 // get the previous sibling
321 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
323 // get first visible item
324 wxTreeItemId
GetFirstVisibleItem() const;
325 // get the next visible item: item must be visible itself!
326 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
327 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
328 // get the previous visible item: item must be visible itself!
329 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
334 // add the root node to the tree
335 wxTreeItemId
AddRoot(const wxString
& text
,
336 int image
= -1, int selectedImage
= -1,
337 wxTreeItemData
*data
= NULL
);
339 // insert a new item in as the first child of the parent
340 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
341 const wxString
& text
,
342 int image
= -1, int selectedImage
= -1,
343 wxTreeItemData
*data
= NULL
);
345 // insert a new item after a given one
346 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
347 const wxTreeItemId
& idPrevious
,
348 const wxString
& text
,
349 int image
= -1, int selectedImage
= -1,
350 wxTreeItemData
*data
= NULL
);
352 // insert a new item in as the last child of the parent
353 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
354 const wxString
& text
,
355 int image
= -1, int selectedImage
= -1,
356 wxTreeItemData
*data
= NULL
);
358 // delete this item and associated data if any
359 void Delete(const wxTreeItemId
& item
);
360 // delete all children (but don't delete the item itself)
361 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
362 void DeleteChildren(const wxTreeItemId
& item
);
363 // delete all items from the tree
364 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
365 void DeleteAllItems();
368 void Expand(const wxTreeItemId
& item
);
369 // collapse the item without removing its children
370 void Collapse(const wxTreeItemId
& item
);
371 // collapse the item and remove all children
372 void CollapseAndReset(const wxTreeItemId
& item
);
373 // toggles the current state
374 void Toggle(const wxTreeItemId
& item
);
376 // remove the selection from currently selected item (if any)
378 // unselect all items (only makes sense for multiple selection control)
381 void SelectItem(const wxTreeItemId
& item
);
382 // make sure this item is visible (expanding the parent item and/or
383 // scrolling to this item if necessary)
384 void EnsureVisible(const wxTreeItemId
& item
);
385 // scroll to this item (but don't expand its parent)
386 void ScrollTo(const wxTreeItemId
& item
);
388 // start editing the item label: this (temporarily) replaces the item
389 // with a one line edit control. The item will be selected if it hadn't
390 // been before. textCtrlClass parameter allows you to create an edit
391 // control of arbitrary user-defined class deriving from wxTextCtrl.
392 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
393 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
394 // returns the same pointer as StartEdit() if the item is being edited,
395 // NULL otherwise (it's assumed that no more than one item may be
396 // edited simultaneously)
397 wxTextCtrl
* GetEditControl() const;
398 // end editing and accept or discard the changes to item label
399 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
402 // this function is called to compare 2 items and should return -1, 0
403 // or +1 if the first item is less than, equal to or greater than the
404 // second one. The base class version performs alphabetic comparaison
405 // of item labels (GetText)
406 virtual int OnCompareItems(const wxTreeItemId
& item1
,
407 const wxTreeItemId
& item2
);
408 // sort the children of this item using OnCompareItems
410 // NB: this function is not reentrant and not MT-safe (FIXME)!
411 void SortChildren(const wxTreeItemId
& item
);
416 // determine to which item (if any) belongs the given point (the
417 // coordinates specified are relative to the client area of tree ctrl)
418 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
421 // The first function is more portable (because easier to implement
422 // on other platforms), but the second one returns some extra info.
423 wxTreeItemId
HitTest(const wxPoint
& point
)
424 { int dummy
; return HitTest(point
, dummy
); }
425 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
427 // get the bounding rectangle of the item (or of its label only)
428 bool GetBoundingRect(const wxTreeItemId
& item
,
430 bool textOnly
= FALSE
) const;
435 // these methods are deprecated and will be removed in future versions of
436 // wxWindows, they're here for compatibility only, don't use them in new
437 // code (the comments indicate why these methods are now useless and how to
440 // use Expand, Collapse, CollapseAndReset or Toggle
441 void ExpandItem(const wxTreeItemId
& item
, int action
);
443 // use AddRoot, PrependItem or AppendItem
444 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
445 const wxString
& text
,
446 int image
= -1, int selImage
= -1,
447 long insertAfter
= wxTREE_INSERT_LAST
);
449 // use Set/GetImageList and Set/GetStateImageList
450 wxImageList
*GetImageList(int) const
451 { return GetImageList(); }
452 void SetImageList(wxImageList
*imageList
, int)
453 { SetImageList(imageList
); }
457 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
458 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
460 // get/set the check state for the item (only for wxTR_MULTIPLE)
461 bool IsItemChecked(const wxTreeItemId
& item
) const;
462 void SetItemCheck(const wxTreeItemId
& item
, bool check
= TRUE
);
465 // SetImageList helper
466 void SetAnyImageList(wxImageList
*imageList
, int which
);
468 wxTextCtrl
* m_textCtrl
; // used while editing the item label
469 wxImageList
*m_imageListNormal
, // images for tree elements
470 *m_imageListState
; // special images for app defined states
473 // the common part of all ctors
477 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
478 inline void DoSetItem(wxTreeViewItem
*tvItem
);
480 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
482 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
483 wxTreeItemId hInsertAfter
,
484 const wxString
& text
,
485 int image
, int selectedImage
,
486 wxTreeItemData
*data
);
488 void DoSetItemImages(const wxTreeItemId
& item
, int image
, int imageSel
);
490 void DeleteTextCtrl();
492 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)