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:
def on_tree_clicked(event)
if event.get_item.nonzero?
# .. an item was clicked, make it bold
set_item_bold(event.get_item)
else
# otherwise the click didn't happen over an item
...
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.
TreeCtrl.new(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.
TreeItemId add_root(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.
TreeItemId append_item(TreeItemId 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.
collapse_all()
Collapses the root item.
collapse_all_children(TreeItemId item)
Collapses this item and all of its children, recursively.
collapse_and_reset(TreeItemId item)
Collapses the given item and removes all children.
Boolean TreeCtrl.new(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.
delete(TreeItemId 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.
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.
delete_children(TreeItemId 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.
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.
edit_label(TreeItemId 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.
TreeCtrl#end_edit_label, TreeEvent
end_edit_label(Boolean cancelEdit)
Ends label editing. If cancelEdit is true, the edit will be cancelled.
This function is currently supported under Windows only.
ensure_visible(TreeItemId item)
Scrolls and/or expands items to ensure that the given item is visible.
expand(TreeItemId item)
Expands the given item.
expand_all()
Expands all items in the tree.
expand_all_children(TreeItemId item)
Expands the given item and all its children recursively.
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.
Array get_children(TreeItemId item)
Returns all of the children of the item as an Array of Tree Item Ids.
Integer get_children_count(TreeItemId 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.
unsigned int get_count()
Returns the number of items in the 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.
TreeItemId get_first_child(TreeItemId 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.
TreeItemId get_first_visible_item()
Returns the first visible item.
ImageList get_image_list()
Returns the normal image list.
Integer get_indent()
Returns the current tree control indentation.
Colour get_item_background_colour(TreeItemId item)
Returns the background colour of the item.
Object get_item_data(TreeItemId item)
Returns the tree item data associated with the item.
Font get_item_font(TreeItemId item)
Returns the font of the item label.
Integer get_item_image(TreeItemId item,
int which = Wx::TREE_ITEM_ICON_NORMAL)
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)Rect get_item_rect(TreeItemId 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.
String get_item_text(TreeItemId item)
Returns the item label.
Colour get_item_text_colour(TreeItemId item)
Returns the colour of the item label.
Rect get_label_rect(TreeItemId 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.
TreeItemId get_last_child(TreeItemId item)
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
TreeItemId get_next_sibling(TreeItemId 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.
TreeItemId get_next_visible(TreeItemId item)
Returns the next visible item.
TreeItemId get_item_parent(TreeItemId item)
Returns the item’s parent.
TreeItemId get_prev_sibling(TreeItemId 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.
TreeItemId get_prev_visible(TreeItemId item)
Returns the previous visible item.
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.
TreeItemId 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
always return 0, an invalid tree id.
Integer get_item_selected_image(TreeItemId item)
Gets the selected item image (this function is obsolete, use
GetItemImage(item, TreeItemIcon_Selected) instead).
TreeItemId 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.
unsigned int get_selections(ArrayTreeItemIds selection)
Fills the array of tree items passed in with the currently selected items. This function can be called only if the control has the TR_MULTIPLE style.
Returns the number of selected items.ImageList get_state_image_list()
Returns the state image list (from which application-defined state images are taken).
TreeItemId hit_test(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. |
TreeItemId insert_item(TreeItemId parent,
TreeItemId 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.
TreeItemId insert_item_before(TreeItemId 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.
Boolean is_bold(TreeItemId item)
Returns true if the given item is in bold state.
See also: set_item_bold
Boolean is_empty()
Returns if the control is empty (i.e. has no items, even no root one).
Boolean is_expanded(TreeItemId item)
Returns true if the item is expanded (only makes sense if it has children).
Boolean is_selected(TreeItemId item)
Returns true if the item is selected.
Boolean is_visible(TreeItemId item)
Returns true if the item is visible (it might be outside the view, or not expanded).
Boolean item_has_children(TreeItemId item)
Returns true if the item has children.
Integer on_compare_items(TreeItemId item1, TreeItemId 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
TreeItemId prepend_item(TreeItemId 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.
scroll_to(TreeItemId item)
Scrolls the specified item into view.
select_item(TreeItemId 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.
set_buttons_image_list(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.
set_indent(Integer indent)
Sets the indentation for the tree control.
set_image_list(ImageList imageList)
Sets the normal image list.
set_item_background_colour(TreeItemId item, Colour col)
Sets the colour of the item’s background.
set_item_bold(TreeItemId 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
set_item_data(TreeItemId item, Object data)
Sets the item client data.
set_item_drop_highlight(TreeItemId 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).
set_item_font(TreeItemId 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.
set_item_has_children(TreeItemId 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.
set_item_image(TreeItemId item, Integer image,
TreeItemIcon which = TreeItemIcon_Normal)
Sets the specified item image. See get_item_image for the description of the which parameter.
set_item_selected_image(TreeItemId item, Integer selImage)
Sets the selected item image (this function is obsolete, use SetItemImage(item, TreeItemIcon_Selected) instead).
set_item_text(TreeItemId item, String text)
Sets the item label.
set_item_text_colour(TreeItemId item, Colour col)
Sets the colour of the item’s text.
set_quick_best_size(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.
set_state_image_list(ImageList imageList)
Sets the state image list (from which application-defined state images are taken).
set_window_style(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.)
sort_children(TreeItemId 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).
toggle(TreeItemId item)
Toggles the given item between collapsed and expanded states.
toggle_item_selection(TreeItemId item)
Toggles the given item between selected and unselected states. For multiselection controls only.
unselect()
Removes the selection from the currently selected item (if any).
traverse(TreeItemId 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.
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.
unselect_item(TreeItemId item)
Unselects the given item. This works in multiselection controls only.
[This page automatically generated from the Textile source at Thu May 01 00:50:47 +0100 2008]