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
);
123 void AssignImageList(wxImageList
*imageList
);
124 void AssignStateImageList(wxImageList
*imageList
);
126 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
127 // member functions of wxTreeItem because they must know the tree the item
128 // belongs to for Windows implementation and storing the pointer to
129 // wxTreeCtrl in each wxTreeItem is just too much waste.
134 // retrieve items label
135 wxString
GetItemText(const wxTreeItemId
& item
) const;
136 // get one of the images associated with the item (normal by default)
137 int GetItemImage(const wxTreeItemId
& item
,
138 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
139 // get the data associated with the item
140 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
146 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
147 // get one of the images associated with the item (normal by default)
148 void SetItemImage(const wxTreeItemId
& item
, int image
,
149 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
150 // associate some data with the item
151 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
153 // force appearance of [+] button near the item. This is useful to
154 // allow the user to expand the items which don't have any children now
155 // - but instead add them only when needed, thus minimizing memory
156 // usage and loading time.
157 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
159 // the item will be shown in bold
160 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
162 // the item will be shown with a drop highlight
163 void SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
= TRUE
);
165 // set the items text colour
166 void SetItemTextColour(const wxTreeItemId
& item
, const wxColour
& col
);
168 // set the items background colour
169 void SetItemBackgroundColour(const wxTreeItemId
& item
, const wxColour
& col
);
171 // set the items font (should be of the same height for all items)
172 void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
174 // item status inquiries
175 // ---------------------
177 // is the item visible (it might be outside the view or not expanded)?
178 bool IsVisible(const wxTreeItemId
& item
) const;
179 // does the item has any children?
180 bool ItemHasChildren(const wxTreeItemId
& item
) const;
181 // is the item expanded (only makes sense if HasChildren())?
182 bool IsExpanded(const wxTreeItemId
& item
) const;
183 // is this item currently selected (the same as has focus)?
184 bool IsSelected(const wxTreeItemId
& item
) const;
185 // is item text in bold font?
186 bool IsBold(const wxTreeItemId
& item
) const;
188 // number of children
189 // ------------------
191 // if 'recursively' is FALSE, only immediate children count, otherwise
192 // the returned number is the number of all items in this branch
193 size_t GetChildrenCount(const wxTreeItemId
& item
,
194 bool recursively
= TRUE
) const;
199 // wxTreeItemId.IsOk() will return FALSE if there is no such item
201 // get the root tree item
202 wxTreeItemId
GetRootItem() const;
204 // get the item currently selected (may return NULL if no selection)
205 wxTreeItemId
GetSelection() const;
207 // get the items currently selected, return the number of such item
209 // NB: this operation is expensive and can take a long time for a
210 // control with a lot of items (~ O(number of items)).
211 size_t GetSelections(wxArrayTreeItemIds
& selections
) const;
213 // get the parent of this item (may return NULL if root)
214 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
216 // for this enumeration function you must pass in a "cookie" parameter
217 // which is opaque for the application but is necessary for the library
218 // to make these functions reentrant (i.e. allow more than one
219 // enumeration on one and the same object simultaneously). Of course,
220 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
223 // get the first child of this item
224 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& _cookie
) const;
225 // get the next child
226 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& _cookie
) const;
227 // get the last child of this item - this method doesn't use cookies
228 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
230 // get the next sibling of this item
231 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
232 // get the previous sibling
233 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
235 // get first visible item
236 wxTreeItemId
GetFirstVisibleItem() const;
237 // get the next visible item: item must be visible itself!
238 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
239 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
240 // get the previous visible item: item must be visible itself!
241 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
246 // add the root node to the tree
247 wxTreeItemId
AddRoot(const wxString
& text
,
248 int image
= -1, int selectedImage
= -1,
249 wxTreeItemData
*data
= NULL
);
251 // insert a new item in as the first child of the parent
252 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
253 const wxString
& text
,
254 int image
= -1, int selectedImage
= -1,
255 wxTreeItemData
*data
= NULL
);
257 // insert a new item after a given one
258 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
259 const wxTreeItemId
& idPrevious
,
260 const wxString
& text
,
261 int image
= -1, int selectedImage
= -1,
262 wxTreeItemData
*data
= NULL
);
264 // insert a new item before the one with the given index
265 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
267 const wxString
& text
,
268 int image
= -1, int selectedImage
= -1,
269 wxTreeItemData
*data
= NULL
);
271 // insert a new item in as the last child of the parent
272 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
273 const wxString
& text
,
274 int image
= -1, int selectedImage
= -1,
275 wxTreeItemData
*data
= NULL
);
277 // delete this item and associated data if any
278 void Delete(const wxTreeItemId
& item
);
279 // delete all children (but don't delete the item itself)
280 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
281 void DeleteChildren(const wxTreeItemId
& item
);
282 // delete all items from the tree
283 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
284 void DeleteAllItems();
287 void Expand(const wxTreeItemId
& item
);
288 // collapse the item without removing its children
289 void Collapse(const wxTreeItemId
& item
);
290 // collapse the item and remove all children
291 void CollapseAndReset(const wxTreeItemId
& item
);
292 // toggles the current state
293 void Toggle(const wxTreeItemId
& item
);
295 // remove the selection from currently selected item (if any)
297 // unselect all items (only makes sense for multiple selection control)
300 void SelectItem(const wxTreeItemId
& item
);
301 // make sure this item is visible (expanding the parent item and/or
302 // scrolling to this item if necessary)
303 void EnsureVisible(const wxTreeItemId
& item
);
304 // scroll to this item (but don't expand its parent)
305 void ScrollTo(const wxTreeItemId
& item
);
307 // start editing the item label: this (temporarily) replaces the item
308 // with a one line edit control. The item will be selected if it hadn't
309 // been before. textCtrlClass parameter allows you to create an edit
310 // control of arbitrary user-defined class deriving from wxTextCtrl.
311 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
312 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
313 // returns the same pointer as StartEdit() if the item is being edited,
314 // NULL otherwise (it's assumed that no more than one item may be
315 // edited simultaneously)
316 wxTextCtrl
* GetEditControl() const;
317 // end editing and accept or discard the changes to item label
318 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
321 // this function is called to compare 2 items and should return -1, 0
322 // or +1 if the first item is less than, equal to or greater than the
323 // second one. The base class version performs alphabetic comparaison
324 // of item labels (GetText)
325 virtual int OnCompareItems(const wxTreeItemId
& item1
,
326 const wxTreeItemId
& item2
);
327 // sort the children of this item using OnCompareItems
329 // NB: this function is not reentrant and not MT-safe (FIXME)!
330 void SortChildren(const wxTreeItemId
& item
);
335 // determine to which item (if any) belongs the given point (the
336 // coordinates specified are relative to the client area of tree ctrl)
337 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
340 // The first function is more portable (because easier to implement
341 // on other platforms), but the second one returns some extra info.
342 wxTreeItemId
HitTest(const wxPoint
& point
)
343 { int dummy
; return HitTest(point
, dummy
); }
344 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
346 // get the bounding rectangle of the item (or of its label only)
347 bool GetBoundingRect(const wxTreeItemId
& item
,
349 bool textOnly
= FALSE
) const;
354 // these methods are deprecated and will be removed in future versions of
355 // wxWindows, they're here for compatibility only, don't use them in new
356 // code (the comments indicate why these methods are now useless and how to
359 // use Expand, Collapse, CollapseAndReset or Toggle
360 void ExpandItem(const wxTreeItemId
& item
, int action
);
362 // use AddRoot, PrependItem or AppendItem
363 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
364 const wxString
& text
,
365 int image
= -1, int selImage
= -1,
366 long insertAfter
= wxTREE_INSERT_LAST
);
368 // use Set/GetImageList and Set/GetStateImageList
369 wxImageList
*GetImageList(int) const
370 { return GetImageList(); }
371 void SetImageList(wxImageList
*imageList
, int)
372 { SetImageList(imageList
); }
374 // use Set/GetItemImage directly
375 // get the selected item image
376 int GetItemSelectedImage(const wxTreeItemId
& item
) const
377 { return GetItemImage(item
, wxTreeItemIcon_Selected
); }
378 // set the selected item image
379 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
380 { SetItemImage(item
, image
, wxTreeItemIcon_Selected
); }
385 virtual long MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
386 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
387 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
389 // override some base class virtuals
390 virtual bool SetBackgroundColour(const wxColour
&colour
);
391 virtual bool SetForegroundColour(const wxColour
&colour
);
393 // get/set the check state for the item (only for wxTR_MULTIPLE)
394 bool IsItemChecked(const wxTreeItemId
& item
) const;
395 void SetItemCheck(const wxTreeItemId
& item
, bool check
= TRUE
);
398 // SetImageList helper
399 void SetAnyImageList(wxImageList
*imageList
, int which
);
401 wxTextCtrl
*m_textCtrl
; // used while editing the item label
402 wxImageList
*m_imageListNormal
, // images for tree elements
403 *m_imageListState
; // special images for app defined states
404 bool m_ownsImageListNormal
, m_ownsImageListState
;
407 // the common part of all ctors
411 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
412 inline void DoSetItem(wxTreeViewItem
*tvItem
);
414 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
416 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
417 wxTreeItemId hInsertAfter
,
418 const wxString
& text
,
419 int image
, int selectedImage
,
420 wxTreeItemData
*data
);
422 int DoGetItemImageFromData(const wxTreeItemId
& item
,
423 wxTreeItemIcon which
) const;
424 void DoSetItemImageFromData(const wxTreeItemId
& item
,
426 wxTreeItemIcon which
) const;
427 void DoSetItemImages(const wxTreeItemId
& item
, int image
, int imageSel
);
429 void DeleteTextCtrl();
431 // support for additional item images
432 friend class wxTreeItemIndirectData
;
433 void SetIndirectItemData(const wxTreeItemId
& item
,
434 wxTreeItemIndirectData
*data
);
435 bool HasIndirectData(const wxTreeItemId
& item
) const;
437 // the array storing all item ids which have indirect data
438 wxArrayTreeItemIds m_itemsWithIndirectData
;
440 // the hash storing the items attributes (indexed by items ids)
443 // TRUE if the hash above is not empty
447 wxDragImage
*m_dragImage
;
449 // the starting item for selection with Shift
450 WXHTREEITEM m_htSelStart
;
452 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)