blt::treeview(3tcl) | BLT Built-In Commands | blt::treeview(3tcl) |
treeview - Create and manipulate hierarchical table widgets
treeview pathName ?options?
The treeview widget displays a tree of data. It replaces both the hiertable and hierbox widgets. The treeview is 100% syntax compatible with the hiertable widget. The hiertable command is retained for sake of script-level compatibility. This widget obsoletes the hierbox widget. It does everything the old hierbox widget did, but also provides data sharing (via tree data objects) and the ability to tag nodes.
The treeview widget displays hierarchical data. Data is represented as nodes in a general-ordered tree. Each node may have sub-nodes and these nodes can in turn has their own children.
A node is displayed as a row entry in the widget. Each entry has a text label and icon. When a node has children, its entry is drawn with a small button to the left of the label. Clicking the mouse over this button opens or closes the node. When a node is open, its children are exposed. When it is closed, the children and their descedants are hidden. The button is normally a + or - symbol (ala Windows Explorer), but can be replaced with a pair of Tk images (open and closed images).
If the node has data associated with it, they can be displayed in columns running vertically on either side the tree. You can control the color, font, etc of each entry. Any entry label or data field can be edited in-place.
The tree is not stored inside the widget but in a tree data object (see the tree command for a further explanation). Tree data objects can be shared among different clients, such as a treeview widget or the tree command. You can walk the tree and manage its data with the tree command tree, while displaying it with the treeview widget. Whenever the tree is updated, the treeview widget is automatically redrawn.
By default, the treeview widget creates its own tree object. The tree initially contains just a root node. But you can also display trees created by the tree command using the -tree configuration option. Treeview widgets can share the same tree object, possibly displaying different views of the same data.
A tree object has both a Tcl and C API. You can insert or delete nodes using treeview widget or tree command operations, but also from C code. For example, you can load the tree from your C code while still managing and displaying the tree from Tcl. The widget is automatically notified whenever the tree is modified via C or Tcl.
treeview pathName ?option value?...
The treeview command creates a new window pathName and makes it into a treeview widget. At the time this command is invoked, there must not exist a window named pathName, but pathName's parent must exist. Additional options may be specified on the command line or in the option database to configure aspects of the widget such as its colors and font. See the configure operation below for the exact details about what option and value pairs are valid.
If successful, treeview returns the path name of the widget. It also creates a new Tcl command by the same name. You can use this command to invoke various operations that query or modify the widget. The general form is:
pathName operation ?arg?...
Both operation and its arguments determine the exact behavior of the command. The operations available are described in the TREEVIEW OPERATIONS section.
Nodes can be inserted into a tree using the treeview widget
blt::treeview .t set node [.t insert end root "one"]
or tree command.
set tree [blt::tree create] set node [$tree insert root "one"]
In both cases, a number identifying the node is returned (the value of $node). This serial number or id uniquely identifies the node. Please note that you can't infer a location or position of a node from its id. The only exception is that the root node is always id 0. Since nodes may have the same labels or be moved within the tree, ids provide an convenient way to identify nodes. If a tree is shared, the ids will be the same regardless if you are using by the treeview widget or the tree command. Ids are recycled when the node deleted.
A node may also have any number of tags associated with it. A tag is just a string of characters, and it may take any form except that of an integer. For example, "x123" is valid, but "123" isn't. The same tag may be associated with many different nodes. This is typically done to associate a group of nodes. Many operations in the treeview widget take either node ids or tag names as arguments. Using a tag says to apply the operation to all nodes with that tag.
The tag all is implicitly associated with every node in the tree. It may be used to invoke operations on all the nodes in the tree.
Tags may be shared, just like trees, between clients. For example, you can use the tags created by the tree command with treeview widgets.
There are also several special non-numeric ids. Special ids differ from tags in that they are always translated to their numeric equivalent. They also take precedence over tags. For example, you can't use a tag name that is a special id. These ids are specific to the treeview widget.
A node may be specified as an id or tag. If the specifier is an integer then it is assumed to refer to the single node with that id. If the specifier is not an integer, it's checked to see if it's a special id (such as focus). Otherwise, it's assumed to be tag. Some operations only operate on a single node at a time; if a tag refers to more than one node, then an error is generated.
A node in the tree can have data fields. A data field is a name-value pair, used to represent arbitrary data in the node. Nodes can contain different fields (they aren't required to contain the same fields). You can optionally display these fields in the treeview widget in columns running on either side of the displayed tree. A node's value for the field is drawn in the column along side its node in the hierarchy. Any node that doesn't have a specific field is left blank. Columns can be interactively resized, hidden, or, moved.
You can bind Tcl commands to be invoked when events occur on nodes (much like Tk canvas items). You can bind a node using its id or its bindtags. Bindtags are simply names that associate a binding with one or more nodes. There is a built-in tag all that all node entries automatically have.
The treeview operations are the invoked by specifying the widget's pathname, the operation, and any arguments that pertain to that operation. The general form is:
pathName operation ?arg arg ...?
Operation and the args determine the exact behavior of the command. The following operation are available for treeview widgets:
If all arguments are specified then a new binding is created, replacing any existing binding for the same sequence and tagName. If the first character of command is + then command augments an existing binding rather than replacing it. If no command argument is provided then the command currently associated with tagName and sequence (it's an error occurs if there's no such binding) is returned. If both command and sequence are missing then a list of all the event sequences for which bindings have been defined for tagName.
If all arguments are specified then a new binding is created, replacing any existing binding for the same sequence and tagName. If the first character of command is + then command augments an existing binding rather than replacing it. If no command argument is provided then the command currently associated with tagName and sequence (it's an error occurs if there's no such binding) is returned. If both command and sequence are missing then a list of all the event sequences for which bindings have been defined for tagName.
All ancestors of the new node must already exist, unless the -autocreate option is set. It is also an error if a node already exists, unless the -allowduplicates option is set.
Option and value may have any of the values accepted by the entry configure operation described in the ENTRY OPERATIONS section below. This command returns a list of the ids of the new entries.
Both operation and its arguments determine the exact behavior of the command. The operations available for tags are listed below.
In addition to the configure operation, widget configuration options may also be set by the Tk option command. The class resource name is TreeView.
option add *TreeView.Foreground white option add *TreeView.Background blue
The following widget options are available:
Many widget configuration options have counterparts in entries. For example, there is a -closecommand configuration option for both widget itself and for individual entries. Options set at the widget level are global for all entries. If the entry configuration option is set, then it overrides the widget option. This is done to avoid wasting memory by replicated options. Most entries will have redundant options.
There is no resource class or name for entries.
Button configuration options may also be set by the option command. The resource subclass is Button. The resource name is always button.
option add *TreeView.Button.Foreground white option add *TreeView.button.Background blue
The following are the configuration options available for buttons.
Column configuration options may also be set by the option command. The resource subclass is Column. The resource name is the name of the column.
option add *TreeView.Column.Foreground white option add *TreeView.treeView.Background blue
The following configuration options are available for columns.
Text edit window configuration options may also be set by the option command. The resource class is TreeViewEditor. The resource name is always edit.
option add *TreeViewEditor.Foreground white option add *edit.Background blue
The following are the configuration options available for the text editing window.
Tk automatically creates class bindings for treeviews that give them Motif-like behavior. Much of the behavior of a treeview widget is determined by its -selectmode option, which selects one of two ways of dealing with the selection.
If the selection mode is single, only one node can be selected at a time. Clicking button 1 on an node selects it and deselects any other selected item.
If the selection mode is multiple, any number of entries may be selected at once, including discontiguous ranges. Clicking Control-Button-1 on a node entry toggles its selection state without affecting any other entries. Pressing Shift-Button-1 on a node entry selects it, extends the selection.
The behavior of treeview widgets can be changed by defining new bindings for individual widgets or by redefining the class bindings.
In addition to the above behavior, the following additional behavior is defined by the default widget class (TreeView) bindings.
Buttons have bindings. There are associated with the "all" bindtag (see the entry's -bindtag option). You can use the bind operation to change them.
Entries have default bindings. There are associated with the "all" bindtag (see the entry's -bindtag option). You can use the bind operation to modify them.
Columns have bindings too. They are associated with the column's "all" bindtag (see the column -bindtag option). You can use the column bind operation to change them.
The treeview command creates a new widget.
treeview .h -bg white
A new Tcl command .h is also created. This command can be used to query and modify the treeview widget. For example, to change the background color of the table to "green", you use the new command and the widget's configure operation.
# Change the background color. .h configure -background "green"
By default, the treeview widget will automatically create a new tree object to contain the data. The name of the new tree is the pathname of the widget. Above, the new tree object name is ".h". But you can use the -tree option to specify the name of another tree.
# View the tree "myTree". .h configure -tree "myTree"
When a new tree is created, it contains only a root node. The node is automatically opened. The id of the root node is always 0 (you can use also use the special id root). The insert operation lets you insert one or more new entries into the tree. The last argument is the node's pathname.
# Create a new entry named "myEntry" set id [.h insert end "myEntry"]
This appends a new node named "myEntry". It will positioned as the last child of the root of the tree (using the position "end"). You can supply another position to order the node within its siblings.
# Prepend "fred". set id [.h insert 0 "fred"]
Entry names do not need to be unique. By default, the node's label is its name. To supply a different text label, add the -label option.
# Create a new node named "fred" set id [.h insert end "fred" -label "Fred Flintstone"]
The insert operation returns the id of the new node. You can also use the index operation to get this information.
# Get the id of "fred" .h index "fred"
To insert a node somewhere other than root, use the -at switch. It takes the id of the node where the new child will be added.
# Create a new node "barney" in "fred". .h insert -at $id end "barney"
A pathname describes the path to an entry in the hierarchy. It's a list of entry names that compose the path in the tree. Therefore, you can also add "barney" to "fred" as follows.
# Create a new sub-entry of "fred" .h insert end "fred barney"
Every name in the list is ancestor of the next. All ancestors must already exist. That means that an entry "fred" is an ancestor of "barney" and must already exist. But you can use the -autocreate configuration option to force the creation of ancestor nodes.
# Force the creation of ancestors. .h configure -autocreate yes .h insert end "fred barney wilma betty"
Sometimes the pathname is already separated by a character sequence rather than formed as a list. A file name is a good example of this. You can use the -separator option to specify a separator string to split the path into its components. Each pathname inserted is automatically split using the separator string as a separator. Multiple separators are treated as one.
.h configure -separator / .h insert end "/usr/local/tcl/bin"
If the path is prefixed by extraneous characters, you can automatically trim it off using the -trim option. It removed the string from the path before it is parsed.
.h configure -trim C:/windows -separator / .h insert end "C:/window/system"
You can insert more than one entry at a time with the insert operation. This can be much faster than looping over a list of names.
# The slow way foreach f [glob $dir/*] {
.h insert end $f } # The fast way eval .h insert end [glob $dir/*]
In this case, the insert operation will return a list of ids of the new entries.
You can delete entries with the delete operation. It takes one or more tags of ids as its argument. It deletes the entry and all its children.
.h delete $id
Entries have several configuration options. They control the appearance of the entry's icon and label. We have already seen the -label option that sets the entry's text label. The entry configure operation lets you set or modify an entry's configuration options.
.h entry configure $id -color red -font fixed
You can hide an entry and its children using the -hide option.
.h entry configure $id -hide yes
More that one entry can be configured at once. All entries specified are configured with the same options.
.h entry configure $i1 $i2 $i3 $i4 -color brown
An icon is displayed for each entry. It's a Tk image drawn to the left of the label. You can set the icon with the entry's -icons option. It takes a list of two image names: one to represent the open entry, another when it is closed.
set im1 [image create photo -file openfolder.gif] set im2 [image create photo -file closefolder.gif] .h entry configure $id -icons "$im1 $im2"
If -icons is set to the empty string, no icons are display.
If an entry has children, a button is displayed to the left of the icon. Clicking the mouse on this button opens or closes the sub-hierarchy. The button is normally a + or - symbol, but can be configured in a variety of ways using the button configure operation. For example, the + and - symbols can be replaced with Tk images.
set im1 [image create photo -file closefolder.gif] set im2 [image create photo -file downarrow.gif] .h button configure $id -images "$im1 $im2" \
-openrelief raised -closerelief raised
Entries can contain an arbitrary number of data fields. Data fields are name-value pairs. Both the value and name are strings. The entry's -data option lets you set data fields.
.h entry configure $id -data {mode 0666 group users}
The -data takes a list of name-value pairs.
You can display these data fields as columns in the treeview widget. You can create and configure columns with the column operation. For example, to add a new column to the widget, use the column insert operation. The last argument is the name of the data field that you want to display.
.h column insert end "mode"
The column title is displayed at the top of the column. By default, it's is the field name. You can override this using the column's -text option.
.h column insert end "mode" -text "File Permissions"
Columns have several configuration options. The column configure operation lets you query or modify column options.
.h column configure "mode" -justify left
The -justify option says how the data is justified within in the column. The -hide option indicates whether the column is displayed.
.h column configure "mode" -hide yes
Entries can be selected by clicking on the mouse. Selected entries are drawn using the colors specified by the -selectforeground and -selectbackground configuration options. The selection itself is managed by the selection operation.
# Clear all selections .h selection clear 0 end # Select the root node .h selection set 0
The curselection operation returns a list of ids of all the selected entries.
set ids [.h curselection]
You can use the get operation to convert the ids to their pathnames.
set names [eval .h get -full $ids]
If a treeview is exporting its selection (using the -exportselection option), then it will observe the standard X11 protocols for handling the selection. Treeview selections are available as type STRING; the value of the selection will be the pathnames of the selected entries, separated by newlines.
The treeview supports two modes of selection: single and multiple. In single select mode, only one entry can be selected at a time, while multiple select mode allows several entries to be selected. The mode is set by the widget's -selectmode option.
.h configure -selectmode "multiple"
You can be notified when the list of selected entries changes. The widget's -selectcommand specifies a Tcl procedure that is called whenever the selection changes.
proc SelectNotify { widget } {
set ids [$widget curselection] } .h configure -selectcommand "SelectNotify .h"
The widget supports the standard Tk scrolling and scanning operations. The treeview can be both horizontally and vertically. You can attach scrollbars to the treeview the same way as the listbox or canvas widgets.
scrollbar .xbar -orient horizontal -command ".h xview" scrollbar .ybar -orient vertical -command ".h yview" .h configure -xscrollcommand ".xbar set" \
-yscrollcommand ".ybar set"
There are three different modes of scrolling: listbox, canvas, and hierbox. In listbox mode, the last entry can always be scrolled to the top of the widget. In hierbox mode, the last entry is always drawn at the bottom of the widget. The scroll mode is set by the widget's -selectmode option.
.h configure -scrollmode "listbox"
Entries can be programmatically opened or closed using the open and close operations respectively.
.h open $id .h close $id
When an entry is opened, a Tcl procedure can be automatically invoked. The -opencommand option specifies this procedure. This procedure can lazily insert entries as needed.
proc AddEntries { dir } {
eval .h insert end [glob -nocomplain $dir/*] } .h configure -opencommand "AddEntries %P"
Now when an entry is opened, the procedure AddEntries is called and adds children to the entry. Before the command is invoked, special "%" substitutions (like bind) are performed. Above, %P is translated to the pathname of the entry.
The same feature exists when an entry is closed. The -closecommand option specifies the procedure.
proc DeleteEntries { id } {
.h entry delete $id 0 end } .h configure -closecommand "DeleteEntries %#"
When an entry is closed, the procedure DeleteEntries is called and deletes the entry's children using the entry delete operation (%# is the id of entry).
treeview, widget
2.5 | BLT |