Next: Selecting Files and Up: Hierarchical Listbox Previous: Handling the Selection


Creating Collapsible Tree Structures with TixTree 

The TixTree widget is based on the TixScrolledHList widget; you can use it to create a collapsible hierarchical structure so that the user can conveniently navigate through a large number of list entries. As shown in figure 4-7, the TixTree puts the little ``+'' and ``-'' icons next to the branches of an HList entry that has descendants. These two icons are knows as the open and close icons, respectively. When the user presses the open icon next to an entry, its immediate children of an entry will be displayed. Conversely, when the user presses the close icon, the entry's children will become hidden.

Program 4-6 shows how to create a collapsible tree. We first create a TixTree widget. Then we add the entries in your hierarchical structure into its hlist subwidget using the add method of this subwidget. When we are finished with adding the entries, we just call the autosetmode method of the TixTree widget, which will automatically adds the open and close icons next to the entries who have children.

set folder [tix getimage folder]
tixTree .tree -command Command -options {
hlist.separator /
hlist.itemType imagetext
hlist.drawBranch true
hlist.indent 18
pack .tree -expand yes -fill both
set hlist [.tree subwidget hlist]

foreach directory {/ /usr /usr/bin /usr/local /etc /etc/rc.d} {
$hlist add $directory -image $folder -text $directory
.tree autosetmode

proc Command {entry} {
puts "you have selected $entry"

(Figure 4-6) Creating a Collapsible Hierarchy 

Note that in program 4-6 we use the -command option of the TixTree widget, not the -command option of its hlist subwidget. This is because the TixTree actually used the -command option of its hlist subwidget to process some low-level events. In general, if both a mega-widget and its subwidget have the options of the same name, you would always use the option that belongs to the mega-widget.