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 // NB: all the following flags are for compatbility only and will be removed in the
47 // flags for deprecated `Expand(int action)'
51 wxTREE_EXPAND_COLLAPSE
,
52 wxTREE_EXPAND_COLLAPSE_RESET
,
56 // flags for deprecated InsertItem() variant
57 #define wxTREE_INSERT_FIRST 0xFFFF0001
58 #define wxTREE_INSERT_LAST 0xFFFF0002
60 // ----------------------------------------------------------------------------
61 // wxTreeItemId identifies an element of the tree. In this implementation, it's
62 // just a trivial wrapper around Win32 HTREEITEM. It's opaque for the
64 // ----------------------------------------------------------------------------
65 class WXDLLEXPORT wxTreeItemId
69 // 0 is invalid value for HTREEITEM
70 wxTreeItemId() { m_itemId
= 0; }
72 // default copy ctor/assignment operator are ok for us
75 // is this a valid tree item?
76 bool IsOk() const { return m_itemId
!= 0; }
78 // conversion to/from either real (system-dependent) tree item id or
79 // to "long" which used to be the type for tree item ids in previous
80 // versions of wxWindows
82 // for wxTreeCtrl usage only
83 wxTreeItemId(WXHTREEITEM itemId
) { m_itemId
= (long)itemId
; }
84 operator WXHTREEITEM() const { return (WXHTREEITEM
)m_itemId
; }
86 void operator=(WXHTREEITEM item
) { m_itemId
= (long) item
; }
92 WX_DEFINE_EXPORTED_ARRAY(WXHTREEITEM
, wxArrayTreeItemIds
);
94 // ----------------------------------------------------------------------------
95 // wxTreeItemData is some (arbitrary) user class associated with some item. The
96 // main advantage of having this class (compared to old untyped interface) is
97 // that wxTreeItemData's are destroyed automatically by the tree and, as this
98 // class has virtual dtor, it means that the memory will be automatically
99 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
100 // the size of this class is critical: in any real application, each tree leaf
101 // will have wxTreeItemData associated with it and number of leaves may be
104 // Because the objects of this class are deleted by the tree, they should
105 // always be allocated on the heap!
106 // ----------------------------------------------------------------------------
107 class WXDLLEXPORT wxTreeItemData
: private wxTreeItemId
110 // default ctor/copy ctor/assignment operator are ok
112 // dtor is virtual and all the items are deleted by the tree control when
113 // it's deleted, so you normally don't have to care about freeing memory
114 // allocated in your wxTreeItemData-derived class
115 virtual ~wxTreeItemData() { }
117 // accessors: set/get the item associated with this node
118 void SetId(const wxTreeItemId
& id
) { m_itemId
= id
; }
119 const wxTreeItemId
GetId() const { return *this; }
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
125 class WXDLLEXPORT wxTreeCtrl
: public wxControl
130 wxTreeCtrl() { Init(); }
132 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
133 const wxPoint
& pos
= wxDefaultPosition
,
134 const wxSize
& size
= wxDefaultSize
,
135 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
136 const wxValidator
& validator
= wxDefaultValidator
,
137 const wxString
& name
= "wxTreeCtrl")
139 Create(parent
, id
, pos
, size
, style
, validator
, name
);
142 virtual ~wxTreeCtrl();
144 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
145 const wxPoint
& pos
= wxDefaultPosition
,
146 const wxSize
& size
= wxDefaultSize
,
147 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
148 const wxValidator
& validator
= wxDefaultValidator
,
149 const wxString
& name
= "wxTreeCtrl");
154 // get the total number of items in the control
155 size_t GetCount() const;
157 // indent is the number of pixels the children are indented relative to
158 // the parents position. SetIndent() also redraws the control
160 unsigned int GetIndent() const;
161 void SetIndent(unsigned int indent
);
163 // spacing is the number of pixels between the start and the Text
164 // not implemented under wxMSW
165 unsigned int GetSpacing() const { return 18; } // return wxGTK default
166 void SetSpacing(unsigned int WXUNUSED(spacing
)) { }
168 // image list: these functions allow to associate an image list with
169 // the control and retrieve it. Note that the control does _not_ delete
170 // the associated image list when it's deleted in order to allow image
171 // lists to be shared between different controls.
173 // The normal image list is for the icons which correspond to the
174 // normal tree item state (whether it is selected or not).
175 // Additionally, the application might choose to show a state icon
176 // which corresponds to an app-defined item state (for example,
177 // checked/unchecked) which are taken from the state image list.
178 wxImageList
*GetImageList() const;
179 wxImageList
*GetStateImageList() const;
181 void SetImageList(wxImageList
*imageList
);
182 void SetStateImageList(wxImageList
*imageList
);
184 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
185 // member functions of wxTreeItem because they must know the tree the item
186 // belongs to for Windows implementation and storing the pointer to
187 // wxTreeCtrl in each wxTreeItem is just too much waste.
192 // retrieve items label
193 wxString
GetItemText(const wxTreeItemId
& item
) const;
194 // get one of the images associated with the item (normal by default)
195 int GetItemImage(const wxTreeItemId
& item
,
196 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
197 // get the data associated with the item
198 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
204 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
205 // get one of the images associated with the item (normal by default)
206 void SetItemImage(const wxTreeItemId
& item
, int image
,
207 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
208 // associate some data with the item
209 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
211 // force appearance of [+] button near the item. This is useful to
212 // allow the user to expand the items which don't have any children now
213 // - but instead add them only when needed, thus minimizing memory
214 // usage and loading time.
215 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
217 // the item will be shown in bold
218 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
220 // the item will be shown with a drop highlight
221 void SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
= TRUE
);
223 // set the items text colour
224 void SetItemTextColour(const wxTreeItemId
& item
, const wxColour
& col
);
226 // set the items background colour
227 void SetItemBackgroundColour(const wxTreeItemId
& item
, const wxColour
& col
);
229 // set the items font (should be of the same height for all items)
230 void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
232 // item status inquiries
233 // ---------------------
235 // is the item visible (it might be outside the view or not expanded)?
236 bool IsVisible(const wxTreeItemId
& item
) const;
237 // does the item has any children?
238 bool ItemHasChildren(const wxTreeItemId
& item
) const;
239 // is the item expanded (only makes sense if HasChildren())?
240 bool IsExpanded(const wxTreeItemId
& item
) const;
241 // is this item currently selected (the same as has focus)?
242 bool IsSelected(const wxTreeItemId
& item
) const;
243 // is item text in bold font?
244 bool IsBold(const wxTreeItemId
& item
) const;
246 // number of children
247 // ------------------
249 // if 'recursively' is FALSE, only immediate children count, otherwise
250 // the returned number is the number of all items in this branch
251 size_t GetChildrenCount(const wxTreeItemId
& item
,
252 bool recursively
= TRUE
) const;
257 // wxTreeItemId.IsOk() will return FALSE if there is no such item
259 // get the root tree item
260 wxTreeItemId
GetRootItem() const;
262 // get the item currently selected (may return NULL if no selection)
263 wxTreeItemId
GetSelection() const;
265 // get the items currently selected, return the number of such item
267 // NB: this operation is expensive and can take a long time for a
268 // control with a lot of items (~ O(number of items)).
269 size_t GetSelections(wxArrayTreeItemIds
& selections
) const;
271 // get the parent of this item (may return NULL if root)
272 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
274 // for this enumeration function you must pass in a "cookie" parameter
275 // which is opaque for the application but is necessary for the library
276 // to make these functions reentrant (i.e. allow more than one
277 // enumeration on one and the same object simultaneously). Of course,
278 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
281 // get the first child of this item
282 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& _cookie
) const;
283 // get the next child
284 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& _cookie
) const;
285 // get the last child of this item - this method doesn't use cookies
286 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
288 // get the next sibling of this item
289 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
290 // get the previous sibling
291 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
293 // get first visible item
294 wxTreeItemId
GetFirstVisibleItem() const;
295 // get the next visible item: item must be visible itself!
296 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
297 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
298 // get the previous visible item: item must be visible itself!
299 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
304 // add the root node to the tree
305 wxTreeItemId
AddRoot(const wxString
& text
,
306 int image
= -1, int selectedImage
= -1,
307 wxTreeItemData
*data
= NULL
);
309 // insert a new item in as the first child of the parent
310 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
311 const wxString
& text
,
312 int image
= -1, int selectedImage
= -1,
313 wxTreeItemData
*data
= NULL
);
315 // insert a new item after a given one
316 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
317 const wxTreeItemId
& idPrevious
,
318 const wxString
& text
,
319 int image
= -1, int selectedImage
= -1,
320 wxTreeItemData
*data
= NULL
);
322 // insert a new item before the one with the given index
323 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
325 const wxString
& text
,
326 int image
= -1, int selectedImage
= -1,
327 wxTreeItemData
*data
= NULL
);
329 // insert a new item in as the last child of the parent
330 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
331 const wxString
& text
,
332 int image
= -1, int selectedImage
= -1,
333 wxTreeItemData
*data
= NULL
);
335 // delete this item and associated data if any
336 void Delete(const wxTreeItemId
& item
);
337 // delete all children (but don't delete the item itself)
338 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
339 void DeleteChildren(const wxTreeItemId
& item
);
340 // delete all items from the tree
341 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
342 void DeleteAllItems();
345 void Expand(const wxTreeItemId
& item
);
346 // collapse the item without removing its children
347 void Collapse(const wxTreeItemId
& item
);
348 // collapse the item and remove all children
349 void CollapseAndReset(const wxTreeItemId
& item
);
350 // toggles the current state
351 void Toggle(const wxTreeItemId
& item
);
353 // remove the selection from currently selected item (if any)
355 // unselect all items (only makes sense for multiple selection control)
358 void SelectItem(const wxTreeItemId
& item
);
359 // make sure this item is visible (expanding the parent item and/or
360 // scrolling to this item if necessary)
361 void EnsureVisible(const wxTreeItemId
& item
);
362 // scroll to this item (but don't expand its parent)
363 void ScrollTo(const wxTreeItemId
& item
);
365 // start editing the item label: this (temporarily) replaces the item
366 // with a one line edit control. The item will be selected if it hadn't
367 // been before. textCtrlClass parameter allows you to create an edit
368 // control of arbitrary user-defined class deriving from wxTextCtrl.
369 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
370 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
371 // returns the same pointer as StartEdit() if the item is being edited,
372 // NULL otherwise (it's assumed that no more than one item may be
373 // edited simultaneously)
374 wxTextCtrl
* GetEditControl() const;
375 // end editing and accept or discard the changes to item label
376 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
379 // this function is called to compare 2 items and should return -1, 0
380 // or +1 if the first item is less than, equal to or greater than the
381 // second one. The base class version performs alphabetic comparaison
382 // of item labels (GetText)
383 virtual int OnCompareItems(const wxTreeItemId
& item1
,
384 const wxTreeItemId
& item2
);
385 // sort the children of this item using OnCompareItems
387 // NB: this function is not reentrant and not MT-safe (FIXME)!
388 void SortChildren(const wxTreeItemId
& item
);
393 // determine to which item (if any) belongs the given point (the
394 // coordinates specified are relative to the client area of tree ctrl)
395 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
398 // The first function is more portable (because easier to implement
399 // on other platforms), but the second one returns some extra info.
400 wxTreeItemId
HitTest(const wxPoint
& point
)
401 { int dummy
; return HitTest(point
, dummy
); }
402 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
404 // get the bounding rectangle of the item (or of its label only)
405 bool GetBoundingRect(const wxTreeItemId
& item
,
407 bool textOnly
= FALSE
) const;
412 // these methods are deprecated and will be removed in future versions of
413 // wxWindows, they're here for compatibility only, don't use them in new
414 // code (the comments indicate why these methods are now useless and how to
417 // use Expand, Collapse, CollapseAndReset or Toggle
418 void ExpandItem(const wxTreeItemId
& item
, int action
);
420 // use AddRoot, PrependItem or AppendItem
421 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
422 const wxString
& text
,
423 int image
= -1, int selImage
= -1,
424 long insertAfter
= wxTREE_INSERT_LAST
);
426 // use Set/GetImageList and Set/GetStateImageList
427 wxImageList
*GetImageList(int) const
428 { return GetImageList(); }
429 void SetImageList(wxImageList
*imageList
, int)
430 { SetImageList(imageList
); }
432 // use Set/GetItemImage directly
433 // get the selected item image
434 int GetItemSelectedImage(const wxTreeItemId
& item
) const
435 { return GetItemImage(item
, wxTreeItemIcon_Selected
); }
436 // set the selected item image
437 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
438 { SetItemImage(item
, image
, wxTreeItemIcon_Selected
); }
442 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
443 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
445 // override some base class virtuals
446 virtual bool SetBackgroundColour(const wxColour
&colour
);
447 virtual bool SetForegroundColour(const wxColour
&colour
);
449 // get/set the check state for the item (only for wxTR_MULTIPLE)
450 bool IsItemChecked(const wxTreeItemId
& item
) const;
451 void SetItemCheck(const wxTreeItemId
& item
, bool check
= TRUE
);
454 // SetImageList helper
455 void SetAnyImageList(wxImageList
*imageList
, int which
);
457 wxTextCtrl
*m_textCtrl
; // used while editing the item label
458 wxImageList
*m_imageListNormal
, // images for tree elements
459 *m_imageListState
; // special images for app defined states
462 // the common part of all ctors
466 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
467 inline void DoSetItem(wxTreeViewItem
*tvItem
);
469 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
471 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
472 wxTreeItemId hInsertAfter
,
473 const wxString
& text
,
474 int image
, int selectedImage
,
475 wxTreeItemData
*data
);
477 int DoGetItemImageFromData(const wxTreeItemId
& item
,
478 wxTreeItemIcon which
) const;
479 void DoSetItemImageFromData(const wxTreeItemId
& item
,
481 wxTreeItemIcon which
) const;
482 void DoSetItemImages(const wxTreeItemId
& item
, int image
, int imageSel
);
484 void DeleteTextCtrl();
486 // support for additional item images
487 friend class wxTreeItemIndirectData
;
488 void SetIndirectItemData(const wxTreeItemId
& item
,
489 wxTreeItemIndirectData
*data
);
490 bool HasIndirectData(const wxTreeItemId
& item
) const;
492 // the array storing all item ids which have indirect data
493 wxArrayTreeItemIds m_itemsWithIndirectData
;
495 // the hash storing the items attributes (indexed by items ids)
498 // TRUE if the hash above is not empty
501 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)