This command adjusts the view in the window up or down according to
I<number> and I<what>.
I<Number> must be an integer.
I<What> must be either B<units> or B<pages>.
If I<what> is B<units>, the view adjusts up or down by
I<number> lines; if it is B<pages> then
the view adjusts by I<number> screenfuls.
If I<number> is negative then earlier elements
become visible; if it is positive then later elements
become visible.
=back
=back
=head1 DEFAULT BINDINGS
Tk automatically creates class bindings for listboxes that give them
Motif-like behavior. Much of the behavior of a listbox is determined
by its B<selectMode> option, which selects one of four ways
of dealing with the selection.
If the selection mode is B<single> or B<browse>, at most one
element can be selected in the listbox at once.
In both modes, clicking button 1 on an element selects
it and deselects any other selected item.
In B<browse> mode it is also possible to drag the selection
with button 1.
If the selection mode is B<multiple> or B<extended>,
any number of elements may be selected at once, including discontiguous
ranges. In B<multiple> mode, clicking button 1 on an element
toggles its selection state without affecting any other elements.
In B<extended> mode, pressing button 1 on an element selects
it, deselects everything else, and sets the anchor to the element
under the mouse; dragging the mouse with button 1
down extends the selection to include all the elements between
the anchor and the element under the mouse, inclusive.
Most people will probably want to use B<browse> mode for
single selections and B<extended> mode for multiple selections;
the other modes appear to be useful only in special situations.
Any time the selection changes in the listbox, the virtual event
B<<< <<ListboxSelect>> >>> will be generated. It is easiest to bind to this
event to be made aware of any changes to listbox selection.
In addition to the above behavior, the following additional behavior
is defined by the default bindings:
=over 4
=item [1]
In B<extended> mode, the selected range can be adjusted by pressing
button 1 with the Shift key down: this modifies the selection to
consist of the elements between the anchor and the element under
the mouse, inclusive.
The un-anchored end of this new selection can also be dragged with
the button down.
=item [2]
In B<extended> mode, pressing button 1 with the Control key down
starts a toggle operation: the anchor is set to the element under
the mouse, and its selection state is reversed. The selection state
of other elements isn't changed.
If the mouse is dragged with button 1 down, then the selection state
of all elements between the anchor and the element under the mouse
is set to match that of the anchor element; the selection state of
all other elements remains what it was before the toggle operation
began.
=item [3]
If the mouse leaves the listbox window with button 1 down, the window
scrolls away from the mouse, making information visible that used
to be off-screen on the side of the mouse.
The scrolling continues until the mouse re-enters the window, the
button is released, or the end of the listbox is reached.
=item [4]
Mouse button 2 may be used for scanning.
If it is pressed and dragged over the listbox, the contents of
the listbox drag at high speed in the direction the mouse moves.
=item [5]
If the Up or Down key is pressed, the location cursor (active
element) moves up or down one element.
If the selection mode is B<browse> or B<extended> then the
new active element is also selected and all other elements are
deselected.
In B<extended> mode the new active element becomes the
selection anchor.
=item [6]
In B<extended> mode, Shift-Up and Shift-Down move the location
cursor (active element) up or down one element and also extend
the selection to that element in a fashion similar to dragging
with mouse button 1.
=item [7]
The Left and Right keys scroll the listbox view left and right
by the width of the character B<0>.
Control-Left and Control-Right scroll the listbox view left and
right by the width of the window.
Control-Prior and Control-Next also scroll left and right by
the width of the window.
=item [8]
The Prior and Next keys scroll the listbox view up and down
by one page (the height of the window).
=item [9]
The Home and End keys scroll the listbox horizontally to
the left and right edges, respectively.
=item [10]
Control-Home sets the location cursor to the the first element in
the listbox, selects that element, and deselects everything else
in the listbox.
=item [11]
Control-End sets the location cursor to the the last element in
the listbox, selects that element, and deselects everything else
in the listbox.
=item [12]
In B<extended> mode, Control-Shift-Home extends the selection
to the first element in the listbox and Control-Shift-End extends
the selection to the last element.
=item [13]
In B<multiple> mode, Control-Shift-Home moves the location cursor
to the first element in the listbox and Control-Shift-End moves
the location cursor to the last element.
=item [14]
The space and Select keys make a selection at the location cursor
(active element) just as if mouse button 1 had been pressed over
this element.
=item [15]
In B<extended> mode, Control-Shift-space and Shift-Select
extend the selection to the active element just as if button 1
had been pressed with the Shift key down.
=item [16]
In B<extended> mode, the Escape key cancels the most recent
selection and restores all the elements in the selected range
to their previous selection state.
=item [17]
Control-slash selects everything in the widget, except in
B<single> and B<browse> modes, in which case it selects
the active element and deselects everything else.
=item [18]
Control-backslash deselects everything in the widget, except in
B<browse> mode where it has no effect.
=item [19]
The F16 key (labelled Copy on many Sun workstations) or Meta-w
copies the selection in the widget to the clipboard, if there is
a selection.
The behavior of listboxes can be changed by defining new bindings for
individual widgets or by redefining the class bindings.
=back
=head1 TIED INTERFACE
The Tk::Listbox widget can also be tied to a scalar or array variable, with
different behaviour depending on the variable type, with the following
tie commands:
use Tk;
my ( @array, $scalar, $other );
my %options = ( ReturnType => "index" );
my $MW = MainWindow->new();
my $lbox = $MW->Listbox()->pack();
my @list = ( "a", "b", "c", "d", "e", "f" );
$lbox->insert('end', @list );
tie @array, "Tk::Listbox", $lbox
tie $scalar, "Tk::Listbox", $lbox;
tie $other, "Tk::Listbox", $lbox, %options;
currently only one modifier is implemented, a 3 way flag for tied scalars
"ReturnType" which can have values "element", "index" or "both". The default
is "element".
=over 4
=item Tied Arrays
If you tie an array to the Listbox you can manipulate the items currently
contained by the box in the same manner as a normal array, e.g.
print @array;
push(@array, @list);
my $popped = pop(@array);
my $shifted = shift(@array);
unshift(@array, @list);
delete $array[$index];
print $string if exists $array[$i];
@array = ();
splice @array, $offset, $length, @list
The delete function is implemented slightly differently from the standard
array implementation. Instead of setting the element at that index to undef
it instead physically removes it from the Listbox. This has the effect of
changing the array indices, so for instance if you had a list on non-continuous
indices you wish to remove from the Listbox you should reverse sort the list
and then apply the delete function, e.g.
my @list = ( 1, 2, 4, 12, 20 );
my @remove = reverse sort { $a <=> $b } @list;
delete @array[@remove];
would safely remove indices 20, 12, 4, 2 and 1 from the Listbox without
problems. It should also be noted that a similar warning applies to the
splice function (which would normally be used in this context to perform
the same job).
=item Tied Scalars
Unlike tied arrays, if you tie a scalar to the Listbox you can retrieve the
currently selected elements in the box as an array referenced by the scalar,
for instance
my @list = ( "a", "b", "c", "d", "e", "f" );
$lbox->insert('end', sort @list );
$lbox->selectionSet(1);
inserts @list as elements in an already existing listbox and selects the
element at index 1, which is "b". If we then
print @$selected;
this will return the currently selected elements, in this case "b".
However, if the "ReturnType" arguement is passed when tying the Listbox to the
scalar with value "index" then the indices of the selected elements will be
returned instead of the elements themselves, ie in this case "1". This can be
useful when manipulating both contents and selected elements in the Listbox at
the same time.
Importantly, if a value "both" is given the scalar will not be tied to an
array, but instead to a hash, with keys being the indices and values being
the elements at those indices
You can also manipulate the selected items using the scalar. Equating the
scalar to an array reference will select any elements that match elements
in the Listbox, non-matching array items are ignored, e.g.
my @list = ( "a", "b", "c", "d", "e", "f" );
$lbox->insert('end', sort @list );
$lbox->selectionSet(1);
would insert the array @list into an already existing Listbox and select
element at index 1, i.e. "b"
@array = ( "a", "b", "f" );
$selected = \@array;
would select elements "a", "b" and "f" in the Listbox.
Again, if the "index" we indicate we want to use indices in the options hash
then the indices are use instead of elements, e.g.
@array = ( 0, 1, 5 );
$selected = \@array;
would have the same effect, selecting elements "a", "b" and "f" if the
$selected variable was tied with %options = ( ReturnType => "index" ).
If we are returning "both", i.e. the tied scalar points to a hash, both key and
value must match, e.g.
%hash = ( 0 => "a", 1 => "b", 5 => "f" );
$selected = \%hash;
would have the same effect as the previous examples.
It should be noted that, despite being a reference to an array (or possibly a has), you still can not copy the tied variable without it being untied, instead
you must pass a reference to the tied scalar between subroutines.