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.
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.
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. |
TreeItemData, ListBox, ListCtrl, ImageList, TreeEvent
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:
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 .
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.
Constructor, creating and showing a tree control.
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.
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.
Collapses the root item.
Collapses this item and all of its children, recursively.
Collapses the given item and removes all children.
Creates the tree control. See TreeCtrl.new for further details.
Deletes the specified item. A EVT_TREE_DELETE_ITEM
event will be
generated.
This function may cause a subsequent call to GetNextChild to fail.
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.
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.
Passes the item id of each and every item in the TreeCtrl into the
passed block, allowing iteration of the control’s contents.
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.
TreeCtrl#end_edit_label,
TreeEvent
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.
Scrolls and/or expands items to ensure that the given item is visible.
Expands the given item.
Expands all items in the tree.
Expands the given item and all its children recursively.
Returns the buttons image list (from which application-defined button images are taken).
This function is only available in the generic version.
Returns all of the children of the item as an Array of Tree Item Ids.
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.
Returns the number of items in the 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.
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.
Returns the first visible item.
Returns the normal image list.
Returns the current tree control indentation.
Returns the background colour of the item.
Returns the tree item data associated with the item.
Returns the font of the item label.
Gets the specified item image. The value of which may be:
TREE_ITEM_ICON_NORMAL
to get the normal item imageTREE_ITEM_ICON_SELECTED
to get the selected item image (i.e. the image which is shown when the item is currently selected)TREE_ITEM_ICON_EXPANDED
to get the expanded image (this only makes sense for items which have children – then this image is shown when the item is expanded and the normal image is shown when it is collapsed)TREE_ITEM_ICON_SELECTED_EXPANDED
to get the selected expanded image (which is shown when an expanded item is currently selected)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.
Returns the item label.
Returns the colour of the item label.
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.
Returns the last child of the item (or an invalid tree item if this item has no children).
get_first_child,
TreeCtrl#get_next_sibling,
get_last_child
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.
Returns the next visible item.
Returns the item’s parent.
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.
Returns the previous visible item.
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.
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.
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.
Gets the selected item image (this function is obsolete, use
GetItemImage(item, TreeItemIcon_Selected
) instead).
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.
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.
Returns the state image list (from which application-defined state images are taken).
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. |
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.
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.
Returns true
if the given item is in bold state.
See also: set_item_bold
Returns if the control is empty (i.e. has no items, even no root one).
Returns true
if the item is expanded (only makes sense if it has children).
Returns true
if the item is selected.
Returns true
if the item is visible (it might be outside the view, or not expanded).
Returns true
if the item has children.
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
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.
Scrolls the specified item into view.
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.
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.
Sets the indentation for the tree control.
Sets the normal image list.
Sets the colour of the item’s background.
true
)
Makes item appear in bold font if bold parameter is true
or resets it to
the normal state.
See also: is_bold
Sets the item client data.
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).
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.
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.
Sets the specified item image. See get_item_image
for the description of the which parameter.
Sets the selected item image (this function is obsolete, use SetItemImage(item, TreeItemIcon_Selected
) instead).
Sets the item label.
Sets the colour of the item’s text.
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.
Sets the state image list (from which application-defined state images are taken).
Sets the mode flags associated with the display of the tree control.
The new mode takes effect immediately.
(Generic only; MSW ignores changes.)
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).
Toggles the given item between collapsed and expanded states.
Toggles the given item between selected and unselected states. For
multiselection controls only.
Removes the selection from the currently selected item (if any).
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.
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.
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]