| 1 |
# Copyright (c) 1990 The Regents of the University of California. |
| 2 |
# Copyright (c) 1994-1997 Sun Microsystems, Inc. |
| 3 |
# See the file "license.terms" for information on usage and redistribution |
| 4 |
# of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 5 |
# |
| 6 |
# |
| 7 |
|
| 8 |
=head1 NAME |
| 9 |
|
| 10 |
Tk::Listbox - Create and manipulate Listbox widgets |
| 11 |
|
| 12 |
=for category Tk Widget Classes |
| 13 |
|
| 14 |
=head1 SYNOPSIS |
| 15 |
|
| 16 |
I<$listbox> = I<$parent>-E<gt>B<Listbox>(?I<options>?); |
| 17 |
|
| 18 |
=head1 STANDARD OPTIONS |
| 19 |
|
| 20 |
B<-background> B<-borderwidth> B<-cursor> B<-disabledforeground> |
| 21 |
B<-exportselection> B<-font> B<-foreground> B<-height> |
| 22 |
B<-highlightbackground> B<-highlightcolor> B<-highlightthickness> |
| 23 |
B<-offset> B<-relief> B<-selectbackground> B<-selectborderwidth> |
| 24 |
B<-selectforeground> B<-setgrid> B<-state> B<-takefocus> B<-tile> |
| 25 |
B<-width> B<-xscrollcommand> B<-yscrollcommand> |
| 26 |
|
| 27 |
|
| 28 |
See L<Tk::options> for details of the standard options. |
| 29 |
|
| 30 |
=head1 WIDGET-SPECIFIC OPTIONS |
| 31 |
|
| 32 |
=over 4 |
| 33 |
|
| 34 |
=item Name: B<activeStyle> |
| 35 |
|
| 36 |
=item Class: B<ActiveStyle> |
| 37 |
|
| 38 |
=item Switch: B<-activestyle> |
| 39 |
|
| 40 |
Specifies the style in which to draw the active element. This must be |
| 41 |
one of B<dotbox> (show a focus ring around the active element), B<none> |
| 42 |
(no special indication of active element) or B<underline> (underline the |
| 43 |
active element). The default is B<underline>. |
| 44 |
|
| 45 |
=item Name: B<height> |
| 46 |
|
| 47 |
=item Class: B<Height> |
| 48 |
|
| 49 |
=item Switch: B<-height> |
| 50 |
|
| 51 |
Specifies the desired height for the window, in lines. |
| 52 |
If zero or less, then the desired height for the window is made just |
| 53 |
large enough to hold all the elements in the listbox. |
| 54 |
|
| 55 |
=item Name: B<listVariable> |
| 56 |
|
| 57 |
=item Class: B<Variable> |
| 58 |
|
| 59 |
=item Switch: B<-listvariable> |
| 60 |
|
| 61 |
I<The following is only partially implemented in Perl/Tk:> |
| 62 |
|
| 63 |
Specifies the reference of a variable. The value of the variable is an array |
| 64 |
to be displayed inside the widget; if the variable value changes |
| 65 |
then the widget will automatically update itself to reflect the new |
| 66 |
value. Attempts to assign a variable with an invalid list value to |
| 67 |
B<-listvariable> will cause an error. Attempts to unset a variable in use |
| 68 |
as a B<-listvariable> will fail but will not generate an error. |
| 69 |
|
| 70 |
=item Name: B<selectMode> |
| 71 |
|
| 72 |
=item Class: B<SelectMode> |
| 73 |
|
| 74 |
=item Switch: B<-selectmode> |
| 75 |
|
| 76 |
Specifies one of several styles for manipulating the selection. |
| 77 |
The value of the option may be arbitrary, but the default bindings |
| 78 |
expect it to be either B<single>, B<browse>, B<multiple>, |
| 79 |
or B<extended>; the default value is B<browse>. |
| 80 |
|
| 81 |
=item Name: B<state> |
| 82 |
|
| 83 |
=item Class: B<State> |
| 84 |
|
| 85 |
=item Switch: B<-state> |
| 86 |
|
| 87 |
Specifies one of two states for the listbox: B<normal> or B<disabled>. |
| 88 |
If the listbox is disabled then items may not be inserted or deleted, |
| 89 |
items are drawn in the B<-disabledforeground> color, and selection |
| 90 |
cannot be modified and is not shown (though selection information is |
| 91 |
retained). |
| 92 |
|
| 93 |
=item Name: B<width> |
| 94 |
|
| 95 |
=item Class: B<Width> |
| 96 |
|
| 97 |
=item Switch: B<-width> |
| 98 |
|
| 99 |
Specifies the desired width for the window in characters. |
| 100 |
If the font doesn't have a uniform width then the width of the |
| 101 |
character ``0'' is used in translating from character units to |
| 102 |
screen units. |
| 103 |
If zero or less, then the desired width for the window is made just |
| 104 |
large enough to hold all the elements in the listbox. |
| 105 |
|
| 106 |
=back |
| 107 |
|
| 108 |
=head1 DESCRIPTION |
| 109 |
|
| 110 |
The B<Listbox> method creates a new window (given by the |
| 111 |
$widget argument) and makes it into a listbox widget. |
| 112 |
Additional |
| 113 |
options, described above, may be specified on the command line |
| 114 |
or in the option database |
| 115 |
to configure aspects of the listbox such as its colors, font, |
| 116 |
text, and relief. The B<listbox> command returns its |
| 117 |
$widget argument. At the time this command is invoked, |
| 118 |
there must not exist a window named $widget, but |
| 119 |
$widget's parent must exist. |
| 120 |
|
| 121 |
A listbox is a widget that displays a list of strings, one per line. |
| 122 |
When first created, a new listbox has no elements. |
| 123 |
Elements may be added or deleted using methods described |
| 124 |
below. In addition, one or more elements may be selected as described |
| 125 |
below. |
| 126 |
If a listbox is exporting its selection (see B<exportSelection> |
| 127 |
option), then it will observe the standard X11 protocols |
| 128 |
for handling the selection. |
| 129 |
Listbox selections are available as type B<STRING>; |
| 130 |
the value of the selection will be the text of the selected elements, with |
| 131 |
newlines separating the elements. |
| 132 |
|
| 133 |
It is not necessary for all the elements to be |
| 134 |
displayed in the listbox window at once; commands described below |
| 135 |
may be used to change the view in the window. Listboxes allow |
| 136 |
scrolling in both directions using the standard B<xScrollCommand> |
| 137 |
and B<yScrollCommand> options. |
| 138 |
They also support scanning, as described below. |
| 139 |
|
| 140 |
=head1 INDICES |
| 141 |
|
| 142 |
Many of the methods for listboxes take one or more indices |
| 143 |
as arguments. |
| 144 |
An index specifies a particular element of the listbox, in any of |
| 145 |
the following ways: |
| 146 |
|
| 147 |
=over 4 |
| 148 |
|
| 149 |
=item I<number> |
| 150 |
|
| 151 |
Specifies the element as a numerical index, where 0 corresponds |
| 152 |
to the first element in the listbox. |
| 153 |
|
| 154 |
=item B<active> |
| 155 |
|
| 156 |
Indicates the element that has the location cursor. This element |
| 157 |
will be displayed with an underline when the listbox has the |
| 158 |
keyboard focus, and it is specified with the B<activate> |
| 159 |
method. |
| 160 |
|
| 161 |
=item B<anchor> |
| 162 |
|
| 163 |
Indicates the anchor point for the selection, which is set with the |
| 164 |
B<selection anchor> method. |
| 165 |
|
| 166 |
=item B<end> |
| 167 |
|
| 168 |
Indicates the end of the listbox. |
| 169 |
For most commands this refers to the last element in the listbox, |
| 170 |
but for a few commands such as B<index> and B<insert> |
| 171 |
it refers to the element just after the last one. |
| 172 |
|
| 173 |
=item B<@>I<x>B<,>I<y> |
| 174 |
|
| 175 |
Indicates the element that covers the point in the listbox window |
| 176 |
specified by I<x> and I<y> (in pixel coordinates). If no |
| 177 |
element covers that point, then the closest element to that |
| 178 |
point is used. |
| 179 |
|
| 180 |
=back |
| 181 |
|
| 182 |
In the method descriptions below, arguments named I<index>, |
| 183 |
I<first>, and I<last> always contain text indices in one of |
| 184 |
the above forms. |
| 185 |
|
| 186 |
=head1 WIDGET METHODS |
| 187 |
|
| 188 |
The B<Listbox> method creates a widget object. |
| 189 |
This object supports the B<configure> and B<cget> methods |
| 190 |
described in L<Tk::options> which can be used to enquire and |
| 191 |
modify the options described above. |
| 192 |
The widget also inherits all the methods provided by the generic |
| 193 |
L<Tk::Widget|Tk::Widget> class. |
| 194 |
|
| 195 |
The following additional methods are available for listbox widgets: |
| 196 |
|
| 197 |
=over 4 |
| 198 |
|
| 199 |
=item I<$listbox>-E<gt>B<activate>(I<index>) |
| 200 |
|
| 201 |
Sets the active element to the one indicated by I<index>. |
| 202 |
If I<index> is outside the range of elements in the listbox |
| 203 |
then the closest element is activated. |
| 204 |
The active element is drawn with an underline when the widget |
| 205 |
has the input focus, and its index may be retrieved with the |
| 206 |
index B<active>. |
| 207 |
|
| 208 |
=item I<$listbox>-E<gt>B<bbox>(I<index>) |
| 209 |
|
| 210 |
Returns a list of four numbers describing the bounding box of |
| 211 |
the text in the element given by I<index>. |
| 212 |
The first two elements of the list give the x and y coordinates |
| 213 |
of the upper-left corner of the screen area covered by the text |
| 214 |
(specified in pixels relative to the widget) and the last two |
| 215 |
elements give the width and height of the area, in pixels. |
| 216 |
If no part of the element given by I<index> is visible on the |
| 217 |
screen, |
| 218 |
or if I<index> refers to a non-existent element, |
| 219 |
then the result is an empty string; if the element is |
| 220 |
partially visible, the result gives the full area of the element, |
| 221 |
including any parts that are not visible. |
| 222 |
|
| 223 |
=item I<$listbox>-E<gt>B<curselection> |
| 224 |
|
| 225 |
Returns a list containing the numerical indices of |
| 226 |
all of the elements in the listbox that are currently selected. |
| 227 |
If there are no elements selected in the listbox then an empty |
| 228 |
string is returned. |
| 229 |
|
| 230 |
=item I<$listbox>-E<gt>B<delete>(I<first, >?I<last>?) |
| 231 |
|
| 232 |
Deletes one or more elements of the listbox. I<First> and I<last> |
| 233 |
are indices specifying the first and last elements in the range |
| 234 |
to delete. If I<last> isn't specified it defaults to |
| 235 |
I<first>, i.e. a single element is deleted. |
| 236 |
|
| 237 |
=item I<$listbox>-E<gt>B<get>(I<first, >?I<last>?) |
| 238 |
|
| 239 |
If I<last> is omitted, returns the contents of the listbox |
| 240 |
element indicated by I<first>, |
| 241 |
or an empty string if I<first> refers to a non-existent element. |
| 242 |
If I<last> is specified, the command returns a list whose elements |
| 243 |
are all of the listbox elements between I<first> and I<last>, |
| 244 |
inclusive. |
| 245 |
Both I<first> and I<last> may have any of the standard |
| 246 |
forms for indices. |
| 247 |
|
| 248 |
=item I<$listbox>-E<gt>B<index>(I<index>) |
| 249 |
|
| 250 |
Returns the integer index value that corresponds to I<index>. |
| 251 |
If I<index> is B<end> the return value is a count of the number |
| 252 |
of elements in the listbox (not the index of the last element). |
| 253 |
|
| 254 |
=item I<$listbox>-E<gt>B<insert>(I<index, >?I<element, element, ...>?) |
| 255 |
|
| 256 |
Inserts zero or more new elements in the list just before the |
| 257 |
element given by I<index>. If I<index> is specified as |
| 258 |
B<end> then the new elements are added to the end of the |
| 259 |
list. Returns an empty string. |
| 260 |
|
| 261 |
=item I<$listbox>-E<gt>B<itemcget>(I<index>, I<option>) |
| 262 |
|
| 263 |
Returns the current value of the item configuration option given by |
| 264 |
I<option>. Option may have any of the values accepted by the listbox |
| 265 |
B<itemconfigure> command. |
| 266 |
|
| 267 |
=item I<$listbox>-E<gt>B<itemconfigure>(I<index, >?I<option, value, |
| 268 |
option, value, ...>?) |
| 269 |
|
| 270 |
Query or modify the configuration options of an item in the listbox. |
| 271 |
If no option is specified, returns a list describing all of the |
| 272 |
available options for the item (see Tk_ConfigureInfo for information |
| 273 |
on the format of this list). If option is specified with no value, |
| 274 |
then the command returns a list describing the one named option (this |
| 275 |
list will be identical to the corresponding sublist of the value |
| 276 |
returned if no option is specified). If one or more option-value pairs |
| 277 |
are specified, then the command modifies the given widget option(s) to |
| 278 |
have the given value(s); in this case the command returns an empty |
| 279 |
string. The following options are currently supported for items: |
| 280 |
|
| 281 |
=over |
| 282 |
|
| 283 |
=item B<-background> =E<gt> I<color> |
| 284 |
|
| 285 |
I<Color> specifies the background color to use when displaying the |
| 286 |
item. It may have any of the forms accepted by Tk_GetColor. |
| 287 |
|
| 288 |
=item B<-foreground> =E<gt> I<color> |
| 289 |
|
| 290 |
I<Color> specifies the foreground color to use when displaying the |
| 291 |
item. It may have any of the forms accepted by Tk_GetColor. |
| 292 |
|
| 293 |
=item B<-selectbackground> =E<gt> I<color> |
| 294 |
|
| 295 |
I<Color> specifies the background color to use when displaying the |
| 296 |
item while it is selected. It may have any of the forms accepted by |
| 297 |
Tk_GetColor. |
| 298 |
|
| 299 |
=item B<-selectforeground> =E<gt> I<color> |
| 300 |
|
| 301 |
I<Color> specifies the foreground color to use when displaying the |
| 302 |
item while it is selected. It may have any of the forms accepted by |
| 303 |
Tk_GetColor. |
| 304 |
|
| 305 |
=back |
| 306 |
|
| 307 |
=item I<$listbox>-E<gt>B<nearest>(I<y>) |
| 308 |
|
| 309 |
Given a y-coordinate within the listbox window, this command returns |
| 310 |
the index of the (visible) listbox element nearest to that y-coordinate. |
| 311 |
|
| 312 |
=item I<$listbox>-E<gt>B<scan>(I<option, args>) |
| 313 |
|
| 314 |
This command is used to implement scanning on listboxes. It has |
| 315 |
two forms, depending on I<option>: |
| 316 |
|
| 317 |
=over 8 |
| 318 |
|
| 319 |
=item I<$listbox>-E<gt>B<scanMark>(I<x, y>) |
| 320 |
|
| 321 |
Records I<x> and I<y> and the current view in the listbox |
| 322 |
window; used in conjunction with later B<scan dragto> commands. |
| 323 |
Typically this command is associated with a mouse button press in |
| 324 |
the widget. It returns an empty string. |
| 325 |
|
| 326 |
=item I<$listbox>-E<gt>B<scanDragto>(I<x, y>.) |
| 327 |
|
| 328 |
This command computes the difference between its I<x> and I<y> |
| 329 |
arguments and the I<x> and I<y> arguments to the last |
| 330 |
B<scan mark> command for the widget. |
| 331 |
It then adjusts the view by 10 times the |
| 332 |
difference in coordinates. This command is typically associated |
| 333 |
with mouse motion events in the widget, to produce the effect of |
| 334 |
dragging the list at high speed through the window. The return |
| 335 |
value is an empty string. |
| 336 |
|
| 337 |
=back |
| 338 |
|
| 339 |
=item I<$listbox>-E<gt>B<see>(I<index>) |
| 340 |
|
| 341 |
Adjust the view in the listbox so that the element given by I<index> |
| 342 |
is visible. |
| 343 |
If the element is already visible then the command has no effect; |
| 344 |
if the element is near one edge of the window then the listbox |
| 345 |
scrolls to bring the element into view at the edge; otherwise |
| 346 |
the listbox scrolls to center the element. |
| 347 |
|
| 348 |
=item I<$listbox>-E<gt>B<selection>(I<option, arg>) |
| 349 |
|
| 350 |
This command is used to adjust the selection within a listbox. It |
| 351 |
has several forms, depending on I<option>: |
| 352 |
|
| 353 |
=over 8 |
| 354 |
|
| 355 |
=item I<$listbox>-E<gt>B<selectionAnchor>(I<index>) |
| 356 |
|
| 357 |
Sets the selection anchor to the element given by I<index>. |
| 358 |
If I<index> refers to a non-existent element, then the closest |
| 359 |
element is used. |
| 360 |
The selection anchor is the end of the selection that is fixed |
| 361 |
while dragging out a selection with the mouse. |
| 362 |
The index B<anchor> may be used to refer to the anchor |
| 363 |
element. |
| 364 |
|
| 365 |
=item I<$listbox>-E<gt>B<selectionClear>(I<first, >?I<last>?) |
| 366 |
|
| 367 |
If any of the elements between I<first> and I<last> |
| 368 |
(inclusive) are selected, they are deselected. |
| 369 |
The selection state is not changed for elements outside |
| 370 |
this range. |
| 371 |
|
| 372 |
=item I<$listbox>-E<gt>B<selectionIncludes>(I<index>) |
| 373 |
|
| 374 |
Returns 1 if the element indicated by I<index> is currently |
| 375 |
selected, 0 if it isn't. |
| 376 |
|
| 377 |
=item I<$listbox>-E<gt>B<selectionSet>(I<first, >?I<last>?) |
| 378 |
|
| 379 |
Selects all of the elements in the range between |
| 380 |
I<first> and I<last>, inclusive, without affecting |
| 381 |
the selection state of elements outside that range. |
| 382 |
|
| 383 |
=back |
| 384 |
|
| 385 |
=item I<$listbox>-E<gt>B<size> |
| 386 |
|
| 387 |
Returns a decimal string indicating the total number of elements |
| 388 |
in the listbox. |
| 389 |
|
| 390 |
=item I<$listbox>-E<gt>B<xview>(I<args>) |
| 391 |
|
| 392 |
This command is used to query and change the horizontal position of the |
| 393 |
information in the widget's window. It can take any of the following |
| 394 |
forms: |
| 395 |
|
| 396 |
=over 8 |
| 397 |
|
| 398 |
=item I<$listbox>-E<gt>B<xview> |
| 399 |
|
| 400 |
Returns a list containing two elements. |
| 401 |
Each element is a real fraction between 0 and 1; together they describe |
| 402 |
the horizontal span that is visible in the window. |
| 403 |
For example, if the first element is .2 and the second element is .6, |
| 404 |
20% of the listbox's text is off-screen to the left, the middle 40% is visible |
| 405 |
in the window, and 40% of the text is off-screen to the right. |
| 406 |
These are the same values passed to scrollbars via the B<-xscrollcommand> |
| 407 |
option. |
| 408 |
|
| 409 |
=item I<$listbox>-E<gt>B<xview>(I<index>) |
| 410 |
|
| 411 |
Adjusts the view in the window so that the character position given by |
| 412 |
I<index> is displayed at the left edge of the window. |
| 413 |
Character positions are defined by the width of the character B<0>. |
| 414 |
|
| 415 |
=item I<$listbox>-E<gt>B<xviewMoveto>( I<fraction> ); |
| 416 |
|
| 417 |
Adjusts the view in the window so that I<fraction> of the |
| 418 |
total width of the listbox text is off-screen to the left. |
| 419 |
I<fraction> must be a fraction between 0 and 1. |
| 420 |
|
| 421 |
=item I<$listbox>-E<gt>B<xviewScroll>( I<number, what> ); |
| 422 |
|
| 423 |
This command shifts the view in the window left or right according to |
| 424 |
I<number> and I<what>. |
| 425 |
I<Number> must be an integer. |
| 426 |
I<What> must be either B<units> or B<pages> or an abbreviation |
| 427 |
of one of these. |
| 428 |
If I<what> is B<units>, the view adjusts left or right by |
| 429 |
I<number> character units (the width of the B<0> character) |
| 430 |
on the display; if it is B<pages> then the view adjusts by |
| 431 |
I<number> screenfuls. |
| 432 |
If I<number> is negative then characters farther to the left |
| 433 |
become visible; if it is positive then characters farther to the right |
| 434 |
become visible. |
| 435 |
|
| 436 |
=back |
| 437 |
|
| 438 |
=item I<$listbox>-E<gt>B<yview>(I<?args>?) |
| 439 |
|
| 440 |
This command is used to query and change the vertical position of the |
| 441 |
text in the widget's window. |
| 442 |
It can take any of the following forms: |
| 443 |
|
| 444 |
=over 8 |
| 445 |
|
| 446 |
=item I<$listbox>-E<gt>B<yview> |
| 447 |
|
| 448 |
Returns a list containing two elements, both of which are real fractions |
| 449 |
between 0 and 1. |
| 450 |
The first element gives the position of the listbox element at the |
| 451 |
top of the window, relative to the listbox as a whole (0.5 means |
| 452 |
it is halfway through the listbox, for example). |
| 453 |
The second element gives the position of the listbox element just after |
| 454 |
the last one in the window, relative to the listbox as a whole. |
| 455 |
These are the same values passed to scrollbars via the B<-yscrollcommand> |
| 456 |
option. |
| 457 |
|
| 458 |
=item I<$listbox>-E<gt>B<yview>(I<index>) |
| 459 |
|
| 460 |
Adjusts the view in the window so that the element given by |
| 461 |
I<index> is displayed at the top of the window. |
| 462 |
|
| 463 |
=item I<$listbox>-E<gt>B<yviewMoveto>( I<fraction> ); |
| 464 |
|
| 465 |
Adjusts the view in the window so that the element given by I<fraction> |
| 466 |
appears at the top of the window. |
| 467 |
I<Fraction> is a fraction between 0 and 1; 0 indicates the first |
| 468 |
element in the listbox, 0.33 indicates the element one-third the |
| 469 |
way through the listbox, and so on. |
| 470 |
|
| 471 |
=item I<$listbox>-E<gt>B<yviewScroll>( I<number, what> ); |
| 472 |
|
| 473 |
This command adjusts the view in the window up or down according to |
| 474 |
I<number> and I<what>. |
| 475 |
I<Number> must be an integer. |
| 476 |
I<What> must be either B<units> or B<pages>. |
| 477 |
If I<what> is B<units>, the view adjusts up or down by |
| 478 |
I<number> lines; if it is B<pages> then |
| 479 |
the view adjusts by I<number> screenfuls. |
| 480 |
If I<number> is negative then earlier elements |
| 481 |
become visible; if it is positive then later elements |
| 482 |
become visible. |
| 483 |
|
| 484 |
=back |
| 485 |
|
| 486 |
=back |
| 487 |
|
| 488 |
=head1 DEFAULT BINDINGS |
| 489 |
|
| 490 |
Tk automatically creates class bindings for listboxes that give them |
| 491 |
Motif-like behavior. Much of the behavior of a listbox is determined |
| 492 |
by its B<selectMode> option, which selects one of four ways |
| 493 |
of dealing with the selection. |
| 494 |
|
| 495 |
If the selection mode is B<single> or B<browse>, at most one |
| 496 |
element can be selected in the listbox at once. |
| 497 |
In both modes, clicking button 1 on an element selects |
| 498 |
it and deselects any other selected item. |
| 499 |
In B<browse> mode it is also possible to drag the selection |
| 500 |
with button 1. |
| 501 |
|
| 502 |
If the selection mode is B<multiple> or B<extended>, |
| 503 |
any number of elements may be selected at once, including discontiguous |
| 504 |
ranges. In B<multiple> mode, clicking button 1 on an element |
| 505 |
toggles its selection state without affecting any other elements. |
| 506 |
In B<extended> mode, pressing button 1 on an element selects |
| 507 |
it, deselects everything else, and sets the anchor to the element |
| 508 |
under the mouse; dragging the mouse with button 1 |
| 509 |
down extends the selection to include all the elements between |
| 510 |
the anchor and the element under the mouse, inclusive. |
| 511 |
|
| 512 |
Most people will probably want to use B<browse> mode for |
| 513 |
single selections and B<extended> mode for multiple selections; |
| 514 |
the other modes appear to be useful only in special situations. |
| 515 |
|
| 516 |
Any time the selection changes in the listbox, the virtual event |
| 517 |
B<<< <<ListboxSelect>> >>> will be generated. It is easiest to bind to this |
| 518 |
event to be made aware of any changes to listbox selection. |
| 519 |
|
| 520 |
|
| 521 |
In addition to the above behavior, the following additional behavior |
| 522 |
is defined by the default bindings: |
| 523 |
|
| 524 |
=over 4 |
| 525 |
|
| 526 |
=item [1] |
| 527 |
|
| 528 |
In B<extended> mode, the selected range can be adjusted by pressing |
| 529 |
button 1 with the Shift key down: this modifies the selection to |
| 530 |
consist of the elements between the anchor and the element under |
| 531 |
the mouse, inclusive. |
| 532 |
The un-anchored end of this new selection can also be dragged with |
| 533 |
the button down. |
| 534 |
|
| 535 |
=item [2] |
| 536 |
|
| 537 |
In B<extended> mode, pressing button 1 with the Control key down |
| 538 |
starts a toggle operation: the anchor is set to the element under |
| 539 |
the mouse, and its selection state is reversed. The selection state |
| 540 |
of other elements isn't changed. |
| 541 |
If the mouse is dragged with button 1 down, then the selection state |
| 542 |
of all elements between the anchor and the element under the mouse |
| 543 |
is set to match that of the anchor element; the selection state of |
| 544 |
all other elements remains what it was before the toggle operation |
| 545 |
began. |
| 546 |
|
| 547 |
=item [3] |
| 548 |
|
| 549 |
If the mouse leaves the listbox window with button 1 down, the window |
| 550 |
scrolls away from the mouse, making information visible that used |
| 551 |
to be off-screen on the side of the mouse. |
| 552 |
The scrolling continues until the mouse re-enters the window, the |
| 553 |
button is released, or the end of the listbox is reached. |
| 554 |
|
| 555 |
=item [4] |
| 556 |
|
| 557 |
Mouse button 2 may be used for scanning. |
| 558 |
If it is pressed and dragged over the listbox, the contents of |
| 559 |
the listbox drag at high speed in the direction the mouse moves. |
| 560 |
|
| 561 |
=item [5] |
| 562 |
|
| 563 |
If the Up or Down key is pressed, the location cursor (active |
| 564 |
element) moves up or down one element. |
| 565 |
If the selection mode is B<browse> or B<extended> then the |
| 566 |
new active element is also selected and all other elements are |
| 567 |
deselected. |
| 568 |
In B<extended> mode the new active element becomes the |
| 569 |
selection anchor. |
| 570 |
|
| 571 |
=item [6] |
| 572 |
|
| 573 |
In B<extended> mode, Shift-Up and Shift-Down move the location |
| 574 |
cursor (active element) up or down one element and also extend |
| 575 |
the selection to that element in a fashion similar to dragging |
| 576 |
with mouse button 1. |
| 577 |
|
| 578 |
=item [7] |
| 579 |
|
| 580 |
The Left and Right keys scroll the listbox view left and right |
| 581 |
by the width of the character B<0>. |
| 582 |
Control-Left and Control-Right scroll the listbox view left and |
| 583 |
right by the width of the window. |
| 584 |
Control-Prior and Control-Next also scroll left and right by |
| 585 |
the width of the window. |
| 586 |
|
| 587 |
=item [8] |
| 588 |
|
| 589 |
The Prior and Next keys scroll the listbox view up and down |
| 590 |
by one page (the height of the window). |
| 591 |
|
| 592 |
=item [9] |
| 593 |
|
| 594 |
The Home and End keys scroll the listbox horizontally to |
| 595 |
the left and right edges, respectively. |
| 596 |
|
| 597 |
=item [10] |
| 598 |
|
| 599 |
Control-Home sets the location cursor to the the first element in |
| 600 |
the listbox, selects that element, and deselects everything else |
| 601 |
in the listbox. |
| 602 |
|
| 603 |
=item [11] |
| 604 |
|
| 605 |
Control-End sets the location cursor to the the last element in |
| 606 |
the listbox, selects that element, and deselects everything else |
| 607 |
in the listbox. |
| 608 |
|
| 609 |
=item [12] |
| 610 |
|
| 611 |
In B<extended> mode, Control-Shift-Home extends the selection |
| 612 |
to the first element in the listbox and Control-Shift-End extends |
| 613 |
the selection to the last element. |
| 614 |
|
| 615 |
=item [13] |
| 616 |
|
| 617 |
In B<multiple> mode, Control-Shift-Home moves the location cursor |
| 618 |
to the first element in the listbox and Control-Shift-End moves |
| 619 |
the location cursor to the last element. |
| 620 |
|
| 621 |
=item [14] |
| 622 |
|
| 623 |
The space and Select keys make a selection at the location cursor |
| 624 |
(active element) just as if mouse button 1 had been pressed over |
| 625 |
this element. |
| 626 |
|
| 627 |
=item [15] |
| 628 |
|
| 629 |
In B<extended> mode, Control-Shift-space and Shift-Select |
| 630 |
extend the selection to the active element just as if button 1 |
| 631 |
had been pressed with the Shift key down. |
| 632 |
|
| 633 |
=item [16] |
| 634 |
|
| 635 |
In B<extended> mode, the Escape key cancels the most recent |
| 636 |
selection and restores all the elements in the selected range |
| 637 |
to their previous selection state. |
| 638 |
|
| 639 |
=item [17] |
| 640 |
|
| 641 |
Control-slash selects everything in the widget, except in |
| 642 |
B<single> and B<browse> modes, in which case it selects |
| 643 |
the active element and deselects everything else. |
| 644 |
|
| 645 |
=item [18] |
| 646 |
|
| 647 |
Control-backslash deselects everything in the widget, except in |
| 648 |
B<browse> mode where it has no effect. |
| 649 |
|
| 650 |
=item [19] |
| 651 |
|
| 652 |
The F16 key (labelled Copy on many Sun workstations) or Meta-w |
| 653 |
copies the selection in the widget to the clipboard, if there is |
| 654 |
a selection. |
| 655 |
|
| 656 |
The behavior of listboxes can be changed by defining new bindings for |
| 657 |
individual widgets or by redefining the class bindings. |
| 658 |
|
| 659 |
=back |
| 660 |
|
| 661 |
=head1 TIED INTERFACE |
| 662 |
|
| 663 |
The Tk::Listbox widget can also be tied to a scalar or array variable, with |
| 664 |
different behaviour depending on the variable type, with the following |
| 665 |
tie commands: |
| 666 |
|
| 667 |
use Tk; |
| 668 |
|
| 669 |
my ( @array, $scalar, $other ); |
| 670 |
my %options = ( ReturnType => "index" ); |
| 671 |
|
| 672 |
my $MW = MainWindow->new(); |
| 673 |
my $lbox = $MW->Listbox()->pack(); |
| 674 |
|
| 675 |
my @list = ( "a", "b", "c", "d", "e", "f" ); |
| 676 |
$lbox->insert('end', @list ); |
| 677 |
|
| 678 |
tie @array, "Tk::Listbox", $lbox |
| 679 |
tie $scalar, "Tk::Listbox", $lbox; |
| 680 |
tie $other, "Tk::Listbox", $lbox, %options; |
| 681 |
|
| 682 |
currently only one modifier is implemented, a 3 way flag for tied scalars |
| 683 |
"ReturnType" which can have values "element", "index" or "both". The default |
| 684 |
is "element". |
| 685 |
|
| 686 |
=over 4 |
| 687 |
|
| 688 |
=item Tied Arrays |
| 689 |
|
| 690 |
If you tie an array to the Listbox you can manipulate the items currently |
| 691 |
contained by the box in the same manner as a normal array, e.g. |
| 692 |
|
| 693 |
print @array; |
| 694 |
push(@array, @list); |
| 695 |
my $popped = pop(@array); |
| 696 |
my $shifted = shift(@array); |
| 697 |
unshift(@array, @list); |
| 698 |
delete $array[$index]; |
| 699 |
print $string if exists $array[$i]; |
| 700 |
@array = (); |
| 701 |
splice @array, $offset, $length, @list |
| 702 |
|
| 703 |
The delete function is implemented slightly differently from the standard |
| 704 |
array implementation. Instead of setting the element at that index to undef |
| 705 |
it instead physically removes it from the Listbox. This has the effect of |
| 706 |
changing the array indices, so for instance if you had a list on non-continuous |
| 707 |
indices you wish to remove from the Listbox you should reverse sort the list |
| 708 |
and then apply the delete function, e.g. |
| 709 |
|
| 710 |
my @list = ( 1, 2, 4, 12, 20 ); |
| 711 |
my @remove = reverse sort { $a <=> $b } @list; |
| 712 |
delete @array[@remove]; |
| 713 |
|
| 714 |
would safely remove indices 20, 12, 4, 2 and 1 from the Listbox without |
| 715 |
problems. It should also be noted that a similar warning applies to the |
| 716 |
splice function (which would normally be used in this context to perform |
| 717 |
the same job). |
| 718 |
|
| 719 |
|
| 720 |
=item Tied Scalars |
| 721 |
|
| 722 |
Unlike tied arrays, if you tie a scalar to the Listbox you can retrieve the |
| 723 |
currently selected elements in the box as an array referenced by the scalar, |
| 724 |
for instance |
| 725 |
|
| 726 |
my @list = ( "a", "b", "c", "d", "e", "f" ); |
| 727 |
$lbox->insert('end', sort @list ); |
| 728 |
$lbox->selectionSet(1); |
| 729 |
|
| 730 |
inserts @list as elements in an already existing listbox and selects the |
| 731 |
element at index 1, which is "b". If we then |
| 732 |
|
| 733 |
print @$selected; |
| 734 |
|
| 735 |
this will return the currently selected elements, in this case "b". |
| 736 |
|
| •737 |
However, if the "ReturnType" argument is passed when tying the Listbox to the |
| 738 |
scalar with value "index" then the indices of the selected elements will be |
| 739 |
returned instead of the elements themselves, ie in this case "1". This can be |
| 740 |
useful when manipulating both contents and selected elements in the Listbox at |
| 741 |
the same time. |
| 742 |
|
| 743 |
Importantly, if a value "both" is given the scalar will not be tied to an |
| 744 |
array, but instead to a hash, with keys being the indices and values being |
| 745 |
the elements at those indices |
| 746 |
|
| 747 |
You can also manipulate the selected items using the scalar. Equating the |
| 748 |
scalar to an array reference will select any elements that match elements |
| 749 |
in the Listbox, non-matching array items are ignored, e.g. |
| 750 |
|
| 751 |
my @list = ( "a", "b", "c", "d", "e", "f" ); |
| 752 |
$lbox->insert('end', sort @list ); |
| 753 |
$lbox->selectionSet(1); |
| 754 |
|
| 755 |
would insert the array @list into an already existing Listbox and select |
| 756 |
element at index 1, i.e. "b" |
| 757 |
|
| 758 |
@array = ( "a", "b", "f" ); |
| 759 |
$selected = \@array; |
| 760 |
|
| 761 |
would select elements "a", "b" and "f" in the Listbox. |
| 762 |
|
| 763 |
Again, if the "index" we indicate we want to use indices in the options hash |
| 764 |
then the indices are use instead of elements, e.g. |
| 765 |
|
| 766 |
@array = ( 0, 1, 5 ); |
| 767 |
$selected = \@array; |
| 768 |
|
| 769 |
would have the same effect, selecting elements "a", "b" and "f" if the |
| 770 |
$selected variable was tied with %options = ( ReturnType => "index" ). |
| 771 |
|
| 772 |
If we are returning "both", i.e. the tied scalar points to a hash, both key and |
| 773 |
value must match, e.g. |
| 774 |
|
| 775 |
%hash = ( 0 => "a", 1 => "b", 5 => "f" ); |
| 776 |
$selected = \%hash; |
| 777 |
|
| 778 |
would have the same effect as the previous examples. |
| 779 |
|
| 780 |
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 |
| 781 |
you must pass a reference to the tied scalar between subroutines. |
| 782 |
|
| 783 |
=back |
| 784 |
|
| 785 |
=head1 KEYWORDS |
| 786 |
|
| 787 |
listbox, widget, tied |
| 788 |
|
| 789 |
=head1 SEE ALSO |
| 790 |
|
| 791 |
L<Tk::HList>, L<Tk::TextList>. |
| 792 |
|
| 793 |
=cut |
| 794 |
|