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 in as the last child of the parent
323 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
324 const wxString
& text
,
325 int image
= -1, int selectedImage
= -1,
326 wxTreeItemData
*data
= NULL
);
328 // delete this item and associated data if any
329 void Delete(const wxTreeItemId
& item
);
330 // delete all children (but don't delete the item itself)
331 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
332 void DeleteChildren(const wxTreeItemId
& item
);
333 // delete all items from the tree
334 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
335 void DeleteAllItems();
338 void Expand(const wxTreeItemId
& item
);
339 // collapse the item without removing its children
340 void Collapse(const wxTreeItemId
& item
);
341 // collapse the item and remove all children
342 void CollapseAndReset(const wxTreeItemId
& item
);
343 // toggles the current state
344 void Toggle(const wxTreeItemId
& item
);
346 // remove the selection from currently selected item (if any)
348 // unselect all items (only makes sense for multiple selection control)
351 void SelectItem(const wxTreeItemId
& item
);
352 // make sure this item is visible (expanding the parent item and/or
353 // scrolling to this item if necessary)
354 void EnsureVisible(const wxTreeItemId
& item
);
355 // scroll to this item (but don't expand its parent)
356 void ScrollTo(const wxTreeItemId
& item
);
358 // start editing the item label: this (temporarily) replaces the item
359 // with a one line edit control. The item will be selected if it hadn't
360 // been before. textCtrlClass parameter allows you to create an edit
361 // control of arbitrary user-defined class deriving from wxTextCtrl.
362 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
363 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
364 // returns the same pointer as StartEdit() if the item is being edited,
365 // NULL otherwise (it's assumed that no more than one item may be
366 // edited simultaneously)
367 wxTextCtrl
* GetEditControl() const;
368 // end editing and accept or discard the changes to item label
369 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
372 // this function is called to compare 2 items and should return -1, 0
373 // or +1 if the first item is less than, equal to or greater than the
374 // second one. The base class version performs alphabetic comparaison
375 // of item labels (GetText)
376 virtual int OnCompareItems(const wxTreeItemId
& item1
,
377 const wxTreeItemId
& item2
);
378 // sort the children of this item using OnCompareItems
380 // NB: this function is not reentrant and not MT-safe (FIXME)!
381 void SortChildren(const wxTreeItemId
& item
);
386 // determine to which item (if any) belongs the given point (the
387 // coordinates specified are relative to the client area of tree ctrl)
388 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
391 // The first function is more portable (because easier to implement
392 // on other platforms), but the second one returns some extra info.
393 wxTreeItemId
HitTest(const wxPoint
& point
)
394 { int dummy
; return HitTest(point
, dummy
); }
395 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
397 // get the bounding rectangle of the item (or of its label only)
398 bool GetBoundingRect(const wxTreeItemId
& item
,
400 bool textOnly
= FALSE
) const;
405 // these methods are deprecated and will be removed in future versions of
406 // wxWindows, they're here for compatibility only, don't use them in new
407 // code (the comments indicate why these methods are now useless and how to
410 // use Expand, Collapse, CollapseAndReset or Toggle
411 void ExpandItem(const wxTreeItemId
& item
, int action
);
413 // use AddRoot, PrependItem or AppendItem
414 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
415 const wxString
& text
,
416 int image
= -1, int selImage
= -1,
417 long insertAfter
= wxTREE_INSERT_LAST
);
419 // use Set/GetImageList and Set/GetStateImageList
420 wxImageList
*GetImageList(int) const
421 { return GetImageList(); }
422 void SetImageList(wxImageList
*imageList
, int)
423 { SetImageList(imageList
); }
425 // use Set/GetItemImage directly
426 // get the selected item image
427 int GetItemSelectedImage(const wxTreeItemId
& item
) const
428 { return GetItemImage(item
, wxTreeItemIcon_Selected
); }
429 // set the selected item image
430 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
431 { SetItemImage(item
, image
, wxTreeItemIcon_Selected
); }
435 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
436 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
438 // get/set the check state for the item (only for wxTR_MULTIPLE)
439 bool IsItemChecked(const wxTreeItemId
& item
) const;
440 void SetItemCheck(const wxTreeItemId
& item
, bool check
= TRUE
);
443 // SetImageList helper
444 void SetAnyImageList(wxImageList
*imageList
, int which
);
446 wxTextCtrl
*m_textCtrl
; // used while editing the item label
447 wxImageList
*m_imageListNormal
, // images for tree elements
448 *m_imageListState
; // special images for app defined states
451 // the common part of all ctors
455 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
456 inline void DoSetItem(wxTreeViewItem
*tvItem
);
458 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
460 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
461 wxTreeItemId hInsertAfter
,
462 const wxString
& text
,
463 int image
, int selectedImage
,
464 wxTreeItemData
*data
);
466 int DoGetItemImageFromData(const wxTreeItemId
& item
,
467 wxTreeItemIcon which
) const;
468 void DoSetItemImageFromData(const wxTreeItemId
& item
,
470 wxTreeItemIcon which
) const;
471 void DoSetItemImages(const wxTreeItemId
& item
, int image
, int imageSel
);
473 void DeleteTextCtrl();
475 // support for additional item images
476 friend class wxTreeItemIndirectData
;
477 void SetIndirectItemData(const wxTreeItemId
& item
,
478 wxTreeItemIndirectData
*data
);
479 bool HasIndirectData(const wxTreeItemId
& item
) const;
481 // the array storing all item ids which have indirect data
482 wxArrayTreeItemIds m_itemsWithIndirectData
;
484 // the hash storing the items attributes (indexed by items ids)
487 // TRUE if the hash above is not empty
490 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)