1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: documentation for wxTreeItemData class
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 wxTreeItemData is some (arbitrary) user class associated with some item. The
14 main advantage of having this class is that wxTreeItemData objects are
15 destroyed automatically by the tree and, as this class has virtual destructor,
16 it means that the memory and any other resources associated with a tree item
17 will be automatically freed when it is deleted. Note that we don't use wxObject
18 as the base class for wxTreeItemData because the size of this class is
19 critical: in many applications, each tree leaf will have wxTreeItemData
20 associated with it and the number of leaves may be quite big.
22 Also please note that because the objects of this class are deleted by the tree
23 using the operator @c delete, they must always be allocated on the heap
32 class wxTreeItemData
: public wxClientData
37 In addition, the following methods are added in wxPython for accessing
45 Returns a reference to the Python Object
50 Associates a new Python Object with the
61 Returns the item associated with this node.
63 const wxTreeItemId
GetId();
66 Sets the item associated with this node.
68 void SetId(const wxTreeItemId
& id
);
76 A tree control presents information as a hierarchy, with items that may be
78 to show further items. Items in a tree control are referenced by wxTreeItemId
80 which may be tested for validity by calling wxTreeItemId::IsOk.
82 To intercept events from a tree control, use the event table macros described
86 @style{wxTR_EDIT_LABELS}:
87 Use this style if you wish the user to be able to edit labels in
89 @style{wxTR_NO_BUTTONS}:
90 For convenience to document that no buttons are to be drawn.
91 @style{wxTR_HAS_BUTTONS}:
92 Use this style to show + and - buttons to the left of parent items.
93 @style{wxTR_NO_LINES}:
94 Use this style to hide vertical level connectors.
95 @style{wxTR_FULL_ROW_HIGHLIGHT}:
96 Use this style to have the background colour and the selection
97 highlight extend over the entire horizontal row of the tree control
98 window. (This flag is ignored under Windows unless you specify
99 wxTR_NO_LINES as well.)
100 @style{wxTR_LINES_AT_ROOT}:
101 Use this style to show lines between root nodes. Only applicable if
102 wxTR_HIDE_ROOT is set and wxTR_NO_LINES is not set.
103 @style{wxTR_HIDE_ROOT}:
104 Use this style to suppress the display of the root node,
105 effectively causing the first-level nodes to appear as a series of
107 @style{wxTR_ROW_LINES}:
108 Use this style to draw a contrasting border between displayed rows.
109 @style{wxTR_HAS_VARIABLE_ROW_HEIGHT}:
110 Use this style to cause row heights to be just big enough to fit
111 the content. If not set, all rows use the largest row height. The
112 default is that this flag is unset. Generic only.
114 For convenience to document that only one item may be selected at a
115 time. Selecting another item causes the current selection, if any,
116 to be deselected. This is the default.
117 @style{wxTR_MULTIPLE}:
118 Use this style to allow a range of items to be selected. If a
119 second range is selected, the current range, if any, is deselected.
120 @style{wxTR_DEFAULT_STYLE}:
121 The set of flags that are closest to the defaults for the native
122 control for a particular toolkit.
127 @appearance{treectrl.png}
130 wxTreeItemData, @ref overview_wxtreectrloverview "wxTreeCtrl overview",
131 wxListBox, wxListCtrl, wxImageList, wxTreeEvent
133 class wxTreeCtrl
: public wxControl
138 Constructor, creating and showing a tree control.
141 Parent window. Must not be @NULL.
144 Window identifier. The value wxID_ANY indicates a default value.
150 Window size. If wxDefaultSize is specified then the window is sized
154 Window style. See wxTreeCtrl.
162 @sa Create(), wxValidator
165 wxTreeCtrl(wxWindow
* parent
, wxWindowID id
,
166 const wxPoint
& pos
= wxDefaultPosition
,
167 const wxSize
& size
= wxDefaultSize
,
168 long style
= wxTR_HAS_BUTTONS
,
169 const wxValidator
& validator
= wxDefaultValidator
,
170 const wxString
& name
= "treeCtrl");
174 Destructor, destroying the tree control.
179 Adds the root node to the tree, returning the new item.
181 The @e image and @e selImage parameters are an index within
182 the normal image list specifying the image to use for unselected and
183 selected items, respectively.
184 If @e image -1 and @e selImage is -1, the same image is used for
185 both selected and unselected items.
187 wxTreeItemId
AddRoot(const wxString
& text
, int image
= -1,
189 wxTreeItemData
* data
= @NULL
);
192 Appends an item to the end of the branch identified by @e parent, return a new
195 The @e image and @e selImage parameters are an index within
196 the normal image list specifying the image to use for unselected and
197 selected items, respectively.
198 If @e image -1 and @e selImage is -1, the same image is used for
199 both selected and unselected items.
201 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
202 const wxString
& text
,
205 wxTreeItemData
* data
= @NULL
);
208 Sets the buttons image list. The button images assigned with this method will
209 be automatically deleted by wxTreeCtrl as appropriate
210 (i.e. it takes ownership of the list).
212 Setting or assigning the button image list enables the display of image buttons.
213 Once enabled, the only way to disable the display of button images is to set
214 the button image list to @NULL.
216 This function is only available in the generic version.
218 See also SetButtonsImageList().
220 void AssignButtonsImageList(wxImageList
* imageList
);
223 Sets the normal image list. Image list assigned with this method will
224 be automatically deleted by wxTreeCtrl as appropriate
225 (i.e. it takes ownership of the list).
227 See also SetImageList().
229 void AssignImageList(wxImageList
* imageList
);
232 Sets the state image list. Image list assigned with this method will
233 be automatically deleted by wxTreeCtrl as appropriate
234 (i.e. it takes ownership of the list).
236 See also SetStateImageList().
238 void AssignStateImageList(wxImageList
* imageList
);
241 Collapses the given item.
243 void Collapse(const wxTreeItemId
& item
);
246 Collapses the root item.
253 Collapses this item and all of its children, recursively.
255 @sa ExpandAllChildren()
257 void CollapseAllChildren(const wxTreeItemId
& item
);
260 Collapses the given item and removes all children.
262 void CollapseAndReset(const wxTreeItemId
& item
);
265 Creates the tree control. See wxTreeCtrl() for further details.
267 bool wxTreeCtrl(wxWindow
* parent
, wxWindowID id
,
268 const wxPoint
& pos
= wxDefaultPosition
,
269 const wxSize
& size
= wxDefaultSize
,
270 long style
= wxTR_HAS_BUTTONS
,
271 const wxValidator
& validator
= wxDefaultValidator
,
272 const wxString
& name
= "treeCtrl");
275 Deletes the specified item. A @c EVT_TREE_DELETE_ITEM event will be
278 This function may cause a subsequent call to GetNextChild to fail.
280 void Delete(const wxTreeItemId
& item
);
283 Deletes all items in the control. Note that this may not generate
284 @c EVT_TREE_DELETE_ITEM events under some Windows versions although
285 normally such event is generated for each removed item.
287 void DeleteAllItems();
290 Deletes all children of the given item (but not the item itself). Note that
291 this will @b not generate any events unlike
294 If you have called SetItemHasChildren(), you
295 may need to call it again since @e DeleteChildren does not automatically
298 void DeleteChildren(const wxTreeItemId
& item
);
301 Starts editing the label of the given item. This function generates a
302 EVT_TREE_BEGIN_LABEL_EDIT event which can be vetoed so that no
303 text control will appear for in-place editing.
305 If the user changed the label (i.e. s/he does not press ESC or leave
306 the text control without changes, a EVT_TREE_END_LABEL_EDIT event
307 will be sent which can be vetoed as well.
309 @sa EndEditLabel(), wxTreeEvent
311 void EditLabel(const wxTreeItemId
& item
);
314 Ends label editing. If @e cancelEdit is @true, the edit will be cancelled.
316 This function is currently supported under Windows only.
320 void EndEditLabel(bool cancelEdit
);
323 Scrolls and/or expands items to ensure that the given item is visible.
325 void EnsureVisible(const wxTreeItemId
& item
);
328 Expands the given item.
330 void Expand(const wxTreeItemId
& item
);
333 Expands all items in the tree.
338 Expands the given item and all its children recursively.
340 void ExpandAllChildren(const wxTreeItemId
& item
);
343 Retrieves the rectangle bounding the @e item. If @e textOnly is @true,
344 only the rectangle around the item's label will be returned, otherwise the
345 item's image is also taken into account.
347 The return value is @true if the rectangle was successfully retrieved or @c
349 if it was not (in this case @e rect is not changed) -- for example, if the
350 item is currently invisible.
352 Notice that the rectangle coordinates are logical, not physical ones. So, for
353 example, the x coordinate may be negative if the tree has a horizontal
354 scrollbar and its position is not 0.
356 bool GetBoundingRect(const wxTreeItemId
& item
, wxRect
& rect
,
357 bool textOnly
= @
false);
360 Returns the buttons image list (from which application-defined button images
363 This function is only available in the generic version.
365 wxImageList
* GetButtonsImageList();
368 Returns the number of items in the branch. If @e recursively is @true,
369 returns the total number
370 of descendants, otherwise only one level of children is counted.
372 unsigned int GetChildrenCount(const wxTreeItemId
& item
,
373 bool recursively
= @
true);
376 Returns the number of items in the control.
378 unsigned int GetCount();
381 Returns the edit control being currently used to edit a label. Returns @NULL
382 if no label is being edited.
384 @b NB: It is currently only implemented for wxMSW.
386 wxTextCtrl
* GetEditControl();
389 Returns the first child; call GetNextChild() for the next child.
391 For this enumeration function you must pass in a 'cookie' parameter
392 which is opaque for the application but is necessary for the library
393 to make these functions reentrant (i.e. allow more than one
394 enumeration on one and the same object simultaneously). The cookie passed to
395 GetFirstChild and GetNextChild should be the same variable.
397 Returns an invalid tree item (i.e. IsOk() returns @false) if there are no
400 @sa GetNextChild(), GetNextSibling()
402 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
403 wxTreeItemIdValue
& cookie
);
406 Returns the first visible item.
408 wxTreeItemId
GetFirstVisibleItem();
411 Returns the normal image list.
413 wxImageList
* GetImageList();
416 Returns the current tree control indentation.
421 Returns the background colour of the item.
423 wxColour
GetItemBackgroundColour(const wxTreeItemId
& item
);
427 Returns the font of the item label.
429 wxTreeItemData
* GetItemData(const wxTreeItemId
& item
);
430 See also wxPython note
: wxPython provides the following shortcut method
:
443 Returns the Python Object
444 associated with the wxTreeItemData
for the given item Id
.
452 wxFont
GetItemFont(const wxTreeItemId
& item
);
456 Gets the specified item image. The value of @e which may be:
458 wxTreeItemIcon_Normal to get the normal item image
459 wxTreeItemIcon_Selected to get the selected item image (i.e. the image
460 which is shown when the item is currently selected)
461 wxTreeItemIcon_Expanded to get the expanded image (this only
462 makes sense for items which have children - then this image is shown when the
463 item is expanded and the normal image is shown when it is collapsed)
464 wxTreeItemIcon_SelectedExpanded to get the selected expanded image
465 (which is shown when an expanded item is currently selected)
467 int GetItemImage(const wxTreeItemId
& item
,
468 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
471 Returns the item's parent.
473 wxTreeItemId
GetItemParent(const wxTreeItemId
& item
);
476 Gets the selected item image (this function is obsolete, use
477 @c GetItemImage(item, wxTreeItemIcon_Selected) instead).
479 int GetItemSelectedImage(const wxTreeItemId
& item
);
482 Returns the item label.
484 wxString
GetItemText(const wxTreeItemId
& item
);
487 Returns the colour of the item label.
489 wxColour
GetItemTextColour(const wxTreeItemId
& item
);
492 Returns the last child of the item (or an invalid tree item if this item has no
495 @sa GetFirstChild(), GetNextSibling(),
498 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
);
501 Returns the next child; call GetFirstChild() for the first child.
503 For this enumeration function you must pass in a 'cookie' parameter
504 which is opaque for the application but is necessary for the library
505 to make these functions reentrant (i.e. allow more than one
506 enumeration on one and the same object simultaneously). The cookie passed to
507 GetFirstChild and GetNextChild should be the same.
509 Returns an invalid tree item if there are no further children.
513 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
514 wxTreeItemIdValue
& cookie
);
517 Returns the next sibling of the specified item; call GetPrevSibling() for the
520 Returns an invalid tree item if there are no further siblings.
524 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
);
527 Returns the next visible item or an invalid item if this item is the last
530 Notice that the @e item itself must be visible.
532 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
);
535 Returns the previous sibling of the specified item; call GetNextSibling() for
538 Returns an invalid tree item if there are no further children.
542 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
);
545 Returns the previous visible item or an invalid item if this item is the first
548 Notice that the @e item itself must be visible.
550 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
);
553 Returns @true if the control will use a quick calculation for the best size,
554 looking only at the first and last items. The default is @false.
556 @sa SetQuickBestSize()
558 bool GetQuickBestSize();
561 Returns the root item for the tree control.
563 wxTreeItemId
GetRootItem();
566 Returns the selection, or an invalid item if there is no selection.
567 This function only works with the controls without wxTR_MULTIPLE style, use
568 GetSelections() for the controls which do have
571 wxTreeItemId
GetSelection();
574 Fills the array of tree items passed in with the currently selected items. This
575 function can be called only if the control has the wxTR_MULTIPLE style.
577 Returns the number of selected items.
579 unsigned int GetSelections(wxArrayTreeItemIds
& selection
);
582 Returns the state image list (from which application-defined state images are
585 wxImageList
* GetStateImageList();
588 Calculates which (if any) item is under the given point, returning the tree item
589 id at this point plus extra information @e flags. @e flags is a bitlist of the
596 Above the client area.
601 Below the client area.
603 wxTREE_HITTEST_NOWHERE
606 In the client area but below the last item.
608 wxTREE_HITTEST_ONITEMBUTTON
611 On the button associated with an item.
613 wxTREE_HITTEST_ONITEMICON
616 On the bitmap associated with an item.
618 wxTREE_HITTEST_ONITEMINDENT
621 In the indentation associated with an item.
623 wxTREE_HITTEST_ONITEMLABEL
626 On the label (string) associated with an item.
628 wxTREE_HITTEST_ONITEMRIGHT
631 In the area to the right of an item.
633 wxTREE_HITTEST_ONITEMSTATEICON
636 On the state icon for a tree view item that is in a user-defined state.
638 wxTREE_HITTEST_TOLEFT
641 To the right of the client area.
643 wxTREE_HITTEST_TORIGHT
646 To the left of the client area.
648 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
652 Inserts an item after a given one (@e previous) or before one identified by its
653 position (@e before).
654 @e before must be less than the number of children.
656 The @e image and @e selImage parameters are an index within
657 the normal image list specifying the image to use for unselected and
658 selected items, respectively.
659 If @e image -1 and @e selImage is -1, the same image is used for
660 both selected and unselected items.
662 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
663 const wxTreeItemId
& previous
,
664 const wxString
& text
,
667 wxTreeItemData
* data
= @NULL
);
668 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
670 const wxString
& text
,
673 wxTreeItemData
* data
= @NULL
);
677 Returns @true if the given item is in bold state.
679 See also: SetItemBold()
681 bool IsBold(const wxTreeItemId
& item
);
684 Returns @true if the control is empty (i.e. has no items, even no root one).
689 Returns @true if the item is expanded (only makes sense if it has children).
691 bool IsExpanded(const wxTreeItemId
& item
);
694 Returns @true if the item is selected.
696 bool IsSelected(const wxTreeItemId
& item
);
699 Returns @true if the item is visible on the screen.
701 bool IsVisible(const wxTreeItemId
& item
);
704 Returns @true if the item has children.
706 bool ItemHasChildren(const wxTreeItemId
& item
);
709 Override this function in the derived class to change the sort order of the
710 items in the tree control. The function should return a negative, zero or
711 positive value if the first item is less than, equal to or greater than the
714 Please note that you @b must use wxRTTI macros
715 DECLARE_DYNAMIC_CLASS and
716 IMPLEMENT_DYNAMIC_CLASS if you override this
717 function because otherwise the base class considers that it is not overridden
718 and uses the default comparison, i.e. sorts the items alphabetically, which
719 allows it optimize away the calls to the virtual function completely.
721 See also: SortChildren()
723 int OnCompareItems(const wxTreeItemId
& item1
,
724 const wxTreeItemId
& item2
);
727 Appends an item as the first child of @e parent, return a new item id.
729 The @e image and @e selImage parameters are an index within
730 the normal image list specifying the image to use for unselected and
731 selected items, respectively.
732 If @e image -1 and @e selImage is -1, the same image is used for
733 both selected and unselected items.
735 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
736 const wxString
& text
,
739 wxTreeItemData
* data
= @NULL
);
742 Scrolls the specified item into view.
744 void ScrollTo(const wxTreeItemId
& item
);
747 Selects the given item. In multiple selection controls, can be also used to
748 deselect a currently selected item if the value of @e select is @false.
750 void SelectItem(const wxTreeItemId
& item
, bool select
= @
true);
753 Sets the buttons image list (from which application-defined button images are
755 The button images assigned with this method will
756 @b not be deleted by wxTreeCtrl's destructor, you must delete it yourself.
758 Setting or assigning the button image list enables the display of image buttons.
759 Once enabled, the only way to disable the display of button images is to set
760 the button image list to @NULL.
762 This function is only available in the generic version.
764 See also AssignButtonsImageList().
766 void SetButtonsImageList(wxImageList
* imageList
);
769 Sets the normal image list. Image list assigned with this method will
770 @b not be deleted by wxTreeCtrl's destructor, you must delete it yourself.
772 See also AssignImageList().
774 void SetImageList(wxImageList
* imageList
);
777 Sets the indentation for the tree control.
779 void SetIndent(int indent
);
782 Sets the colour of the item's background.
784 void SetItemBackgroundColour(const wxTreeItemId
& item
,
785 const wxColour
& col
);
788 Makes item appear in bold font if @e bold parameter is @true or resets it to
793 void SetItemBold(const wxTreeItemId
& item
, bool bold
= @
true);
797 Gives the item the visual feedback for Drag'n'Drop actions, which is
798 useful if something is dragged from the outside onto the tree control
799 (as opposed to a DnD operation within the tree control, which already
800 is implemented internally).
802 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
* data
);
803 wxPython note
: SetPyData(item
, obj
)
808 Associate the given Python
809 Object with the wxTreeItemData
for the given item Id
.
817 void SetItemDropHighlight(const wxTreeItemId
& item
,
818 bool highlight
= @
true);
822 Sets the item's font. All items in the tree should have the same height to avoid
823 text clipping, so the fonts height should be the same for all of them,
824 although font attributes may vary.
828 void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
831 Force appearance of the button next to the item. This is useful to
832 allow the user to expand the items which don't have any children now,
833 but instead adding them only when needed, thus minimizing memory
834 usage and loading time.
836 void SetItemHasChildren(const wxTreeItemId
& item
,
837 bool hasChildren
= @
true);
840 Sets the specified item image. See GetItemImage()
841 for the description of the @e which parameter.
843 void SetItemImage(const wxTreeItemId
& item
, int image
,
844 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
847 Sets the selected item image (this function is obsolete, use
848 @c SetItemImage(item, wxTreeItemIcon_Selected) instead).
850 void SetItemSelectedImage(const wxTreeItemId
& item
, int selImage
);
855 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
858 Sets the colour of the item's text.
860 void SetItemTextColour(const wxTreeItemId
& item
,
861 const wxColour
& col
);
864 If @true is passed, specifies that the control will use a quick calculation for
866 looking only at the first and last items. Otherwise, it will look at all items.
867 The default is @false.
869 @sa GetQuickBestSize()
871 void SetQuickBestSize(bool quickBestSize
);
874 Sets the state image list (from which application-defined state images are
876 Image list assigned with this method will
877 @b not be deleted by wxTreeCtrl's destructor, you must delete it yourself.
879 See also AssignStateImageList().
881 void SetStateImageList(wxImageList
* imageList
);
884 Sets the mode flags associated with the display of the tree control.
885 The new mode takes effect immediately.
886 (Generic only; MSW ignores changes.)
888 void SetWindowStyle(long styles
);
891 Sorts the children of the given item using
892 OnCompareItems() method of wxTreeCtrl. You
893 should override that method to change the sort order (the default is ascending
894 case-sensitive alphabetical order).
896 @sa wxTreeItemData, OnCompareItems()
898 void SortChildren(const wxTreeItemId
& item
);
901 Toggles the given item between collapsed and expanded states.
903 void Toggle(const wxTreeItemId
& item
);
906 Toggles the given item between selected and unselected states. For
907 multiselection controls only.
909 void ToggleItemSelection(const wxTreeItemId
& item
);
912 Removes the selection from the currently selected item (if any).
917 This function either behaves the same as Unselect()
918 if the control doesn't have wxTR_MULTIPLE style, or removes the selection from
919 all items if it does have this style.
924 Unselects the given item. This works in multiselection controls only.
926 void UnselectItem(const wxTreeItemId
& item
);
932 @wxheader{treectrl.h}
934 A tree event holds information about events associated with wxTreeCtrl objects.
942 class wxTreeEvent
: public wxNotifyEvent
948 Constructor, used by wxWidgets itself only.
950 wxTreeEvent(wxEventType commandType
, wxTreeCtrl
* tree
);
953 Returns the item (valid for all events).
955 wxTreeItemId
GetItem();
958 Returns the key code if the event is a key event. Use
959 GetKeyEvent() to get the values of the
960 modifier keys for this event (i.e. Shift or Ctrl).
965 Returns the key event for @c EVT_TREE_KEY_DOWN events.
967 const wxKeyEvent
GetKeyEvent();
970 Returns the label if the event is a begin or end edit label event.
972 const wxString
GetLabel();
975 Returns the old item index (valid for EVT_TREE_ITEM_CHANGING and CHANGED events)
977 wxTreeItemId
GetOldItem();
980 Returns the position of the mouse pointer if the event is a drag or
982 In both cases the position is in client coordinates - i.e. relative to the
984 window (so that you can pass it directly to e.g. wxWindow::PopupMenu).
989 Returns @true if the label edit was cancelled. This should be
990 called from within an EVT_TREE_END_LABEL_EDIT handler.
992 bool IsEditCancelled();
995 Set the tooltip for the item (valid for EVT_TREE_ITEM_GETTOOLTIP events).
998 void SetToolTip(const wxString
& tooltip
);