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
38 In addition, the following methods are added in wxPython for accessing
43 Returns a reference to the Python Object
47 Associates a new Python Object with the
58 Returns the item associated with this node.
60 const wxTreeItemId
GetId();
63 Sets the item associated with this node.
65 void SetId(const wxTreeItemId
& id
);
73 A tree control presents information as a hierarchy, with items that may be
75 to show further items. Items in a tree control are referenced by wxTreeItemId
77 which may be tested for validity by calling wxTreeItemId::IsOk.
79 To intercept events from a tree control, use the event table macros described
83 @style{wxTR_EDIT_LABELS}:
84 Use this style if you wish the user to be able to edit labels in
86 @style{wxTR_NO_BUTTONS}:
87 For convenience to document that no buttons are to be drawn.
88 @style{wxTR_HAS_BUTTONS}:
89 Use this style to show + and - buttons to the left of parent items.
90 @style{wxTR_NO_LINES}:
91 Use this style to hide vertical level connectors.
92 @style{wxTR_FULL_ROW_HIGHLIGHT}:
93 Use this style to have the background colour and the selection
94 highlight extend over the entire horizontal row of the tree control
95 window. (This flag is ignored under Windows unless you specify
96 wxTR_NO_LINES as well.)
97 @style{wxTR_LINES_AT_ROOT}:
98 Use this style to show lines between root nodes. Only applicable if
99 wxTR_HIDE_ROOT is set and wxTR_NO_LINES is not set.
100 @style{wxTR_HIDE_ROOT}:
101 Use this style to suppress the display of the root node,
102 effectively causing the first-level nodes to appear as a series of
104 @style{wxTR_ROW_LINES}:
105 Use this style to draw a contrasting border between displayed rows.
106 @style{wxTR_HAS_VARIABLE_ROW_HEIGHT}:
107 Use this style to cause row heights to be just big enough to fit
108 the content. If not set, all rows use the largest row height. The
109 default is that this flag is unset. Generic only.
111 For convenience to document that only one item may be selected at a
112 time. Selecting another item causes the current selection, if any,
113 to be deselected. This is the default.
114 @style{wxTR_MULTIPLE}:
115 Use this style to allow a range of items to be selected. If a
116 second range is selected, the current range, if any, is deselected.
117 @style{wxTR_DEFAULT_STYLE}:
118 The set of flags that are closest to the defaults for the native
119 control for a particular toolkit.
124 @appearance{treectrl.png}
127 wxTreeItemData, @ref overview_wxtreectrloverview "wxTreeCtrl overview",
128 wxListBox, wxListCtrl, wxImageList, wxTreeEvent
130 class wxTreeCtrl
: public wxControl
135 Constructor, creating and showing a tree control.
138 Parent window. Must not be @NULL.
140 Window identifier. The value wxID_ANY indicates a default value.
144 Window size. If wxDefaultSize is specified then the window is
148 Window style. See wxTreeCtrl.
154 @see Create(), wxValidator
157 wxTreeCtrl(wxWindow
* parent
, wxWindowID id
,
158 const wxPoint
& pos
= wxDefaultPosition
,
159 const wxSize
& size
= wxDefaultSize
,
160 long style
= wxTR_HAS_BUTTONS
,
161 const wxValidator
& validator
= wxDefaultValidator
,
162 const wxString
& name
= "treeCtrl");
166 Destructor, destroying the tree control.
171 Adds the root node to the tree, returning the new item.
172 The @a image and @a selImage parameters are an index within
173 the normal image list specifying the image to use for unselected and
174 selected items, respectively.
175 If @a image -1 and @a selImage is -1, the same image is used for
176 both selected and unselected items.
178 wxTreeItemId
AddRoot(const wxString
& text
, int image
= -1,
180 wxTreeItemData
* data
= NULL
);
183 Appends an item to the end of the branch identified by @e parent, return a new
185 The @a image and @a selImage parameters are an index within
186 the normal image list specifying the image to use for unselected and
187 selected items, respectively.
188 If @a image -1 and @a selImage is -1, the same image is used for
189 both selected and unselected items.
191 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
192 const wxString
& text
,
195 wxTreeItemData
* data
= NULL
);
198 Sets the buttons image list. The button images assigned with this method will
199 be automatically deleted by wxTreeCtrl as appropriate
200 (i.e. it takes ownership of the list).
201 Setting or assigning the button image list enables the display of image buttons.
202 Once enabled, the only way to disable the display of button images is to set
203 the button image list to @NULL.
204 This function is only available in the generic version.
205 See also SetButtonsImageList().
207 void AssignButtonsImageList(wxImageList
* imageList
);
210 Sets the normal image list. Image list assigned with this method will
211 be automatically deleted by wxTreeCtrl as appropriate
212 (i.e. it takes ownership of the list).
213 See also SetImageList().
215 void AssignImageList(wxImageList
* imageList
);
218 Sets the state image list. Image list assigned with this method will
219 be automatically deleted by wxTreeCtrl as appropriate
220 (i.e. it takes ownership of the list).
221 See also SetStateImageList().
223 void AssignStateImageList(wxImageList
* imageList
);
226 Collapses the given item.
228 void Collapse(const wxTreeItemId
& item
);
231 Collapses the root item.
238 Collapses this item and all of its children, recursively.
240 @see ExpandAllChildren()
242 void CollapseAllChildren(const wxTreeItemId
& item
);
245 Collapses the given item and removes all children.
247 void CollapseAndReset(const wxTreeItemId
& item
);
250 Creates the tree control. See wxTreeCtrl() for further details.
252 bool wxTreeCtrl(wxWindow
* parent
, wxWindowID id
,
253 const wxPoint
& pos
= wxDefaultPosition
,
254 const wxSize
& size
= wxDefaultSize
,
255 long style
= wxTR_HAS_BUTTONS
,
256 const wxValidator
& validator
= wxDefaultValidator
,
257 const wxString
& name
= "treeCtrl");
260 Deletes the specified item. A @c EVT_TREE_DELETE_ITEM event will be
262 This function may cause a subsequent call to GetNextChild to fail.
264 void Delete(const wxTreeItemId
& item
);
267 Deletes all items in the control. Note that this may not generate
268 @c EVT_TREE_DELETE_ITEM events under some Windows versions although
269 normally such event is generated for each removed item.
271 void DeleteAllItems();
274 Deletes all children of the given item (but not the item itself). Note that
275 this will @b not generate any events unlike
277 If you have called SetItemHasChildren(), you
278 may need to call it again since @e DeleteChildren does not automatically
281 void DeleteChildren(const wxTreeItemId
& item
);
284 Starts editing the label of the given item. This function generates a
285 EVT_TREE_BEGIN_LABEL_EDIT event which can be vetoed so that no
286 text control will appear for in-place editing.
287 If the user changed the label (i.e. s/he does not press ESC or leave
288 the text control without changes, a EVT_TREE_END_LABEL_EDIT event
289 will be sent which can be vetoed as well.
291 @see EndEditLabel(), wxTreeEvent
293 void EditLabel(const wxTreeItemId
& item
);
296 Ends label editing. If @a cancelEdit is @true, the edit will be cancelled.
297 This function is currently supported under Windows only.
301 void EndEditLabel(bool cancelEdit
);
304 Scrolls and/or expands items to ensure that the given item is visible.
306 void EnsureVisible(const wxTreeItemId
& item
);
309 Expands the given item.
311 void Expand(const wxTreeItemId
& item
);
314 Expands all items in the tree.
319 Expands the given item and all its children recursively.
321 void ExpandAllChildren(const wxTreeItemId
& item
);
324 Retrieves the rectangle bounding the @e item. If @a textOnly is @true,
325 only the rectangle around the item's label will be returned, otherwise the
326 item's image is also taken into account.
327 The return value is @true if the rectangle was successfully retrieved or @c
329 if it was not (in this case @a rect is not changed) -- for example, if the
330 item is currently invisible.
331 Notice that the rectangle coordinates are logical, not physical ones. So, for
332 example, the x coordinate may be negative if the tree has a horizontal
333 scrollbar and its position is not 0.
335 bool GetBoundingRect(const wxTreeItemId
& item
, wxRect
& rect
,
336 bool textOnly
= false);
339 Returns the buttons image list (from which application-defined button images
341 This function is only available in the generic version.
343 wxImageList
* GetButtonsImageList();
346 Returns the number of items in the branch. If @a recursively is @true,
347 returns the total number
348 of descendants, otherwise only one level of children is counted.
350 unsigned int GetChildrenCount(const wxTreeItemId
& item
,
351 bool recursively
= true);
354 Returns the number of items in the control.
356 unsigned int GetCount();
359 Returns the edit control being currently used to edit a label. Returns @NULL
360 if no label is being edited.
361 @b NB: It is currently only implemented for wxMSW.
363 wxTextCtrl
* GetEditControl();
366 Returns the first child; call GetNextChild() for the next child.
367 For this enumeration function you must pass in a 'cookie' parameter
368 which is opaque for the application but is necessary for the library
369 to make these functions reentrant (i.e. allow more than one
370 enumeration on one and the same object simultaneously). The cookie passed to
371 GetFirstChild and GetNextChild should be the same variable.
372 Returns an invalid tree item (i.e. IsOk() returns @false) if there are no
375 @see GetNextChild(), GetNextSibling()
377 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
378 wxTreeItemIdValue
& cookie
);
381 Returns the first visible item.
383 wxTreeItemId
GetFirstVisibleItem();
386 Returns the normal image list.
388 wxImageList
* GetImageList();
391 Returns the current tree control indentation.
396 Returns the background colour of the item.
398 wxColour
GetItemBackgroundColour(const wxTreeItemId
& item
);
402 Returns the font of the item label.
404 wxTreeItemData
* GetItemData(const wxTreeItemId
& item
);
405 See also wxPython note
:
406 wxPython provides the following shortcut method
:
419 Returns the Python Object
420 associated with the wxTreeItemData
for the given item Id
.
428 wxFont
GetItemFont(const wxTreeItemId
& item
);
432 Gets the specified item image. The value of @a which may be:
433 wxTreeItemIcon_Normal to get the normal item image
434 wxTreeItemIcon_Selected to get the selected item image (i.e. the image
435 which is shown when the item is currently selected)
436 wxTreeItemIcon_Expanded to get the expanded image (this only
437 makes sense for items which have children - then this image is shown when the
438 item is expanded and the normal image is shown when it is collapsed)
439 wxTreeItemIcon_SelectedExpanded to get the selected expanded image
440 (which is shown when an expanded item is currently selected)
442 int GetItemImage(const wxTreeItemId
& item
,
443 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
446 Returns the item's parent.
448 wxTreeItemId
GetItemParent(const wxTreeItemId
& item
);
451 Gets the selected item image (this function is obsolete, use
452 @c GetItemImage(item, wxTreeItemIcon_Selected) instead).
454 int GetItemSelectedImage(const wxTreeItemId
& item
);
457 Returns the item label.
459 wxString
GetItemText(const wxTreeItemId
& item
);
462 Returns the colour of the item label.
464 wxColour
GetItemTextColour(const wxTreeItemId
& item
);
467 Returns the last child of the item (or an invalid tree item if this item has no
470 @see GetFirstChild(), GetNextSibling(),
473 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
);
476 Returns the next child; call GetFirstChild() for the first child.
477 For this enumeration function you must pass in a 'cookie' parameter
478 which is opaque for the application but is necessary for the library
479 to make these functions reentrant (i.e. allow more than one
480 enumeration on one and the same object simultaneously). The cookie passed to
481 GetFirstChild and GetNextChild should be the same.
482 Returns an invalid tree item if there are no further children.
486 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
487 wxTreeItemIdValue
& cookie
);
490 Returns the next sibling of the specified item; call GetPrevSibling() for the
492 Returns an invalid tree item if there are no further siblings.
494 @see GetPrevSibling()
496 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
);
499 Returns the next visible item or an invalid item if this item is the last
501 Notice that the @a item itself must be visible.
503 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
);
506 Returns the previous sibling of the specified item; call GetNextSibling() for
508 Returns an invalid tree item if there are no further children.
510 @see GetNextSibling()
512 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
);
515 Returns the previous visible item or an invalid item if this item is the first
517 Notice that the @a item itself must be visible.
519 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
);
522 Returns @true if the control will use a quick calculation for the best size,
523 looking only at the first and last items. The default is @false.
525 @see SetQuickBestSize()
527 bool GetQuickBestSize();
530 Returns the root item for the tree control.
532 wxTreeItemId
GetRootItem();
535 Returns the selection, or an invalid item if there is no selection.
536 This function only works with the controls without wxTR_MULTIPLE style, use
537 GetSelections() for the controls which do have
540 wxTreeItemId
GetSelection();
543 Fills the array of tree items passed in with the currently selected items. This
544 function can be called only if the control has the wxTR_MULTIPLE style.
545 Returns the number of selected items.
547 unsigned int GetSelections(wxArrayTreeItemIds
& selection
);
550 Returns the state image list (from which application-defined state images are
553 wxImageList
* GetStateImageList();
556 Calculates which (if any) item is under the given point, returning the tree item
557 id at this point plus extra information @e flags. @a flags is a bitlist of the
562 Above the client area.
566 Below the client area.
568 wxTREE_HITTEST_NOWHERE
570 In the client area but below the last item.
572 wxTREE_HITTEST_ONITEMBUTTON
574 On the button associated with an item.
576 wxTREE_HITTEST_ONITEMICON
578 On the bitmap associated with an item.
580 wxTREE_HITTEST_ONITEMINDENT
582 In the indentation associated with an item.
584 wxTREE_HITTEST_ONITEMLABEL
586 On the label (string) associated with an item.
588 wxTREE_HITTEST_ONITEMRIGHT
590 In the area to the right of an item.
592 wxTREE_HITTEST_ONITEMSTATEICON
594 On the state icon for a tree view item that is in a user-defined state.
596 wxTREE_HITTEST_TOLEFT
598 To the right of the client area.
600 wxTREE_HITTEST_TORIGHT
602 To the left of the client area.
604 wxTreeItemId
HitTest(const wxPoint
& point
, int& flags
);
608 Inserts an item after a given one (@e previous) or before one identified by its
609 position (@e before).
610 @a before must be less than the number of children.
611 The @a image and @a selImage parameters are an index within
612 the normal image list specifying the image to use for unselected and
613 selected items, respectively.
614 If @a image -1 and @a selImage is -1, the same image is used for
615 both selected and unselected items.
617 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
618 const wxTreeItemId
& previous
,
619 const wxString
& text
,
622 wxTreeItemData
* data
= NULL
);
623 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
625 const wxString
& text
,
628 wxTreeItemData
* data
= NULL
);
632 Returns @true if the given item is in bold state.
633 See also: SetItemBold()
635 bool IsBold(const wxTreeItemId
& item
);
638 Returns @true if the control is empty (i.e. has no items, even no root one).
643 Returns @true if the item is expanded (only makes sense if it has children).
645 bool IsExpanded(const wxTreeItemId
& item
);
648 Returns @true if the item is selected.
650 bool IsSelected(const wxTreeItemId
& item
);
653 Returns @true if the item is visible on the screen.
655 bool IsVisible(const wxTreeItemId
& item
);
658 Returns @true if the item has children.
660 bool ItemHasChildren(const wxTreeItemId
& item
);
663 Override this function in the derived class to change the sort order of the
664 items in the tree control. The function should return a negative, zero or
665 positive value if the first item is less than, equal to or greater than the
667 Please note that you @b must use wxRTTI macros
668 DECLARE_DYNAMIC_CLASS and
669 IMPLEMENT_DYNAMIC_CLASS if you override this
670 function because otherwise the base class considers that it is not overridden
671 and uses the default comparison, i.e. sorts the items alphabetically, which
672 allows it optimize away the calls to the virtual function completely.
673 See also: SortChildren()
675 int OnCompareItems(const wxTreeItemId
& item1
,
676 const wxTreeItemId
& item2
);
679 Appends an item as the first child of @e parent, return a new item id.
680 The @a image and @a selImage parameters are an index within
681 the normal image list specifying the image to use for unselected and
682 selected items, respectively.
683 If @a image -1 and @a selImage is -1, the same image is used for
684 both selected and unselected items.
686 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
687 const wxString
& text
,
690 wxTreeItemData
* data
= NULL
);
693 Scrolls the specified item into view.
695 void ScrollTo(const wxTreeItemId
& item
);
698 Selects the given item. In multiple selection controls, can be also used to
699 deselect a currently selected item if the value of @a select is @false.
701 void SelectItem(const wxTreeItemId
& item
, bool select
= true);
704 Sets the buttons image list (from which application-defined button images are
706 The button images assigned with this method will
707 @b not be deleted by wxTreeCtrl's destructor, you must delete it yourself.
708 Setting or assigning the button image list enables the display of image buttons.
709 Once enabled, the only way to disable the display of button images is to set
710 the button image list to @NULL.
711 This function is only available in the generic version.
712 See also AssignButtonsImageList().
714 void SetButtonsImageList(wxImageList
* imageList
);
717 Sets the normal image list. Image list assigned with this method will
718 @b not be deleted by wxTreeCtrl's destructor, you must delete it yourself.
719 See also AssignImageList().
721 void SetImageList(wxImageList
* imageList
);
724 Sets the indentation for the tree control.
726 void SetIndent(int indent
);
729 Sets the colour of the item's background.
731 void SetItemBackgroundColour(const wxTreeItemId
& item
,
732 const wxColour
& col
);
735 Makes item appear in bold font if @a bold parameter is @true or resets it to
739 void SetItemBold(const wxTreeItemId
& item
, bool bold
= true);
743 Gives the item the visual feedback for Drag'n'Drop actions, which is
744 useful if something is dragged from the outside onto the tree control
745 (as opposed to a DnD operation within the tree control, which already
746 is implemented internally).
748 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
* data
);
755 Associate the given Python
756 Object with the wxTreeItemData
for the given item Id
.
764 void SetItemDropHighlight(const wxTreeItemId
& item
,
765 bool highlight
= true);
769 Sets the item's font. All items in the tree should have the same height to avoid
770 text clipping, so the fonts height should be the same for all of them,
771 although font attributes may vary.
775 void SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
);
778 Force appearance of the button next to the item. This is useful to
779 allow the user to expand the items which don't have any children now,
780 but instead adding them only when needed, thus minimizing memory
781 usage and loading time.
783 void SetItemHasChildren(const wxTreeItemId
& item
,
784 bool hasChildren
= true);
787 Sets the specified item image. See GetItemImage()
788 for the description of the @a which parameter.
790 void SetItemImage(const wxTreeItemId
& item
, int image
,
791 wxTreeItemIcon which
= wxTreeItemIcon_Normal
);
794 Sets the selected item image (this function is obsolete, use
795 @c SetItemImage(item, wxTreeItemIcon_Selected) instead).
797 void SetItemSelectedImage(const wxTreeItemId
& item
, int selImage
);
802 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
805 Sets the colour of the item's text.
807 void SetItemTextColour(const wxTreeItemId
& item
,
808 const wxColour
& col
);
811 If @true is passed, specifies that the control will use a quick calculation for
813 looking only at the first and last items. Otherwise, it will look at all items.
814 The default is @false.
816 @see GetQuickBestSize()
818 void SetQuickBestSize(bool quickBestSize
);
821 Sets the state image list (from which application-defined state images are
823 Image list assigned with this method will
824 @b not be deleted by wxTreeCtrl's destructor, you must delete it yourself.
825 See also AssignStateImageList().
827 void SetStateImageList(wxImageList
* imageList
);
830 Sets the mode flags associated with the display of the tree control.
831 The new mode takes effect immediately.
832 (Generic only; MSW ignores changes.)
834 void SetWindowStyle(long styles
);
837 Sorts the children of the given item using
838 OnCompareItems() method of wxTreeCtrl. You
839 should override that method to change the sort order (the default is ascending
840 case-sensitive alphabetical order).
842 @see wxTreeItemData, OnCompareItems()
844 void SortChildren(const wxTreeItemId
& item
);
847 Toggles the given item between collapsed and expanded states.
849 void Toggle(const wxTreeItemId
& item
);
852 Toggles the given item between selected and unselected states. For
853 multiselection controls only.
855 void ToggleItemSelection(const wxTreeItemId
& item
);
858 Removes the selection from the currently selected item (if any).
863 This function either behaves the same as Unselect()
864 if the control doesn't have wxTR_MULTIPLE style, or removes the selection from
865 all items if it does have this style.
870 Unselects the given item. This works in multiselection controls only.
872 void UnselectItem(const wxTreeItemId
& item
);
878 @wxheader{treectrl.h}
880 A tree event holds information about events associated with wxTreeCtrl objects.
888 class wxTreeEvent
: public wxNotifyEvent
893 Constructor, used by wxWidgets itself only.
895 wxTreeEvent(wxEventType commandType
, wxTreeCtrl
* tree
);
898 Returns the item (valid for all events).
900 wxTreeItemId
GetItem();
903 Returns the key code if the event is a key event. Use
904 GetKeyEvent() to get the values of the
905 modifier keys for this event (i.e. Shift or Ctrl).
910 Returns the key event for @c EVT_TREE_KEY_DOWN events.
912 const wxKeyEvent
GetKeyEvent();
915 Returns the label if the event is a begin or end edit label event.
917 const wxString
GetLabel();
920 Returns the old item index (valid for EVT_TREE_ITEM_CHANGING and CHANGED events)
922 wxTreeItemId
GetOldItem();
925 Returns the position of the mouse pointer if the event is a drag or
927 In both cases the position is in client coordinates - i.e. relative to the
929 window (so that you can pass it directly to e.g. wxWindow::PopupMenu).
934 Returns @true if the label edit was cancelled. This should be
935 called from within an EVT_TREE_END_LABEL_EDIT handler.
937 bool IsEditCancelled();
940 Set the tooltip for the item (valid for EVT_TREE_ITEM_GETTOOLTIP events).
943 void SetToolTip(const wxString
& tooltip
);