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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TREECTRL_H_
13 #define _WX_TREECTRL_H_
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
21 #include "wx/textctrl.h"
22 #include "wx/dynarray.h"
23 #include "wx/treebase.h"
24 #include "wx/hashmap.h"
27 // Cygwin windows.h defines these identifiers
33 class WXDLLEXPORT wxImageList
;
34 class WXDLLEXPORT wxDragImage
;
35 struct WXDLLEXPORT wxTreeViewItem
;
37 // NB: all the following flags are for compatbility only and will be removed in the
40 // flags for deprecated InsertItem() variant (their values are the same as of
41 // TVI_FIRST and TVI_LAST)
42 #define wxTREE_INSERT_FIRST 0xFFFF0001
43 #define wxTREE_INSERT_LAST 0xFFFF0002
45 // hash storing attributes for our items
46 WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr
*, wxMapTreeAttr
);
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 class WXDLLEXPORT wxTreeCtrl
: public wxControl
57 wxTreeCtrl() { Init(); }
59 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
60 const wxPoint
& pos
= wxDefaultPosition
,
61 const wxSize
& size
= wxDefaultSize
,
62 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
63 const wxValidator
& validator
= wxDefaultValidator
,
64 const wxString
& name
= wxTreeCtrlNameStr
)
66 Create(parent
, id
, pos
, size
, style
, validator
, name
);
69 virtual ~wxTreeCtrl();
71 bool Create(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
72 const wxPoint
& pos
= wxDefaultPosition
,
73 const wxSize
& size
= wxDefaultSize
,
74 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
75 const wxValidator
& validator
= wxDefaultValidator
,
76 const wxString
& name
= wxTreeCtrlNameStr
);
81 // get the total number of items in the control
82 size_t GetCount() const;
84 // indent is the number of pixels the children are indented relative to
85 // the parents position. SetIndent() also redraws the control
87 unsigned int GetIndent() const;
88 void SetIndent(unsigned int indent
);
90 // spacing is the number of pixels between the start and the Text
91 // not implemented under wxMSW
92 unsigned int GetSpacing() const { return 18; } // return wxGTK default
93 void SetSpacing(unsigned int WXUNUSED(spacing
)) { }
95 // image list: these functions allow to associate an image list with
96 // the control and retrieve it. Note that the control does _not_ delete
97 // the associated image list when it's deleted in order to allow image
98 // lists to be shared between different controls.
100 // The normal image list is for the icons which correspond to the
101 // normal tree item state (whether it is selected or not).
102 // Additionally, the application might choose to show a state icon
103 // which corresponds to an app-defined item state (for example,
104 // checked/unchecked) which are taken from the state image list.
105 virtual wxImageList
*GetImageList() const;
106 virtual wxImageList
*GetStateImageList() const;
108 virtual void SetImageList(wxImageList
*imageList
);
109 virtual void SetStateImageList(wxImageList
*imageList
);
110 virtual void AssignImageList(wxImageList
*imageList
);
111 virtual void AssignStateImageList(wxImageList
*imageList
);
113 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
114 // member functions of wxTreeItem because they must know the tree the item
115 // belongs to for Windows implementation and storing the pointer to
116 // wxTreeCtrl in each wxTreeItem is just too much waste.
121 // retrieve items label
122 wxString
GetItemText(const wxTreeItemId
& item
) const;
123 // get one of the images associated with the item (normal by default)
124 virtual int GetItemImage(const wxTreeItemId
& item
,
125 wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const;
126 // get the data associated with the item
127 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
129 // get the item's text colour
130 wxColour
GetItemTextColour(const wxTreeItemId
& item
) const;
132 // get the item's background colour
133 wxColour
GetItemBackgroundColour(const wxTreeItemId
& item
) const;
135 // get the item's font
136 wxFont
GetItemFont(const wxTreeItemId
& item
) const;
142 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
143 // get one of the images associated with the item (normal by default)
144 virtual void SetItemImage(const wxTreeItemId
& item
, int image
,
145 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
146 // associate some data with the item
147 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
149 // force appearance of [+] button near the item. This is useful to
150 // allow the user to expand the items which don't have any children now
151 // - but instead add them only when needed, thus minimizing memory
152 // usage and loading time.
153 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= true);
155 // the item will be shown in bold
156 void SetItemBold(const wxTreeItemId
& item
, bool bold
= true);
158 // the item will be shown with a drop highlight
159 void SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
= true);
161 // set the items text colour
162 void SetItemTextColour(const wxTreeItemId
& item
, const wxColour
& col
);
164 // set the items background colour
165 void SetItemBackgroundColour(const wxTreeItemId
& item
, const wxColour
& col
);
167 // set the items font (should be of the same height for all items)
168 void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
170 // item status inquiries
171 // ---------------------
173 // is the item visible (it might be outside the view or not expanded)?
174 bool IsVisible(const wxTreeItemId
& item
) const;
175 // does the item has any children?
176 bool ItemHasChildren(const wxTreeItemId
& item
) const;
177 // is the item expanded (only makes sense if HasChildren())?
178 bool IsExpanded(const wxTreeItemId
& item
) const;
179 // is this item currently selected (the same as has focus)?
180 bool IsSelected(const wxTreeItemId
& item
) const;
181 // is item text in bold font?
182 bool IsBold(const wxTreeItemId
& item
) const;
184 // number of children
185 // ------------------
187 // if 'recursively' is false, only immediate children count, otherwise
188 // the returned number is the number of all items in this branch
189 size_t GetChildrenCount(const wxTreeItemId
& item
,
190 bool recursively
= true) const;
195 // wxTreeItemId.IsOk() will return false if there is no such item
197 // get the root tree item
198 wxTreeItemId
GetRootItem() const;
200 // get the item currently selected (may return NULL if no selection)
201 wxTreeItemId
GetSelection() const;
203 // get the items currently selected, return the number of such item
205 // NB: this operation is expensive and can take a long time for a
206 // control with a lot of items (~ O(number of items)).
207 size_t GetSelections(wxArrayTreeItemIds
& selections
) const;
209 // get the parent of this item (may return NULL if root)
210 wxTreeItemId
GetItemParent(const wxTreeItemId
& item
) const;
212 #if WXWIN_COMPATIBILITY_2_2
213 // deprecated: Use GetItemParent instead.
214 wxDEPRECATED( wxTreeItemId
GetParent(const wxTreeItemId
& item
) const);
216 // Expose the base class method hidden by the one above. Not deprecatable.
217 wxWindow
*GetParent() const { return wxControl::GetParent(); }
218 #endif // WXWIN_COMPATIBILITY_2_2
220 // for this enumeration function you must pass in a "cookie" parameter
221 // which is opaque for the application but is necessary for the library
222 // to make these functions reentrant (i.e. allow more than one
223 // enumeration on one and the same object simultaneously). Of course,
224 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
227 // get the first child of this item
228 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
229 wxTreeItemIdValue
& cookie
) const;
230 // get the next child
231 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
232 wxTreeItemIdValue
& cookie
) const;
233 // get the last child of this item - this method doesn't use cookies
234 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
236 // get the next sibling of this item
237 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
238 // get the previous sibling
239 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
241 // get first visible item
242 wxTreeItemId
GetFirstVisibleItem() const;
243 // get the next visible item: item must be visible itself!
244 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
245 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
246 // get the previous visible item: item must be visible itself!
247 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
252 // add the root node to the tree
253 virtual wxTreeItemId
AddRoot(const wxString
& text
,
254 int image
= -1, int selectedImage
= -1,
255 wxTreeItemData
*data
= NULL
);
257 // insert a new item in as the first child of the parent
258 virtual wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
259 const wxString
& text
,
260 int image
= -1, int selectedImage
= -1,
261 wxTreeItemData
*data
= NULL
);
263 // insert a new item after a given one
264 virtual wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
265 const wxTreeItemId
& idPrevious
,
266 const wxString
& text
,
267 int image
= -1, int selectedImage
= -1,
268 wxTreeItemData
*data
= NULL
);
270 // insert a new item before the one with the given index
271 virtual wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
273 const wxString
& text
,
274 int image
= -1, int selectedImage
= -1,
275 wxTreeItemData
*data
= NULL
);
277 // insert a new item in as the last child of the parent
278 virtual wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
279 const wxString
& text
,
280 int image
= -1, int selectedImage
= -1,
281 wxTreeItemData
*data
= NULL
);
283 // delete this item and associated data if any
284 void Delete(const wxTreeItemId
& item
);
285 // delete all children (but don't delete the item itself)
286 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
287 void DeleteChildren(const wxTreeItemId
& item
);
288 // delete all items from the tree
289 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
290 void DeleteAllItems();
293 void Expand(const wxTreeItemId
& item
);
294 // collapse the item without removing its children
295 void Collapse(const wxTreeItemId
& item
);
296 // collapse the item and remove all children
297 void CollapseAndReset(const wxTreeItemId
& item
);
298 // toggles the current state
299 void Toggle(const wxTreeItemId
& item
);
301 // remove the selection from currently selected item (if any)
303 // unselect all items (only makes sense for multiple selection control)
306 void SelectItem(const wxTreeItemId
& item
, bool select
= true);
307 // unselect this item
308 void UnselectItem(const wxTreeItemId
& item
);
309 // toggle item selection
310 void ToggleItemSelection(const wxTreeItemId
& item
);
312 // make sure this item is visible (expanding the parent item and/or
313 // scrolling to this item if necessary)
314 void EnsureVisible(const wxTreeItemId
& item
);
315 // scroll to this item (but don't expand its parent)
316 void ScrollTo(const wxTreeItemId
& item
);
318 // start editing the item label: this (temporarily) replaces the item
319 // with a one line edit control. The item will be selected if it hadn't
320 // been before. textCtrlClass parameter allows you to create an edit
321 // control of arbitrary user-defined class deriving from wxTextCtrl.
322 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
323 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
324 // returns the same pointer as StartEdit() if the item is being edited,
325 // NULL otherwise (it's assumed that no more than one item may be
326 // edited simultaneously)
327 wxTextCtrl
* GetEditControl() const;
328 // end editing and accept or discard the changes to item label
329 void EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
330 bool discardChanges
= false)
332 DoEndEditLabel(discardChanges
);
336 // this function is called to compare 2 items and should return -1, 0
337 // or +1 if the first item is less than, equal to or greater than the
338 // second one. The base class version performs alphabetic comparaison
339 // of item labels (GetText)
340 virtual int OnCompareItems(const wxTreeItemId
& item1
,
341 const wxTreeItemId
& item2
);
342 // sort the children of this item using OnCompareItems
344 // NB: this function is not reentrant and not MT-safe (FIXME)!
345 void SortChildren(const wxTreeItemId
& item
);
350 // determine to which item (if any) belongs the given point (the
351 // coordinates specified are relative to the client area of tree ctrl)
352 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
355 // The first function is more portable (because easier to implement
356 // on other platforms), but the second one returns some extra info.
357 wxTreeItemId
HitTest(const wxPoint
& point
)
358 { int dummy
; return HitTest(point
, dummy
); }
359 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
361 // get the bounding rectangle of the item (or of its label only)
362 bool GetBoundingRect(const wxTreeItemId
& item
,
364 bool textOnly
= false) const;
369 #if WXWIN_COMPATIBILITY_2_4
370 // these methods are deprecated and will be removed in future versions of
371 // wxWidgets, they're here for compatibility only, don't use them in new
372 // code (the comments indicate why these methods are now useless and how to
375 // use Expand, Collapse, CollapseAndReset or Toggle
376 wxDEPRECATED( void ExpandItem(const wxTreeItemId
& item
, int action
) );
378 // use AddRoot, PrependItem or AppendItem
379 wxDEPRECATED( wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
380 const wxString
& text
,
381 int image
= -1, int selImage
= -1,
382 long insertAfter
= wxTREE_INSERT_LAST
) );
384 // use Set/GetImageList and Set/GetStateImageList
385 wxDEPRECATED( wxImageList
*GetImageList(int) const );
386 wxDEPRECATED( void SetImageList(wxImageList
*imageList
, int) );
388 // use Set/GetItemImage directly
389 wxDEPRECATED( int GetItemSelectedImage(const wxTreeItemId
& item
) const );
390 wxDEPRECATED( void SetItemSelectedImage(const wxTreeItemId
& item
, int image
) );
392 // use the versions taking wxTreeItemIdValue cookies
393 wxDEPRECATED( wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
394 long& cookie
) const );
395 wxDEPRECATED( wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
396 long& cookie
) const );
397 #endif // WXWIN_COMPATIBILITY_2_4
403 virtual bool ShouldInheritColours() const { return false; }
405 virtual wxVisualAttributes
GetDefaultAttributes() const
407 return GetClassDefaultAttributes(GetWindowVariant());
410 static wxVisualAttributes
411 GetClassDefaultAttributes(wxWindowVariant variant
= wxWINDOW_VARIANT_NORMAL
);
414 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
415 virtual WXLRESULT
MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
416 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
417 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
419 // override some base class virtuals
420 virtual bool SetBackgroundColour(const wxColour
&colour
);
421 virtual bool SetForegroundColour(const wxColour
&colour
);
423 // get/set the check state for the item (only for wxTR_MULTIPLE)
424 bool IsItemChecked(const wxTreeItemId
& item
) const;
425 void SetItemCheck(const wxTreeItemId
& item
, bool check
= true);
427 // set/get the item state.image (state == -1 means cycle to the next one)
428 void SetState(const wxTreeItemId
& node
, int state
);
429 int GetState(const wxTreeItemId
& node
);
432 // SetImageList helper
433 void SetAnyImageList(wxImageList
*imageList
, int which
);
435 // refresh a single item
436 void RefreshItem(const wxTreeItemId
& item
);
439 void DoEndEditLabel(bool discardChanges
= false);
442 // data used only while editing the item label:
443 wxTextCtrl
*m_textCtrl
; // text control in which it is edited
444 wxTreeItemId m_idEdited
; // the item being edited
446 wxImageList
*m_imageListNormal
, // images for tree elements
447 *m_imageListState
; // special images for app defined states
448 bool m_ownsImageListNormal
,
449 m_ownsImageListState
;
452 // the common part of all ctors
456 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
457 inline void DoSetItem(wxTreeViewItem
*tvItem
);
459 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
461 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
462 wxTreeItemId hInsertAfter
,
463 const wxString
& text
,
464 int image
, int selectedImage
,
465 wxTreeItemData
*data
);
467 int DoGetItemImageFromData(const wxTreeItemId
& item
,
468 wxTreeItemIcon which
) const;
469 void DoSetItemImageFromData(const wxTreeItemId
& item
,
471 wxTreeItemIcon which
) const;
472 void DoSetItemImages(const wxTreeItemId
& item
, int image
, int imageSel
);
474 void DeleteTextCtrl();
476 // support for additional item images which we implement using
477 // wxTreeItemIndirectData technique - see the comments in msw/treectrl.cpp
478 void SetIndirectItemData(const wxTreeItemId
& item
,
479 class wxTreeItemIndirectData
*data
);
480 bool HasIndirectData(const wxTreeItemId
& item
) const;
481 bool IsDataIndirect(wxTreeItemData
*data
) const
482 { return data
&& data
->GetId().m_pItem
== 0; }
484 // the hash storing the items attributes (indexed by item ids)
485 wxMapTreeAttr m_attrs
;
487 // true if the hash above is not empty
491 wxDragImage
*m_dragImage
;
493 // Virtual root item, if wxTR_HIDE_ROOT is set.
494 void* m_pVirtualRoot
;
496 // the starting item for selection with Shift
497 wxTreeItemId m_htSelStart
, m_htClickedItem
;
500 friend class wxTreeItemIndirectData
;
501 friend class wxTreeSortHelper
;
503 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
504 DECLARE_NO_COPY_CLASS(wxTreeCtrl
)
507 #endif // wxUSE_TREECTRL