Contents Up Previous Next

wxTreeCtrl

A tree control presents information as a hierarchy, with items that may be expanded to show further items. Items in a tree control are referenced by wxTreeItemId handles.

To intercept events from a tree control, use the event table macros described in wxTreeEvent.

Derived from

wxControl
wxWindow
wxEvtHandler
wxObject

Include files

<wx/treectrl.h>

Window styles

wxTR_HAS_BUTTONS Use this style to show + and - buttons to the left of parent items. Win32 only.
wxTR_EDIT_LABELS Use this style if you wish the user to be able to edit labels in the tree control.
wxTR_MULTIPLE Use this style to allow the user to select more than one item in the control - by default, only one item may be selected.

See also window styles overview.

Event handling

To process input from a tree control, use these event handler macros to direct input to member functions that take a wxTreeEvent argument.

EVT_TREE_BEGIN_DRAG(id, func) Begin dragging with the left mouse button.
EVT_TREE_BEGIN_RDRAG(id, func) Begin dragging with the right mouse button.
EVT_TREE_END_DRAG(id, func) Drag ended (drop).
EVT_TREE_BEGIN_LABEL_EDIT(id, func) Begin editing a label. This can be prevented by calling Veto().
EVT_TREE_END_LABEL_EDIT(id, func) Finish editing a label. This can be prevented by calling Veto().
EVT_TREE_DELETE_ITEM(id, func) Delete an item.
EVT_TREE_GET_INFO(id, func) Request information from the application.
EVT_TREE_SET_INFO(id, func) Information is being supplied.
EVT_TREE_ITEM_ACTIVATED(id, func) The item has been activated, i.e. chosen by double clicking it with mouse or from keyboard
EVT_TREE_ITEM_COLLAPSED(id, func) Parent has been collapsed.
EVT_TREE_ITEM_COLLAPSING(id, func) Parent is being collapsed. This can be prevented by calling Veto().
EVT_TREE_ITEM_EXPANDED(id, func) Parent has been expanded.
EVT_TREE_ITEM_EXPANDING(id, func) Parent is being expanded. This can be prevented by calling Veto().
EVT_TREE_SEL_CHANGED(id, func) Selection has changed.
EVT_TREE_SEL_CHANGING(id, func) Selection is changing. This can be prevented by calling Veto().
EVT_TREE_KEY_DOWN(id, func) A key has been pressed.
Note that to allow drag and drop in the tree control you must explicitly call Allow() in the event handler, by default dragging is disabled. On the other hand, label editing is allowed by default for the controls with wxTR_EDIT_LABELS style but you can call Veto() to prevent it from happening.

See also

wxTreeItemData, wxTreeCtrl overview, wxListBox, wxListCtrl, wxImageList, wxTreeEvent

Win32 notes

wxTreeCtrl class uses the standard common treeview control under Win32 implemented in the system library comctl32.dll. Some versions of this library are known to have bugs with handling the tree control colours: the usual symptom is that the expanded items leave black (or otherwise incorrectly coloured) background behind them, especially for the controls using non default background colour. The recommended solution is to upgrade the comctl32.dll to a newer version: see http://www.microsoft.com/msdownload/ieplatform/ie/comctrlx86.asp.

Members

wxTreeCtrl::wxTreeCtrl
wxTreeCtrl::~wxTreeCtrl
wxTreeCtrl::AddRoot
wxTreeCtrl::AppendItem
wxTreeCtrl::Collapse
wxTreeCtrl::CollapseAndReset
wxTreeCtrl::Create
wxTreeCtrl::Delete
wxTreeCtrl::DeleteAllItems
wxTreeCtrl::EditLabel
wxTreeCtrl::EndEditLabel
wxTreeCtrl::EnsureVisible
wxTreeCtrl::Expand
wxTreeCtrl::GetBoundingRect
wxTreeCtrl::GetChildrenCount
wxTreeCtrl::GetCount
wxTreeCtrl::GetEditControl
wxTreeCtrl::GetFirstChild
wxTreeCtrl::GetFirstVisibleItem
wxTreeCtrl::GetImageList
wxTreeCtrl::GetIndent
wxTreeCtrl::GetItemData
wxTreeCtrl::GetItemImage
wxTreeCtrl::GetItemText
wxTreeCtrl::GetLastChild
wxTreeCtrl::GetNextChild
wxTreeCtrl::GetNextSibling
wxTreeCtrl::GetNextVisible
wxTreeCtrl::GetParent
wxTreeCtrl::GetPrevSibling
wxTreeCtrl::GetPrevVisible
wxTreeCtrl::GetRootItem
wxTreeCtrl::GetItemSelectedImage
wxTreeCtrl::GetSelection
wxTreeCtrl::GetSelections
wxTreeCtrl::GetStateImageList
wxTreeCtrl::HitTest
wxTreeCtrl::InsertItem
wxTreeCtrl::IsBold
wxTreeCtrl::IsExpanded
wxTreeCtrl::IsSelected
wxTreeCtrl::IsVisible
wxTreeCtrl::ItemHasChildren
wxTreeCtrl::OnCompareItems
wxTreeCtrl::PrependItem
wxTreeCtrl::ScrollTo
wxTreeCtrl::SelectItem
wxTreeCtrl::SetIndent
wxTreeCtrl::SetImageList
wxTreeCtrl::SetItemBackgroundColour
wxTreeCtrl::SetItemBold
wxTreeCtrl::SetItemData
wxTreeCtrl::SetItemFont
wxTreeCtrl::SetItemHasChildren
wxTreeCtrl::SetItemImage
wxTreeCtrl::SetItemSelectedImage
wxTreeCtrl::SetItemText
wxTreeCtrl::SetItemTextColour
wxTreeCtrl::SetStateImageList
wxTreeCtrl::SortChildren
wxTreeCtrl::Toggle
wxTreeCtrl::Unselect
wxTreeCtrl::UnselectAll


wxTreeCtrl::wxTreeCtrl

wxTreeCtrl()

Default constructor.

wxTreeCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxTR_HAS_BUTTONS, const wxValidator& validator = wxDefaultValidator, const wxString& name = "listCtrl")

Constructor, creating and showing a tree control.

Parameters

parent

id

pos

size

style

validator

name

See also

wxTreeCtrl::Create, wxValidator


wxTreeCtrl::~wxTreeCtrl

void ~wxTreeCtrl()

Destructor, destroying the list control.


wxTreeCtrl::AddRoot

wxTreeItemId AddRoot(const wxString& text, int image = -1, int selImage = -1, wxTreeItemData* data = NULL)

Adds the root node to the tree, returning the new item.

If image > -1 and selImage is -1, the same image is used for both selected and unselected items.


wxTreeCtrl::AppendItem

wxTreeItemId AppendItem(const wxTreeItemId& parent, const wxString& text, int image = -1, int selImage = -1, wxTreeItemData* data = NULL)

Appends an item to the end of the branch identified by parent, return a new item id.

If image > -1 and selImage is -1, the same image is used for both selected and unselected items.


wxTreeCtrl::Collapse

void Collapse(const wxTreeItemId& item)

Collapses the given item.


wxTreeCtrl::CollapseAndReset

void CollapseAndReset(const wxTreeItemId& item)

Collapses the given item and removes all children.


wxTreeCtrl::Create

bool wxTreeCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxTR_HAS_BUTTONS, const wxValidator& validator = wxDefaultValidator, const wxString& name = "listCtrl")

Creates the tree control. See wxTreeCtrl::wxTreeCtrl for further details.


wxTreeCtrl::Delete

void Delete(const wxTreeItemId& item)

Deletes the specified item.


wxTreeCtrl::DeleteAllItems

void DeleteAllItems()

Deletes all the items in the control.


wxTreeCtrl::EditLabel

void EditLabel(const wxTreeItemId& item)

Starts editing the label of the given item. This function generates a EVT_TREE_BEGIN_LABEL_EDIT event which can be vetoed so that no text control will appear for in-place editing.

If the user changed the label (i.e. s/he does not press ESC or leave the text control without changes, a EVT_TREE_END_LABEL_EDIT event will be sent which can be vetoed as well.

See also

wxTreeCtrl::EndEditLabel, wxTreeEvent


wxTreeCtrl::EndEditLabel

void EndEditLabel(bool cancelEdit)

Ends label editing. If cancelEdit is TRUE, the edit will be cancelled.

This function is currently supported under Windows only.

See also

wxTreeCtrl::EditLabel


wxTreeCtrl::EnsureVisible

void EnsureVisible(const wxTreeItemId& item)

Scrolls and/or expands items to ensure that the given item is visible.


wxTreeCtrl::Expand

void Expand(const wxTreeItemId& item)

Expands the given item.


wxTreeCtrl::GetBoundingRect

bool GetBoundingRect(const wxTreeItemId& item, wxRect& rect, bool textOnly = FALSE) const

Retrieves the rectangle bounding the item. If textOnly is TRUE, only the rectangle around the items label will be returned, otherwise the items image is also taken into account.

The return value is TRUE if the rectangle was successfully retrieved or FALSE if it was not (in this case rect is not changed) - for example, if the item is currently invisible.

wxPython note: The wxPython version of this method requires only the item and textOnly parameters. The return value is either a wxRect object or None.


wxTreeCtrl::GetChildrenCount

size_t GetChildrenCount(const wxTreeItemId& item, bool recursively = TRUE) const

Returns the number of items in the branch. If recursively is TRUE, returns the total number of descendants, otherwise only one level of children is counted.


wxTreeCtrl::GetCount

int GetCount() const

Returns the number of items in the control.


wxTreeCtrl::GetEditControl

wxTextCtrl& GetEditControl() const

Returns the edit control used to edit a label.


wxTreeCtrl::GetFirstChild

wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& cookie) const

Returns the first child; call wxTreeCtrl::GetNextChild for the next child.

For this enumeration function you must pass in a 'cookie' parameter which is opaque for the application but is necessary for the library to make these functions reentrant (i.e. allow more than one enumeration on one and the same object simultaneously). The cookie passed to GetFirstChild and GetNextChild should be the same.

Returns an invalid tree item if there are no further children.

See also

wxTreeCtrl::GetNextChild

wxPython note: In wxPython the returned wxTreeItemId and the new cookie value are both returned as a tuple containing the two values.


wxTreeCtrl::GetFirstVisibleItem

wxTreeItemId GetFirstVisibleItem() const

Returns the first visible item.


wxTreeCtrl::GetImageList

wxImageList* GetImageList() const

Returns the normal image list.


wxTreeCtrl::GetIndent

int GetIndent() const

Returns the current tree control indentation.


wxTreeCtrl::GetItemData

wxTreeItemData* GetItemData(const wxTreeItemId& item) const

Returns the tree item data associated with the item.

See also

wxTreeItemData

wxPython note: wxPython provides the following shortcut method:


wxTreeCtrl::GetItemImage

int GetItemImage(const wxTreeItemId& item, wxTreeItemIcon which = wxTreeItemIcon_Normal) const

Gets the specified item image. The value of which may be:


wxTreeCtrl::GetItemText

wxString GetItemText(const wxTreeItemId& item) const

Returns the item label.


wxTreeCtrl::GetLastChild

wxTreeItemId GetLastChild(const wxTreeItemId& item) const

Returns the last child of the item (or an invalid tree item if this item has no children).

See also

GetFirstChild, GetLastChild


wxTreeCtrl::GetNextChild

wxTreeItemId GetNextChild(const wxTreeItemId& item, long& cookie) const

Returns the next child; call wxTreeCtrl::GetFirstChild for the first child.

For this enumeration function you must pass in a 'cookie' parameter which is opaque for the application but is necessary for the library to make these functions reentrant (i.e. allow more than one enumeration on one and the same object simultaneously). The cookie passed to GetFirstChild and GetNextChild should be the same.

Returns an invalid tree item if there are no further children.

See also

wxTreeCtrl::GetFirstChild

wxPython note: In wxPython the returned wxTreeItemId and the new cookie value are both returned as a tuple containing the two values.


wxTreeCtrl::GetNextSibling

wxTreeItemId GetNextSibling(const wxTreeItemId& item) const

Returns the next sibling of the specified item; call wxTreeCtrl::GetPrevSibling for the previous sibling.

Returns an invalid tree item if there are no further siblings.

See also

wxTreeCtrl::GetPrevSibling


wxTreeCtrl::GetNextVisible

wxTreeItemId GetNextVisible(const wxTreeItemId& item) const

Returns the next visible item.


wxTreeCtrl::GetParent

wxTreeItemId GetParent(const wxTreeItemId& item) const

Returns the item's parent.

wxPython note: This method is named GetItemParent to avoid a name clash with wxWindow::GetParent.


wxTreeCtrl::GetPrevSibling

wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const

Returns the previous sibling of the specified item; call wxTreeCtrl::GetNextSibling for the next sibling.

Returns an invalid tree item if there are no further children.

See also

wxTreeCtrl::GetNextSibling


wxTreeCtrl::GetPrevVisible

wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const

Returns the previous visible item.


wxTreeCtrl::GetRootItem

wxTreeItemId GetRootItem() const

Returns the root item for the tree control.


wxTreeCtrl::GetItemSelectedImage

int GetItemSelectedImage(const wxTreeItemId& item) const

Gets the selected item image (this function is obsolete, use GetItemImage(item, wxTreeItemIcon_Selected) instead).


wxTreeCtrl::GetSelection

wxTreeItemId GetSelection() const

Returns the selection, or an invalid item if there is no selection. This function only works with the controls without wxTR_MULTIPLE style, use GetSelections for the controls which do have this style.


wxTreeCtrl::GetSelections

size_t GetSelections(wxArrayTreeItemIds& selection) const

Fills the array of tree items passed in with the currently selected items. This function can be called only if the control has the wxTR_MULTIPLE style.

Returns the number of selected items.

wxPython note: The wxPython version of this method accepts no parameters and returns a Python list of wxTreeItemIds.


wxTreeCtrl::GetStateImageList

wxImageList* GetStateImageList() const

Returns the state image list (from which application-defined state images are taken).


wxTreeCtrl::HitTest

wxTreeItemId HitTest(const wxPoint& point, int& flags)

Calculates which (if any) item is under the given point, returning the tree item id at this point plus extra information flags. flags is a bitlist of the following:

wxTREE_HITTEST_ABOVE Above the client area.
wxTREE_HITTEST_BELOW Below the client area.
wxTREE_HITTEST_NOWHERE In the client area but below the last item.
wxTREE_HITTEST_ONITEMBUTTON On the button associated with an item.
wxTREE_HITTEST_ONITEMICON On the bitmap associated with an item.
wxTREE_HITTEST_ONITEMINDENT In the indentation associated with an item.
wxTREE_HITTEST_ONITEMLABEL On the label (string) associated with an item.
wxTREE_HITTEST_ONITEMRIGHT In the area to the right of an item.
wxTREE_HITTEST_ONITEMSTATEICON On the state icon for a tree view item that is in a user-defined state.
wxTREE_HITTEST_TOLEFT To the right of the client area.
wxTREE_HITTEST_TORIGHT To the left of the client area.

wxPython note: in wxPython both the wxTreeItemId and the flags are returned as a tuple.


wxTreeCtrl::InsertItem

wxTreeItemId InsertItem(const wxTreeItemId& parent, const wxTreeItemId& previous, const wxString& text, int image = -1, int selImage = -1, wxTreeItemData* data = NULL)

wxTreeItemId InsertItem(const wxTreeItemId& parent, size_t before, const wxString& text, int image = -1, int selImage = -1, wxTreeItemData* data = NULL)

Inserts an item after a given one (previous) or before one identified by its position (before).

If image > -1 and selImage is -1, the same image is used for both selected and unselected items.

wxPython note: The second form of this method is called InsertItemBefore in wxPython.


wxTreeCtrl::IsBold

bool IsBold(const wxTreeItemId& item) const

Returns TRUE if the given item is in bold state.

See also: SetItemBold


wxTreeCtrl::IsExpanded

bool IsExpanded(const wxTreeItemId& item) const

Returns TRUE if the item is expanded (only makes sense if it has children).


wxTreeCtrl::IsSelected

bool IsSelected(const wxTreeItemId& item) const

Returns TRUE if the item is selected.


wxTreeCtrl::IsVisible

bool IsVisible(const wxTreeItemId& item) const

Returns TRUE if the item is visible (it might be outside the view, or not expanded).


wxTreeCtrl::ItemHasChildren

bool ItemHasChildren(const wxTreeItemId& item) const

Returns TRUE if the item has children.


wxTreeCtrl::OnCompareItems

int OnCompareItems(const wxTreeItemId& item1, const wxTreeItemId& item2)

Override this function in the derived class to change the sort order of the items in the tree control. The function should return a negative, zero or positive value if the first item is less than, equal to or greater than the second one.

The base class version compares items alphabetically.

See also: SortChildren


wxTreeCtrl::PrependItem

wxTreeItemId PrependItem(const wxTreeItemId& parent, const wxString& text, int image = -1, int selImage = -1, wxTreeItemData* data = NULL)

Appends an item as the first child of parent, return a new item id.

If image > -1 and selImage is -1, the same image is used for both selected and unselected items.


wxTreeCtrl::ScrollTo

void ScrollTo(const wxTreeItemId& item)

Scrolls the specified item into view.


wxTreeCtrl::SelectItem

bool SelectItem(const wxTreeItemId& item)

Selects the given item.


wxTreeCtrl::SetIndent

void SetIndent(int indent)

Sets the indentation for the tree control.


wxTreeCtrl::SetImageList

void SetImageList(wxImageList* imageList)

Sets the normal image list.


wxTreeCtrl::SetItemBackgroundColour

void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col)

Sets the colour of the items background.


wxTreeCtrl::SetItemBold

void SetItemBold(const wxTreeItemId& item, bool bold = TRUE)

Makes item appear in bold font if bold parameter is TRUE or resets it to the normal state.

See also: IsBold


wxTreeCtrl::SetItemData

void SetItemData(const wxTreeItemId& item, wxTreeItemData* data)

Sets the item client data.

wxPython note: wxPython provides the following shortcut method:


wxTreeCtrl::SetItemFont

void SetItemFont(const wxTreeItemId& item, const wxFont& font)

Sets the items font. All items in the tree should have the same height to avoid text clipping, so the fonts height should be the same for all of them, although font attributes may vary.

See also

SetItemBold


wxTreeCtrl::SetItemHasChildren

void SetItemHasChildren(const wxTreeItemId& item, bool hasChildren = TRUE)

Force appearance of the button next to the item. This is useful to allow the user to expand the items which don't have any children now, but instead adding them only when needed, thus minimizing memory usage and loading time.


wxTreeCtrl::SetItemImage

void SetItemImage(const wxTreeItemId& item, int image, wxTreeItemIcon which = wxTreeItemIcon_Normal)

Sets the specified item image. See GetItemImage for the description of the which parameter.


wxTreeCtrl::SetItemSelectedImage

void SetItemSelectedImage(const wxTreeItemId& item, int selImage)

Sets the selected item image (this function is obsolete, use SetItemImage(item, wxTreeItemIcon_Selected) instead).


wxTreeCtrl::SetItemText

void SetItemText(const wxTreeItemId& item, const wxString& text)

Sets the item label.


wxTreeCtrl::SetItemTextColour

void SetItemTextColour(const wxTreeItemId& item, const wxColour& col)

Sets the colour of the items text.


wxTreeCtrl::SetStateImageList

void SetStateImageList(wxImageList* imageList)

Sets the state image list (from which application-defined state images are taken).


wxTreeCtrl::SortChildren

void SortChildren(const wxTreeItemId& item)

Sorts the children of the given item using OnCompareItems method of wxTreeCtrl. You should override that method to change the sort order (the default is ascending alphabetical order).

See also

wxTreeItemData, OnCompareItems


wxTreeCtrl::Toggle

void Toggle(const wxTreeItemId& item)

Toggles the given item between collapsed and expanded states.


wxTreeCtrl::Unselect

void Unselect()

Removes the selection from the currently selected item (if any).


wxTreeCtrl::UnselectAll

void UnselectAll()

This function either behaves the same as Unselect if the control doesn't have wxTR_MULTIPLE style, or removes the selection from all items if it does have this style.