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 // ----------------------------------------------------------------------------
19 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20 #pragma interface "treectrl.h"
25 #include "wx/textctrl.h"
26 #include "wx/dynarray.h"
27 #include "wx/treebase.h"
28 #include "wx/hashmap.h"
31 // Cygwin windows.h defines these identifiers
37 class WXDLLEXPORT wxImageList
;
38 class WXDLLEXPORT wxDragImage
;
39 struct WXDLLEXPORT wxTreeViewItem
;
41 // NB: all the following flags are for compatbility only and will be removed in the
44 // flags for deprecated `Expand(int action)'
48 wxTREE_EXPAND_COLLAPSE
,
49 wxTREE_EXPAND_COLLAPSE_RESET
,
53 // flags for deprecated InsertItem() variant (their values are the same as of
54 // TVI_FIRST and TVI_LAST)
55 #define wxTREE_INSERT_FIRST 0xFFFF0001
56 #define wxTREE_INSERT_LAST 0xFFFF0002
58 // hash storing attributes for our items
59 WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr
*, wxMapTreeAttr
);
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
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
= wxTreeCtrlNameStr
)
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
= wxTreeCtrlNameStr
);
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;
142 // get the item's text colour
143 wxColour
GetItemTextColour(const wxTreeItemId
& item
) const;
145 // get the item's background colour
146 wxColour
GetItemBackgroundColour(const wxTreeItemId
& item
) const;
148 // get the item's font
149 wxFont
GetItemFont(const wxTreeItemId
& item
) const;
155 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
156 // get one of the images associated with the item (normal by default)
157 void SetItemImage(const wxTreeItemId
& item
, int image
,
158 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
159 // associate some data with the item
160 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
162 // force appearance of [+] button near the item. This is useful to
163 // allow the user to expand the items which don't have any children now
164 // - but instead add them only when needed, thus minimizing memory
165 // usage and loading time.
166 void SetItemHasChildren(const wxTreeItemId
& item
, bool has
= TRUE
);
168 // the item will be shown in bold
169 void SetItemBold(const wxTreeItemId
& item
, bool bold
= TRUE
);
171 // the item will be shown with a drop highlight
172 void SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
= TRUE
);
174 // set the items text colour
175 void SetItemTextColour(const wxTreeItemId
& item
, const wxColour
& col
);
177 // set the items background colour
178 void SetItemBackgroundColour(const wxTreeItemId
& item
, const wxColour
& col
);
180 // set the items font (should be of the same height for all items)
181 void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
183 // item status inquiries
184 // ---------------------
186 // is the item visible (it might be outside the view or not expanded)?
187 bool IsVisible(const wxTreeItemId
& item
) const;
188 // does the item has any children?
189 bool ItemHasChildren(const wxTreeItemId
& item
) const;
190 // is the item expanded (only makes sense if HasChildren())?
191 bool IsExpanded(const wxTreeItemId
& item
) const;
192 // is this item currently selected (the same as has focus)?
193 bool IsSelected(const wxTreeItemId
& item
) const;
194 // is item text in bold font?
195 bool IsBold(const wxTreeItemId
& item
) const;
197 // number of children
198 // ------------------
200 // if 'recursively' is FALSE, only immediate children count, otherwise
201 // the returned number is the number of all items in this branch
202 size_t GetChildrenCount(const wxTreeItemId
& item
,
203 bool recursively
= TRUE
) const;
208 // wxTreeItemId.IsOk() will return FALSE if there is no such item
210 // get the root tree item
211 wxTreeItemId
GetRootItem() const;
213 // get the item currently selected (may return NULL if no selection)
214 wxTreeItemId
GetSelection() const;
216 // get the items currently selected, return the number of such item
218 // NB: this operation is expensive and can take a long time for a
219 // control with a lot of items (~ O(number of items)).
220 size_t GetSelections(wxArrayTreeItemIds
& selections
) const;
222 // get the parent of this item (may return NULL if root)
223 wxTreeItemId
GetItemParent(const wxTreeItemId
& item
) const;
225 #if WXWIN_COMPATIBILITY_2_2
226 // deprecated: Use GetItemParent instead.
227 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const
228 { return GetItemParent( item
); }
230 // Expose the base class method hidden by the one above.
231 wxWindow
*GetParent() const { return wxControl::GetParent(); }
232 #endif // WXWIN_COMPATIBILITY_2_2
234 // for this enumeration function you must pass in a "cookie" parameter
235 // which is opaque for the application but is necessary for the library
236 // to make these functions reentrant (i.e. allow more than one
237 // enumeration on one and the same object simultaneously). Of course,
238 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
241 // get the first child of this item
242 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
243 wxTreeItemIdValue
& cookie
) const;
244 // get the next child
245 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
246 wxTreeItemIdValue
& cookie
) const;
247 // get the last child of this item - this method doesn't use cookies
248 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
250 // get the next sibling of this item
251 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
252 // get the previous sibling
253 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
255 // get first visible item
256 wxTreeItemId
GetFirstVisibleItem() const;
257 // get the next visible item: item must be visible itself!
258 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
259 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
260 // get the previous visible item: item must be visible itself!
261 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
266 // add the root node to the tree
267 wxTreeItemId
AddRoot(const wxString
& text
,
268 int image
= -1, int selectedImage
= -1,
269 wxTreeItemData
*data
= NULL
);
271 // insert a new item in as the first child of the parent
272 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
273 const wxString
& text
,
274 int image
= -1, int selectedImage
= -1,
275 wxTreeItemData
*data
= NULL
);
277 // insert a new item after a given one
278 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
279 const wxTreeItemId
& idPrevious
,
280 const wxString
& text
,
281 int image
= -1, int selectedImage
= -1,
282 wxTreeItemData
*data
= NULL
);
284 // insert a new item before the one with the given index
285 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
287 const wxString
& text
,
288 int image
= -1, int selectedImage
= -1,
289 wxTreeItemData
*data
= NULL
);
291 // insert a new item in as the last child of the parent
292 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
293 const wxString
& text
,
294 int image
= -1, int selectedImage
= -1,
295 wxTreeItemData
*data
= NULL
);
297 // delete this item and associated data if any
298 void Delete(const wxTreeItemId
& item
);
299 // delete all children (but don't delete the item itself)
300 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
301 void DeleteChildren(const wxTreeItemId
& item
);
302 // delete all items from the tree
303 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
304 void DeleteAllItems();
307 void Expand(const wxTreeItemId
& item
);
308 // collapse the item without removing its children
309 void Collapse(const wxTreeItemId
& item
);
310 // collapse the item and remove all children
311 void CollapseAndReset(const wxTreeItemId
& item
);
312 // toggles the current state
313 void Toggle(const wxTreeItemId
& item
);
315 // remove the selection from currently selected item (if any)
317 // unselect all items (only makes sense for multiple selection control)
320 void SelectItem(const wxTreeItemId
& item
);
321 // make sure this item is visible (expanding the parent item and/or
322 // scrolling to this item if necessary)
323 void EnsureVisible(const wxTreeItemId
& item
);
324 // scroll to this item (but don't expand its parent)
325 void ScrollTo(const wxTreeItemId
& item
);
327 // start editing the item label: this (temporarily) replaces the item
328 // with a one line edit control. The item will be selected if it hadn't
329 // been before. textCtrlClass parameter allows you to create an edit
330 // control of arbitrary user-defined class deriving from wxTextCtrl.
331 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
332 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
333 // returns the same pointer as StartEdit() if the item is being edited,
334 // NULL otherwise (it's assumed that no more than one item may be
335 // edited simultaneously)
336 wxTextCtrl
* GetEditControl() const;
337 // end editing and accept or discard the changes to item label
338 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
341 // this function is called to compare 2 items and should return -1, 0
342 // or +1 if the first item is less than, equal to or greater than the
343 // second one. The base class version performs alphabetic comparaison
344 // of item labels (GetText)
345 virtual int OnCompareItems(const wxTreeItemId
& item1
,
346 const wxTreeItemId
& item2
);
347 // sort the children of this item using OnCompareItems
349 // NB: this function is not reentrant and not MT-safe (FIXME)!
350 void SortChildren(const wxTreeItemId
& item
);
355 // determine to which item (if any) belongs the given point (the
356 // coordinates specified are relative to the client area of tree ctrl)
357 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
360 // The first function is more portable (because easier to implement
361 // on other platforms), but the second one returns some extra info.
362 wxTreeItemId
HitTest(const wxPoint
& point
)
363 { int dummy
; return HitTest(point
, dummy
); }
364 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
366 // get the bounding rectangle of the item (or of its label only)
367 bool GetBoundingRect(const wxTreeItemId
& item
,
369 bool textOnly
= FALSE
) const;
374 #if WXWIN_COMPATIBILITY_2_4
375 // these methods are deprecated and will be removed in future versions of
376 // wxWindows, they're here for compatibility only, don't use them in new
377 // code (the comments indicate why these methods are now useless and how to
380 // use Expand, Collapse, CollapseAndReset or Toggle
381 wxDEPRECATED( void ExpandItem(const wxTreeItemId
& item
, int action
) );
383 // use AddRoot, PrependItem or AppendItem
384 wxDEPRECATED( wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
385 const wxString
& text
,
386 int image
= -1, int selImage
= -1,
387 long insertAfter
= wxTREE_INSERT_LAST
) );
389 // use Set/GetImageList and Set/GetStateImageList
390 wxImageList
*GetImageList(int) const { return GetImageList(); }
391 void SetImageList(wxImageList
*imageList
, int) { SetImageList(imageList
); }
393 // use Set/GetItemImage directly
394 int GetItemSelectedImage(const wxTreeItemId
& item
) const
395 { return GetItemImage(item
, wxTreeItemIcon_Selected
); }
396 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
397 { SetItemImage(item
, image
, wxTreeItemIcon_Selected
); }
399 // use the versions taking wxTreeItemIdValue cookies
400 wxDEPRECATED( wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
401 long& cookie
) const );
402 wxDEPRECATED( wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
403 long& cookie
) const );
404 #endif // WXWIN_COMPATIBILITY_2_4
409 virtual long MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
410 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
411 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
413 // override some base class virtuals
414 virtual bool SetBackgroundColour(const wxColour
&colour
);
415 virtual bool SetForegroundColour(const wxColour
&colour
);
417 // get/set the check state for the item (only for wxTR_MULTIPLE)
418 bool IsItemChecked(const wxTreeItemId
& item
) const;
419 void SetItemCheck(const wxTreeItemId
& item
, bool check
= TRUE
);
421 // set/get the item state.image (state == -1 means cycle to the next one)
422 void SetState(const wxTreeItemId
& node
, int state
);
423 int GetState(const wxTreeItemId
& node
);
426 // SetImageList helper
427 void SetAnyImageList(wxImageList
*imageList
, int which
);
429 // refresh a single item
430 void RefreshItem(const wxTreeItemId
& item
);
432 wxTextCtrl
*m_textCtrl
; // used while editing the item label
433 wxImageList
*m_imageListNormal
, // images for tree elements
434 *m_imageListState
; // special images for app defined states
435 bool m_ownsImageListNormal
, m_ownsImageListState
;
438 // the common part of all ctors
442 inline bool DoGetItem(wxTreeViewItem
*tvItem
) const;
443 inline void DoSetItem(wxTreeViewItem
*tvItem
);
445 inline void DoExpand(const wxTreeItemId
& item
, int flag
);
447 wxTreeItemId
DoInsertItem(const wxTreeItemId
& parent
,
448 wxTreeItemId hInsertAfter
,
449 const wxString
& text
,
450 int image
, int selectedImage
,
451 wxTreeItemData
*data
);
453 int DoGetItemImageFromData(const wxTreeItemId
& item
,
454 wxTreeItemIcon which
) const;
455 void DoSetItemImageFromData(const wxTreeItemId
& item
,
457 wxTreeItemIcon which
) const;
458 void DoSetItemImages(const wxTreeItemId
& item
, int image
, int imageSel
);
460 void DeleteTextCtrl();
462 // support for additional item images which we implement using
463 // wxTreeItemIndirectData technique - see the comments in msw/treectrl.cpp
464 void SetIndirectItemData(const wxTreeItemId
& item
,
465 class wxTreeItemIndirectData
*data
);
466 bool HasIndirectData(const wxTreeItemId
& item
) const;
467 bool IsDataIndirect(wxTreeItemData
*data
) const
468 { return data
&& data
->GetId().m_pItem
== 0; }
470 // the hash storing the items attributes (indexed by item ids)
471 wxMapTreeAttr m_attrs
;
473 // TRUE if the hash above is not empty
477 wxDragImage
*m_dragImage
;
479 // Virtual root item, if wxTR_HIDE_ROOT is set.
480 void* m_pVirtualRoot
;
482 // the starting item for selection with Shift
483 wxTreeItemId m_htSelStart
;
485 friend class wxTreeItemIndirectData
;
486 friend class wxTreeSortHelper
;
488 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
489 DECLARE_NO_COPY_CLASS(wxTreeCtrl
)
492 #endif // wxUSE_TREECTRL