HList
The HList widget is used to display data that has
a hierarchial structure, such as a
computer directory tree or a company's organization
chart. The basic syntax for creating such a widget is
The basic syntax is
my $hlist = $mw->HList( [ option => value ] );
Some common options are
- -itemtype => type:
This specifies the type of item to be displayed,
which currently is one of imagetext, text,
and $widget.
- -separator => value
This specifies the separator character; by default, ``.'' is used.
- -selectmode => mode:
This specifies the mode of selection; the default bindings
assume one of single, browse, multiple,
or extended.
- -browsecmd => callback:
This specifies a callback to be invoked when the user
browses through the selections
In addition, for creating and deleting items the following
methods are available.
- $hlist->add($entryPath, [option => value]):
This creates a new list item with the specified path name.
- $hlist->addchild($ParentPath, [option => value]):
This creates a new child entry under the specified parent
path, which is automatically generated.
- $hlist->delete(option, $entryPath):
This deletes one or more of the entries specified by option,
which may be one of all, entry, offsprings,
or siblings (the last two do not delete $entryPath).
Here is an example of its use:
#!perl
# file hlist.pl
use Tk;
use strict;
use warnings;
use Tk::HList;
my $message = 'nothing yet ...';
my $mw = MainWindow->new;
$mw->title('Dialog');
my $label = $mw->Label(-textvariable => \$message);
my $exit = $mw->Button(-text => 'Exit',
-command => [$mw => 'destroy']);
my $hlist = $mw->Scrolled('HList',
-itemtype => 'text',
-separator => '/',
-selectmode => 'single',
-browsecmd => \&browse,
);
my @dirs = qw(/ /home /home/joe /home/jane /usr
/usr/lib /usr/bin /usr/man /usr/include /usr/local
/usr/local/lib /usr/local/bin /usr/local/include
);
$hlist->add($_, -text => $_) for @dirs;
$exit->pack(-side => 'bottom');
$label->pack(-side => 'top');
$hlist->pack;
MainLoop;
sub browse {
my $choice = shift;
$message = "You chose '$choice'";
}
which is illustrated in the screenshot below.
Figure 3.18:
Example of a HList widget
|
Here the HList widget is created and populated, through
the add method, with an array of directory names.
When the user selects one of the entries, the
browse callback is invoked, which changes the
message associated with the $label widget to
one refelcting which entry has been chosen.
Randy Kobes
2003-11-17