1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/msw/treectrl.h
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"
25 #include "wx/treebase.h"
28 // Cygwin windows.h defines these identifiers
33 // the type for "untyped" data
34 typedef long wxDataType
;
37 class WXDLLEXPORT wxImageList
;
38 class WXDLLEXPORT wxDragImage
;
39 struct WXDLLEXPORT wxTreeViewItem
;
41 // a callback function used for sorting tree items, it should return -1 if the
42 // first item precedes the second, +1 if the second precedes the first or 0 if
46 // NB: all the following flags are for compatbility only and will be removed in the
49 // flags for deprecated `Expand(int action)'
53 wxTREE_EXPAND_COLLAPSE
,
54 wxTREE_EXPAND_COLLAPSE_RESET
,
58 // flags for deprecated InsertItem() variant
59 #define wxTREE_INSERT_FIRST 0xFFFF0001
60 #define wxTREE_INSERT_LAST 0xFFFF0002
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
65 class WXDLLEXPORT wxTreeCtrl
: public wxControl
70 wxTreeCtrl() { Init(); }
72 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
73 const wxPoint
& pos
= wxDefaultPosition
,
74 const wxSize
& size
= wxDefaultSize
,
75 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
76 const wxValidator
& validator
= wxDefaultValidator
,
77 const wxString
& name
= "wxTreeCtrl")
79 Create(parent
, id
, pos
, size
, style
, validator
, name
);
82 virtual ~wxTreeCtrl();
84 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
85 const wxPoint
& pos
= wxDefaultPosition
,
86 const wxSize
& size
= wxDefaultSize
,
87 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
88 const wxValidator
& validator
= wxDefaultValidator
,
89 const wxString
& name
= "wxTreeCtrl");
94 // get the total number of items in the control
95 size_t GetCount() const;
97 // indent is the number of pixels the children are indented relative to
98 // the parents position. SetIndent() also redraws the control
100 unsigned int GetIndent() const;
101 void SetIndent(unsigned int indent
);
103 // spacing is the number of pixels between the start and the Text
104 // not implemented under wxMSW
105 unsigned int GetSpacing() const { return 18; } // return wxGTK default
106 void SetSpacing(unsigned int WXUNUSED(spacing
)) { }
108 // image list: these functions allow to associate an image list with
109 // the control and retrieve it. Note that the control does _not_ delete
110 // the associated image list when it's deleted in order to allow image
111 // lists to be shared between different controls.
113 // The normal image list is for the icons which correspond to the
114 // normal tree item state (whether it is selected or not).
115 // Additionally, the application might choose to show a state icon
116 // which corresponds to an app-defined item state (for example,
117 // checked/unchecked) which are taken from the state image list.
118 wxImageList
*GetImageList() const;
119 wxImageList
*GetStateImageList() const;
121 void SetImageList(wxImageList
*imageList
);
122 void SetStateImageList(wxImageList
*imageList
);
124 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
125 // member functions of wxTreeItem because they must know the tree the item
126 // belongs to for Windows implementation and storing the pointer to
127 // wxTreeCtrl in each wxTreeItem is just too much waste.
132 // retrieve items label
133 wxString
GetItemText(const wxTreeItemId
& item
) const;
134 // get one of the images associated with the item (normal by default)
135 int GetItemImage(const wxTreeItemId
& item
,
136 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
137 // get the data associated with the item
138 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
144 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
145 // get one of the images associated with the item (normal by default)
146 void SetItemImage(const wxTreeItemId
& item
, int image
,
147 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
148 // associate some data with the item
149 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
151 // force appearance of [+] button near the item. This is useful to
152 // allow the user to expand the items which don't have any children now
153 // - but instead add them only when needed, thus minimizing memory
154 // usage and loading time.
155 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
157 // the item will be shown in bold
158 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
160 // the item will be shown with a drop highlight
161 void SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
= TRUE
);
163 // set the items text colour
164 void SetItemTextColour(const wxTreeItemId
& item
, const wxColour
& col
);
166 // set the items background colour
167 void SetItemBackgroundColour(const wxTreeItemId
& item
, const wxColour
& col
);
169 // set the items font (should be of the same height for all items)
170 void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
172 // item status inquiries
173 // ---------------------
175 // is the item visible (it might be outside the view or not expanded)?
176 bool IsVisible(const wxTreeItemId
& item
) const;
177 // does the item has any children?
178 bool ItemHasChildren(const wxTreeItemId
& item
) const;
179 // is the item expanded (only makes sense if HasChildren())?
180 bool IsExpanded(const wxTreeItemId
& item
) const;
181 // is this item currently selected (the same as has focus)?
182 bool IsSelected(const wxTreeItemId
& item
) const;
183 // is item text in bold font?
184 bool IsBold(const wxTreeItemId
& item
) const;
186 // number of children
187 // ------------------
189 // if 'recursively' is FALSE, only immediate children count, otherwise
190 // the returned number is the number of all items in this branch
191 size_t GetChildrenCount(const wxTreeItemId
& item
,
192 bool recursively
= TRUE
) const;
197 // wxTreeItemId.IsOk() will return FALSE if there is no such item
199 // get the root tree item
200 wxTreeItemId
GetRootItem() const;
202 // get the item currently selected (may return NULL if no selection)
203 wxTreeItemId
GetSelection() const;
205 // get the items currently selected, return the number of such item
207 // NB: this operation is expensive and can take a long time for a
208 // control with a lot of items (~ O(number of items)).
209 size_t GetSelections(wxArrayTreeItemIds
& selections
) const;
211 // get the parent of this item (may return NULL if root)
212 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
214 // for this enumeration function you must pass in a "cookie" parameter
215 // which is opaque for the application but is necessary for the library
216 // to make these functions reentrant (i.e. allow more than one
217 // enumeration on one and the same object simultaneously). Of course,
218 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
221 // get the first child of this item
222 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& _cookie
) const;
223 // get the next child
224 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& _cookie
) const;
225 // get the last child of this item - this method doesn't use cookies
226 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
228 // get the next sibling of this item
229 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
230 // get the previous sibling
231 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
233 // get first visible item
234 wxTreeItemId
GetFirstVisibleItem() const;
235 // get the next visible item: item must be visible itself!
236 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
237 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
238 // get the previous visible item: item must be visible itself!
239 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
244 // add the root node to the tree
245 wxTreeItemId
AddRoot(const wxString
& text
,
246 int image
= -1, int selectedImage
= -1,
247 wxTreeItemData
*data
= NULL
);
249 // insert a new item in as the first child of the parent
250 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
251 const wxString
& text
,
252 int image
= -1, int selectedImage
= -1,
253 wxTreeItemData
*data
= NULL
);
255 // insert a new item after a given one
256 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
257 const wxTreeItemId
& idPrevious
,
258 const wxString
& text
,
259 int image
= -1, int selectedImage
= -1,
260 wxTreeItemData
*data
= NULL
);
262 // insert a new item before the one with the given index
263 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
265 const wxString
& text
,
266 int image
= -1, int selectedImage
= -1,
267 wxTreeItemData
*data
= NULL
);
269 // insert a new item in as the last child of the parent
270 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
271 const wxString
& text
,
272 int image
= -1, int selectedImage
= -1,
273 wxTreeItemData
*data
= NULL
);
275 // delete this item and associated data if any
276 void Delete(const wxTreeItemId
& item
);
277 // delete all children (but don't delete the item itself)
278 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
279 void DeleteChildren(const wxTreeItemId
& item
);
280 // delete all items from the tree
281 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
282 void DeleteAllItems();
285 void Expand(const wxTreeItemId
& item
);
286 // collapse the item without removing its children
287 void Collapse(const wxTreeItemId
& item
);
288 // collapse the item and remove all children
289 void CollapseAndReset(const wxTreeItemId
& item
);
290 // toggles the current state
291 void Toggle(const wxTreeItemId
& item
);
293 // remove the selection from currently selected item (if any)
295 // unselect all items (only makes sense for multiple selection control)
298 void SelectItem(const wxTreeItemId
& item
);
299 // make sure this item is visible (expanding the parent item and/or
300 // scrolling to this item if necessary)
301 void EnsureVisible(const wxTreeItemId
& item
);
302 // scroll to this item (but don't expand its parent)
303 void ScrollTo(const wxTreeItemId
& item
);
305 // start editing the item label: this (temporarily) replaces the item
306 // with a one line edit control. The item will be selected if it hadn't
307 // been before. textCtrlClass parameter allows you to create an edit
308 // control of arbitrary user-defined class deriving from wxTextCtrl.
309 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
310 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
311 // returns the same pointer as StartEdit() if the item is being edited,
312 // NULL otherwise (it's assumed that no more than one item may be
313 // edited simultaneously)
314 wxTextCtrl
* GetEditControl() const;
315 // end editing and accept or discard the changes to item label
316 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
319 // this function is called to compare 2 items and should return -1, 0
320 // or +1 if the first item is less than, equal to or greater than the
321 // second one. The base class version performs alphabetic comparaison
322 // of item labels (GetText)
323 virtual int OnCompareItems(const wxTreeItemId
& item1
,
324 const wxTreeItemId
& item2
);
325 // sort the children of this item using OnCompareItems
327 // NB: this function is not reentrant and not MT-safe (FIXME)!
328 void SortChildren(const wxTreeItemId
& item
);
333 // determine to which item (if any) belongs the given point (the
334 // coordinates specified are relative to the client area of tree ctrl)
335 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
338 // The first function is more portable (because easier to implement
339 // on other platforms), but the second one returns some extra info.
340 wxTreeItemId
HitTest(const wxPoint
& point
)
341 { int dummy
; return HitTest(point
, dummy
); }
342 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
344 // get the bounding rectangle of the item (or of its label only)
345 bool GetBoundingRect(const wxTreeItemId
& item
,
347 bool textOnly
= FALSE
) const;
352 // these methods are deprecated and will be removed in future versions of
353 // wxWindows, they're here for compatibility only, don't use them in new
354 // code (the comments indicate why these methods are now useless and how to
357 // use Expand, Collapse, CollapseAndReset or Toggle
358 void ExpandItem(const wxTreeItemId
& item
, int action
);
360 // use AddRoot, PrependItem or AppendItem
361 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
362 const wxString
& text
,
363 int image
= -1, int selImage
= -1,
364 long insertAfter
= wxTREE_INSERT_LAST
);
366 // use Set/GetImageList and Set/GetStateImageList
367 wxImageList
*GetImageList(int) const
368 { return GetImageList(); }
369 void SetImageList(wxImageList
*imageList
, int)
370 { SetImageList(imageList
); }
372 // use Set/GetItemImage directly
373 // get the selected item image
374 int GetItemSelectedImage(const wxTreeItemId
& item
) const
375 { return GetItemImage(item
, wxTreeItemIcon_Selected
); }
376 // set the selected item image
377 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
378 { SetItemImage(item
, image
, wxTreeItemIcon_Selected
); }
383 virtual long MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
384 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
385 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
387 // override some base class virtuals
388 virtual bool SetBackgroundColour(const wxColour
&colour
);
389 virtual bool SetForegroundColour(const wxColour
&colour
);
391 // get/set the check state for the item (only for wxTR_MULTIPLE)
392 bool IsItemChecked(const wxTreeItemId
& item
) const;
393 void SetItemCheck(const wxTreeItemId
& item
, bool check
= TRUE
);
396 // SetImageList helper
397 void SetAnyImageList(wxImageList
*imageList
, int which
);
399 wxTextCtrl
*m_textCtrl
; // used while editing the item label
400 wxImageList
*m_imageListNormal
, // images for tree elements
401 *m_imageListState
; // special images for app defined states
404 // the common part of all ctors
408 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
409 inline void DoSetItem(wxTreeViewItem
*tvItem
);
411 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
413 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
414 wxTreeItemId hInsertAfter
,
415 const wxString
& text
,
416 int image
, int selectedImage
,
417 wxTreeItemData
*data
);
419 int DoGetItemImageFromData(const wxTreeItemId
& item
,
420 wxTreeItemIcon which
) const;
421 void DoSetItemImageFromData(const wxTreeItemId
& item
,
423 wxTreeItemIcon which
) const;
424 void DoSetItemImages(const wxTreeItemId
& item
, int image
, int imageSel
);
426 void DeleteTextCtrl();
428 // support for additional item images
429 friend class wxTreeItemIndirectData
;
430 void SetIndirectItemData(const wxTreeItemId
& item
,
431 wxTreeItemIndirectData
*data
);
432 bool HasIndirectData(const wxTreeItemId
& item
) const;
434 // the array storing all item ids which have indirect data
435 wxArrayTreeItemIds m_itemsWithIndirectData
;
437 // the hash storing the items attributes (indexed by items ids)
440 // TRUE if the hash above is not empty
444 wxDragImage
*m_dragImage
;
446 // the starting item for selection with Shift
447 WXHTREEITEM m_htSelStart
;
449 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)