wxRuby Documentation Home

Wx::TreeCtrl

A tree control presents information as a hierarchy, with items that may
be expanded to show further items. Desktop OSes commonly make use of
this type of control to present a navigable representation of folders
and files on a hard drive.

Derived from

Control

Window

EvtHandler

Object

Window styles

The behaviour and appearance of a TreeCtrl can be modified in several
ways by applying styles when constructing the widget.

TR_EDIT_LABELS Use this style if you wish the user to be able to edit labels in the tree control.
TR_NO_BUTTONS For convenience to document that no buttons are to be drawn.
TR_HAS_BUTTONS Use this style to show + and – buttons to the left of parent items.
TR_NO_LINES Use this style to hide vertical level connectors.
TR_FULL_ROW_HIGHLIGHT Use this style to have the background colour and the selection highlight extend over the entire horizontal row of the tree control window. (This flag is ignored under Windows unless you specify TR_NO_LINES as well.)
TR_LINES_AT_ROOT Use this style to show lines between root nodes. Only applicable if TR_HIDE_ROOT is set and TR_NO_LINES is not set.
TR_HIDE_ROOT Use this style to suppress the display of the root node,effectively causing the first-level nodes to appear as a series of root nodes.
TR_ROW_LINES Use this style to draw a contrasting border between displayed rows.
TR_HAS_VARIABLE_ROW_HEIGHT Use this style to cause row heights to be just big enough to fit the content. If not set, all rows use the largest row height.The default is that this flag is unset.Generic only.
TR_SINGLE For convenience to document that only one item may be selected at a time. Selecting another item causes the current selection, if any,to be deselected. This is the default.
TR_MULTIPLE Use this style to allow a range of items to be selected.If a second range is selected, the current range, if any, is deselected.
TR_EXTENDED Use this style to allow disjoint items to be selected. (Only partially implemented; may not work in all cases.)
TR_DEFAULT_STYLE The set of flags that are closest to the defaults for the native control for a particular toolkit.

See also window styles overview.

Event hooks

TreeCtrls can generate a large number of events, reflecting the variety
of ways in which users can interact with such controls. Handled events
include selecting items, expanding branches, dragging items, and editing
item labels as well as standard mouse and keyboard interactions. To
intercept events from a tree control, use the event table macros
described below and in TreeEvent.

To process input from a tree control, use these event methods to direct
input to blocks that take a TreeEvent argument.

evt_tree_begin_drag(id) { | event | … } Begin dragging with the left mouse button.
evt_tree_begin_rdrag(id) { | event | … } Begin dragging with the right mouse button.
evt_tree_end_drag(id) { | event | … } End dragging with the left or right mouse button.
evt_tree_begin_label_edit(id) { | event | … } Begin editing a label. This can be prevented by calling veto.
evt_tree_end_label_edit(id) { | event | … } Finish editing a label. This can be prevented by calling veto.
evt_tree_delete_item(id) { | event | … } Delete an item.
evt_tree_get_info(id) { | event | … } Request information from the application.
evt_tree_set_info(id) { | event | … } Information is being supplied.
evt_tree_item_activated(id) { | event | … } The item has been activated, i.e. chosen by double clicking it with mouse or from keyboard
evt_tree_item_collapsed(id) { | event | … } The item has been collapsed.
evt_tree_item_collapsing(id) { | event | … } The item is being collapsed. This can be prevented by calling veto.
evt_tree_item_expanded(id) { | event | … } The item has been expanded.
evt_tree_item_expanding(id) { | event | … } The item is being expanded. This can be prevented by calling veto.
evt_tree_item_right_click(id) { | event | … } The user has clicked the item with the right mouse button.
evt_tree_item_middle_click(id) { | event | … } The user has clicked the item with the middle mouse button.
evt_tree_sel_changed(id) { | event | … } Selection has changed.
evt_tree_sel_changing(id) { | event | … } Selection is changing. This can be prevented by calling veto.
evt_tree_key_down(id) { | event | … } A key has been pressed.
evt_tree_item_gettooltip(id) { | event | … } The opportunity to set the item tooltipis being given to the application (call TreeEvent::SetToolTip). Windows only.
evt_tree_item_menu(id) { | event | … } The context menu for the selected item has been requested, either by a right click or by using the menu key.
evt_tree_state_image_click(id) { | event | … } The state image has been clicked. Windows only.

See also

TreeItemData, ListBox, ListCtrl, ImageList, TreeEvent

Working with TreeCtrl items

A TreeCtrl contains one, or more usually, many items. As well as the
text label and parent, tree items can have several other properties,
such as being bold or italic, and icons for expanded and collapsed
states.

In wxRuby, individual items (including branches) in a tree control are
referenced by TreeItemIds. These ids are returned by methods such as
append_item which add items to a tree, and by
TreeEvents which can be queried to find which item is being acted
upon. The ids are passed in as arguments to methods which modify items
within the tree, such as set_item_text. This
class includes Ruby’s Enumerable module, so methods such as find,
collect and select can be used to iterate over the control’s contents.

TreeItemIds are Integers, but the number itself is an opaque identifier;
you should not carry out arithmetic on it, or assume anything about the
id of one item relative to another. The only useful operation that can
be carried out on a TreeItemId is to test whether it refers to a valid
item. If the id is equal to zero, then the id does not refer to a valid
item. This is most useful when testing whether any item has been clicked
as part of a TreeEvent handler; for example:

def on_tree_clicked(event) if event.get_item.nonzero?
  1. .. an item was clicked, make it bold
    set_item_bold(event.get_item)
    else
  2. otherwise the click didn’t happen over an item

Associating Ruby objects with TreeCtrl items

TreeCtrls can be linked to non-GUI application objects by using the
item_data methods. Methods which add items to the tree all accept an
optional item_data argument, which may be any normal Ruby
object. Alternatively, a Ruby object can be explicitly associated with a
TreeCtrl item by calling set_item_data . The
same Ruby object can later be retrieved from a TreeCtrlItemId by calling
get_item_data .

Win32 notes

TreeCtrl 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/downloads/release.asp?ReleaseID=11916.

Methods

TreeCtrl.new

TreeCtrl.new(%(arg-type)Window% parent, Integer id, Point pos = DEFAULT_POSITION, Size size = DEFAULT_SIZE, Integer style = TR_HAS_BUTTONS, Validator validator = DEFAULT_VALIDATOR, String name = “treeCtrl”)

Constructor, creating and showing a tree control.

Parameters

See also

TreeCtrl#create, Validator

TreeCtrl#add_root

Integer add_root(%(arg-type)String% text, Integer image = -1, Integer selImage = -1, Object item_data = nil)

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

The image and selImage parameters are an index within
the normal image list specifying the image to use for unselected and
selected items, respectively.
If image > -1 and selImage is -1, the same image is used for
both selected and unselected items.

TreeCtrl#append_item

Integer append_item(%(arg-type)Integer% parent, String text, Integer image = -1, Integer selImage = -1, Object item_data = nil)

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

The image and selImage parameters are an index within
the normal image list specifying the image to use for unselected and
selected items, respectively.
If image > -1 and selImage is -1, the same image is used for
both selected and unselected items.

TreeCtrl#collapse_all

collapse_all()

Collapses the root item.

See also

expand_all

TreeCtrl#collapse_all_children

collapse_all_children(%(arg-type)Integer% item)

Collapses this item and all of its children, recursively.

See also

expand_all_children

TreeCtrl#collapse_and_reset

collapse_and_reset(%(arg-type)Integer% item)

Collapses the given item and removes all children.

TreeCtrl#create

Boolean TreeCtrl.new(%(arg-type)Window% parent, Integer id, Point pos = DEFAULT_POSITION, Size size = DEFAULT_SIZE, Integer style = TR_HAS_BUTTONS, Validator validator = DEFAULT_VALIDATOR, String name = “treeCtrl”)

Creates the tree control. See TreeCtrl.new for further details.

TreeCtrl#delete

delete(%(arg-type)Integer% item)

Deletes the specified item. A EVT_TREE_DELETE_ITEM event will be
generated.

This function may cause a subsequent call to GetNextChild to fail.

TreeCtrl#delete_all_items

delete_all_items()

Deletes all items in the control. Note that this may not generate
EVT_TREE_DELETE_ITEM events under some Windows versions although
normally such event is generated for each removed item.

TreeCtrl#delete_children

delete_children(%(arg-type)Integer% item)

Deletes all children of the given item (but not the item itself). Note that
this will not generate any events unlike
Delete method.

If you have called TreeCtrl#set_item_has_children, you
may need to call it again since DeleteChildren does not automatically
clear the setting.

TreeCtrl#each

each() { | item_id | … }

Passes the item id of each and every item in the TreeCtrl into the
passed block, allowing iteration of the control’s contents.

TreeCtrl#edit_label

edit_label(%(arg-type)Integer% 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

TreeCtrl#end_edit_label,
TreeEvent

TreeCtrl#end_edit_label

end_edit_label(%(arg-type)Integer% item, Boolean cancelEdit)

Ends the editing of the label of the given item. If cancelEdit is
true, the edit will be cancelled.

The item argument is not important under Windows, where it always
defaults to the item currently being editing. Having to specify the item
is a necessary irritation on Linux / OS X, but if developing for Windows
only, the argument can have any value.

See also

TreeCtrl#edit_label

TreeCtrl#ensure_visible

ensure_visible(%(arg-type)Integer% item)

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

TreeCtrl#expand

expand(%(arg-type)Integer% item)

Expands the given item.

TreeCtrl#expand_all

expand_all()

Expands all items in the tree.

TreeCtrl#expand_all_children

expand_all_children(%(arg-type)Integer% item)

Expands the given item and all its children recursively.

TreeCtrl#get_buttons_image_list

ImageList get_buttons_image_list()

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

This function is only available in the generic version.

TreeCtrl#get_children

Array get_children(%(arg-type)Integer% item)

Returns all of the children of the item as an Array of Tree Item Ids.

TreeCtrl#get_children_count

Integer get_children_count(%(arg-type)Integer% item, Boolean recursively = true)

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.

TreeCtrl#get_count

unsigned int get_count()

Returns the number of items in the control.

TreeCtrl#get_edit_control

TextCtrl get_edit_control()

Returns the edit control being currently used to edit a label. Returns NULL
if no label is being edited.

NB: It is currently only implemented for MSW.

TreeCtrl#get_first_child

Integer get_first_child(%(arg-type)Integer% item)

Returns the id of the first child of an item. Returns 0 if the item
has no children. Use get_next_child, to
iterate through the remaining children.

TreeCtrl#get_first_visible_item

Integer get_first_visible_item()

Returns the first visible item.

TreeCtrl#get_image_list

ImageList get_image_list()

Returns the normal image list.

TreeCtrl#get_indent

Integer get_indent()

Returns the current tree control indentation.

TreeCtrl#get_item_background_colour

Colour get_item_background_colour(%(arg-type)Integer% item)

Returns the background colour of the item.

TreeCtrl#get_item_data

Object get_item_data(%(arg-type)Integer% item)

Returns the tree item data associated with the item.

TreeCtrl#get_item_font

Font get_item_font(%(arg-type)Integer% item)

Returns the font of the item label.

TreeCtrl#get_item_image

Integer get_item_image(%(arg-type)Integer% item, int which = Wx::TREE_ITEM_ICON_NORMAL)

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

TreeCtrl#get_item_rect

Rect get_item_rect(%(arg-type)Integer% item)

Retrieves the pixel location and size of a tree item, including any
image or button. Returns a the rectangle bounding the
item, or nil if the item is not valid, for example, if it is
currently invisible within a collapsed parent.

Notice that the rectangle coordinates are logical, not physical
ones. So, for example, the x coordinate may be negative if the tree has
a horizontal scrollbar and its position is not 0.

See also get_label_rect.

TreeCtrl#get_item_text

String get_item_text(%(arg-type)Integer% item)

Returns the item label.

TreeCtrl#get_item_text_colour

Colour get_item_text_colour(%(arg-type)Integer% item)

Returns the colour of the item label.

TreeCtrl#get_label_rect

Rect get_label_rect(%(arg-type)Integer% item)

Retrieves the pixel location and size of a tree item’s label. Returns a
the rectangle bounding the item label, or nil if the item is
not valid, for example, if it is currently invisible within a collapsed
parent.

Notice that the rectangle coordinates are logical, not physical
ones. So, for example, the x coordinate may be negative if the tree has
a horizontal scrollbar and its position is not 0.

See also get_item_rect.

TreeCtrl#get_last_child

Integer get_last_child(%(arg-type)Integer% item)

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

See also

get_first_child,
TreeCtrl#get_next_sibling,
get_last_child

TreeCtrl#get_next_sibling

Integer get_next_sibling(%(arg-type)Integer% item)

Returns the next sibling of the specified item; call TreeCtrl#get_prev_sibling for the previous sibling.

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

See also

TreeCtrl#get_prev_sibling

TreeCtrl#get_next_visible

Integer get_next_visible(%(arg-type)Integer% item)

Returns the next visible item.

TreeCtrl#get_item_parent

Integer get_item_parent(%(arg-type)Integer% item)

Returns the item’s parent.

TreeCtrl#get_prev_sibling

Integer get_prev_sibling(%(arg-type)Integer% item)

Returns the previous sibling of the specified item; call TreeCtrl#get_next_sibling for the next sibling.

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

See also

TreeCtrl#get_next_sibling

TreeCtrl#get_prev_visible

Integer get_prev_visible(%(arg-type)Integer% item)

Returns the previous visible item.

TreeCtrl#get_quick_best_size

Boolean get_quick_best_size()

Returns true if the control will use a quick calculation for the best size,
looking only at the first and last items. The default is false.

See also

TreeCtrl#set_quick_best_size

TreeCtrl#get_root_item

Integer get_root_item()

Returns the root item for the tree control. Note that if the tree was
created with a hidden root (using the TR_HIDE_ROOT style), this will
return an invalid item id in which cannot be used in other calls (for
example, get_children). See also get_root_items, below.

TreeCtrl#get_root_items

Array get_root_items()

This returns an array of ids of the base items in the tree. It is
intended primarily for use with TreeCtrls with the TR_HIDDEN_ROOT
style applied, for which get_root_item, above, will not work correctly.

TreeCtrl#get_item_selected_image

Integer get_item_selected_image(%(arg-type)Integer% item_id)

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

TreeCtrl#get_selection

Integer get_selection()

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

TreeCtrl#get_selections

Array get_selections()

Returns an array of tree item ids for the currently selected items. This
function can be called only if the control has the TR_MULTIPLE style.

TreeCtrl#get_state_image_list

ImageList get_state_image_list()

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

TreeCtrl#hit_test

Integer hit_test(%(arg-type)Point% point, Integer 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:

TREE_HITTEST_ABOVE Above the client area.
TREE_HITTEST_BELOW Below the client area.
TREE_HITTEST_NOWHERE In the client area but below the last item.
TREE_HITTEST_ONITEMBUTTON On the button associated with an item.
TREE_HITTEST_ONITEMICON On the bitmap associated with an item.
TREE_HITTEST_ONITEMINDENT In the indentation associated with an item.
TREE_HITTEST_ONITEMLABEL On the label (string) associated with an item.
TREE_HITTEST_ONITEMRIGHT In the area to the right of an item.
TREE_HITTEST_ONITEMSTATEICON On the state icon for a tree view item that is in a user-defined state.
TREE_HITTEST_TOLEFT To the right of the client area.
TREE_HITTEST_TORIGHT To the left of the client area.

TreeCtrl#insert_item

Integer insert_item(%(arg-type)Integer% parent,
Integer previous, String text,
Integer image = -1,
Integer selImage = -1,
Object item_data = nil)

Inserts an item after a given one (previous).

The image and selImage parameters are an index within
the normal image list specifying the image to use for unselected and
selected items, respectively.
If image > -1 and selImage is -1, the same image is used for
both selected and unselected items.

TreeCtrl#insert_item_before

Integer insert_item_before(%(arg-type)Integer% parent,
Integer before, String text,
Integer image = -1,
Integer selImage = -1,
Object item_data = nil)

Inserts an item before one identified by its position (before).
before must be less than the number of children.

The image and selImage parameters are an index within
the normal image list specifying the image to use for unselected and
selected items, respectively.
If image > -1 and selImage is -1, the same image is used for
both selected and unselected items.

TreeCtrl#is_bold

Boolean is_bold(%(arg-type)Integer% item)

Returns true if the given item is in bold state.

See also: set_item_bold

TreeCtrl#is_empty

Boolean is_empty()

Returns if the control is empty (i.e. has no items, even no root one).

TreeCtrl#is_expanded

Boolean is_expanded(%(arg-type)Integer% item)

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

TreeCtrl#is_selected

Boolean is_selected(%(arg-type)Integer% item)

Returns true if the item is selected.

TreeCtrl#is_visible

Boolean is_visible(%(arg-type)Integer% item)

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

TreeCtrl#item_has_children

Boolean item_has_children(%(arg-type)Integer% item)

Returns true if the item has children.

TreeCtrl#on_compare_items

Integer on_compare_items(%(arg-type)Integer% item1, Integer 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.

See also: sort_children

TreeCtrl#prepend_item

Integer prepend_item(%(arg-type)Integer% parent, String text,
Integer image = -1,
Integer selImage = -1,
Object item_data = nil)

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

The image and selImage parameters are an index within
the normal image list specifying the image to use for unselected and
selected items, respectively.
If image > -1 and selImage is -1, the same image is used for
both selected and unselected items.

TreeCtrl#scroll_to

scroll_to(%(arg-type)Integer% item)

Scrolls the specified item into view.

TreeCtrl#select_item

select_item(%(arg-type)Integer% item, Boolean select = )

Selects the given item. In multiple selection controls, can be also used to
deselect a currently selected item if the value of select is false.

TreeCtrl#set_buttons_image_list

set_buttons_image_list(%(arg-type)ImageList% imageList)

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

Setting the button image list enables the display of image buttons.
Once enabled, the only way to disable the display of button images is to
set the button image list to nil.

This function is only available in the generic version.

TreeCtrl#set_indent

set_indent(%(arg-type)Integer% indent)

Sets the indentation for the tree control.

TreeCtrl#set_image_list

set_image_list(%(arg-type)ImageList% imageList)

Sets the normal image list.

TreeCtrl#set_item_background_colour

set_item_background_colour(%(arg-type)Integer% item, Colour col)

Sets the colour of the item’s background.

TreeCtrl#set_item_bold

set_item_bold(%(arg-type)Integer% item, Boolean bold = true)

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

See also: is_bold

TreeCtrl#set_item_data

set_item_data(%(arg-type)Integer% item, Object data)

Sets the item client data.

TreeCtrl#set_item_drop_highlight

set_item_drop_highlight(%(arg-type)Integer% item, Boolean highlight = true)

Gives the item the visual feedback for Drag’n’Drop actions, which is
useful if something is dragged from the outside onto the tree control
(as opposed to a DnD operation within the tree control, which already
is implemented internally).

TreeCtrl#set_item_font

set_item_font(%(arg-type)Integer% item, Font font)

Sets the item’s 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

set_item_bold

TreeCtrl#set_item_has_children

set_item_has_children(%(arg-type)Integer% item, Boolean 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.

TreeCtrl#set_item_image

set_item_image(%(arg-type)Integer% item, Integer image, TreeItemIcon which = TreeItemIcon_Normal)

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

TreeCtrl#set_item_selected_image

set_item_selected_image(%(arg-type)Integer% item, Integer selImage)

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

TreeCtrl#set_item_text

set_item_text(%(arg-type)Integer% item, String text)

Sets the item label.

TreeCtrl#set_item_text_colour

set_item_text_colour(%(arg-type)Integer% item, Colour col)

Sets the colour of the item’s text.

TreeCtrl#set_quick_best_size

set_quick_best_size(%(arg-type)Boolean% quickBestSize)

If true is passed, specifies that the control will use a quick calculation for the best size,
looking only at the first and last items. Otherwise, it will look at all items.
The default is false.

See also

TreeCtrl#get_quick_best_size

TreeCtrl#set_state_image_list

set_state_image_list(%(arg-type)ImageList% imageList)

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

TreeCtrl#set_window_style

set_window_style(%(arg-type)Integer% styles)

Sets the mode flags associated with the display of the tree control.
The new mode takes effect immediately.
(Generic only; MSW ignores changes.)

TreeCtrl#sort_children

sort_children(%(arg-type)Integer% item)

Sorts the children of the given item using
on_compare_items method of TreeCtrl. You
should override that method to change the sort order (the default is ascending
case-sensitive alphabetical order).

See also

on_compare_items

TreeCtrl#toggle

toggle(%(arg-type)Integer% item)

Toggles the given item between collapsed and expanded states.

TreeCtrl#toggle_item_selection

toggle_item_selection(%(arg-type)Integer% item)

Toggles the given item between selected and unselected states. For
multiselection controls only.

TreeCtrl#unselect

unselect()

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

TreeCtrl#traverse

traverse(%(arg-type)Integer% start_item = root_id) { | item_id | … }

Recurses over the TreeCtrl’s items, passing each TreeItemId successively
into the passed block. This TreeItemId can be used as an argument to
many other methods in this class.

The start_item argument will be the first item_id passed into the
block, followed by its children and descendants, depth first. If
start_item is not specified, it will traverse over all the items in
the tree. It will starting from the root item, or, if the tree has a
hidden root, the bottom-most items.

TreeCtrl#unselect_all

unselect_all()

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

TreeCtrl#unselect_item

unselect_item(%(arg-type)Integer% item)

Unselects the given item. This works in multiselection controls only.

[This page automatically generated from the Textile source at 2023-06-13 21:31:30 +0000]