Commit | Line | Data |
---|---|---|
86530b38 AT |
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<-foreground> B<-relief> B<-takefocus> | |
21 | B<-borderwidth> B<-height> B<-selectbackground> B<-width> | |
22 | B<-cursor> B<-highlightbackground> B<-selectborderwidth> B<-xscrollcommand> | |
23 | B<-exportselection> B<-highlightcolor> B<-selectforeground> B<-yscrollcommand> | |
24 | B<-font> B<-highlightthickness> B<-setgrid> | |
25 | ||
26 | See L<Tk::options> for details of the standard options. | |
27 | ||
28 | =head1 WIDGET-SPECIFIC OPTIONS | |
29 | ||
30 | =over 4 | |
31 | ||
32 | =item Name: B<height> | |
33 | ||
34 | =item Class: B<Height> | |
35 | ||
36 | =item Switch: B<-height> | |
37 | ||
38 | Specifies the desired height for the window, in lines. | |
39 | If zero or less, then the desired height for the window is made just | |
40 | large enough to hold all the elements in the listbox. | |
41 | ||
42 | =item Name: B<selectMode> | |
43 | ||
44 | =item Class: B<SelectMode> | |
45 | ||
46 | =item Switch: B<-selectmode> | |
47 | ||
48 | Specifies one of several styles for manipulating the selection. | |
49 | The value of the option may be arbitrary, but the default bindings | |
50 | expect it to be either B<single>, B<browse>, B<multiple>, | |
51 | or B<extended>; the default value is B<browse>. | |
52 | ||
53 | =item Name: B<width> | |
54 | ||
55 | =item Class: B<Width> | |
56 | ||
57 | =item Switch: B<-width> | |
58 | ||
59 | Specifies the desired width for the window in characters. | |
60 | If the font doesn't have a uniform width then the width of the | |
61 | character ``0'' is used in translating from character units to | |
62 | screen units. | |
63 | If zero or less, then the desired width for the window is made just | |
64 | large enough to hold all the elements in the listbox. | |
65 | ||
66 | =back | |
67 | ||
68 | =head1 DESCRIPTION | |
69 | ||
70 | The B<Listbox> method creates a new window (given by the | |
71 | $widget argument) and makes it into a listbox widget. | |
72 | Additional | |
73 | options, described above, may be specified on the command line | |
74 | or in the option database | |
75 | to configure aspects of the listbox such as its colors, font, | |
76 | text, and relief. The B<listbox> command returns its | |
77 | $widget argument. At the time this command is invoked, | |
78 | there must not exist a window named $widget, but | |
79 | $widget's parent must exist. | |
80 | ||
81 | A listbox is a widget that displays a list of strings, one per line. | |
82 | When first created, a new listbox has no elements. | |
83 | Elements may be added or deleted using methods described | |
84 | below. In addition, one or more elements may be selected as described | |
85 | below. | |
86 | If a listbox is exporting its selection (see B<exportSelection> | |
87 | option), then it will observe the standard X11 protocols | |
88 | for handling the selection. | |
89 | Listbox selections are available as type B<STRING>; | |
90 | the value of the selection will be the text of the selected elements, with | |
91 | newlines separating the elements. | |
92 | ||
93 | It is not necessary for all the elements to be | |
94 | displayed in the listbox window at once; commands described below | |
95 | may be used to change the view in the window. Listboxes allow | |
96 | scrolling in both directions using the standard B<xScrollCommand> | |
97 | and B<yScrollCommand> options. | |
98 | They also support scanning, as described below. | |
99 | ||
100 | =head1 INDICES | |
101 | ||
102 | Many of the methods for listboxes take one or more indices | |
103 | as arguments. | |
104 | An index specifies a particular element of the listbox, in any of | |
105 | the following ways: | |
106 | ||
107 | =over 4 | |
108 | ||
109 | =item I<number> | |
110 | ||
111 | Specifies the element as a numerical index, where 0 corresponds | |
112 | to the first element in the listbox. | |
113 | ||
114 | =item B<active> | |
115 | ||
116 | Indicates the element that has the location cursor. This element | |
117 | will be displayed with an underline when the listbox has the | |
118 | keyboard focus, and it is specified with the B<activate> | |
119 | method. | |
120 | ||
121 | =item B<anchor> | |
122 | ||
123 | Indicates the anchor point for the selection, which is set with the | |
124 | B<selection anchor> method. | |
125 | ||
126 | =item B<end> | |
127 | ||
128 | Indicates the end of the listbox. | |
129 | For most commands this refers to the last element in the listbox, | |
130 | but for a few commands such as B<index> and B<insert> | |
131 | it refers to the element just after the last one. | |
132 | ||
133 | =item B<@>I<x>B<,>I<y> | |
134 | ||
135 | Indicates the element that covers the point in the listbox window | |
136 | specified by I<x> and I<y> (in pixel coordinates). If no | |
137 | element covers that point, then the closest element to that | |
138 | point is used. | |
139 | ||
140 | =back | |
141 | ||
142 | In the method descriptions below, arguments named I<index>, | |
143 | I<first>, and I<last> always contain text indices in one of | |
144 | the above forms. | |
145 | ||
146 | =head1 WIDGET METHODS | |
147 | ||
148 | The B<Listbox> method creates a widget object. | |
149 | This object supports the B<configure> and B<cget> methods | |
150 | described in L<Tk::options> which can be used to enquire and | |
151 | modify the options described above. | |
152 | The widget also inherits all the methods provided by the generic | |
153 | L<Tk::Widget|Tk::Widget> class. | |
154 | ||
155 | The following additional methods are available for listbox widgets: | |
156 | ||
157 | =over 4 | |
158 | ||
159 | =item I<$listbox>-E<gt>B<activate>(I<index>) | |
160 | ||
161 | Sets the active element to the one indicated by I<index>. | |
162 | If I<index> is outside the range of elements in the listbox | |
163 | then the closest element is activated. | |
164 | The active element is drawn with an underline when the widget | |
165 | has the input focus, and its index may be retrieved with the | |
166 | index B<active>. | |
167 | ||
168 | =item I<$listbox>-E<gt>B<bbox>(I<index>) | |
169 | ||
170 | Returns a list of four numbers describing the bounding box of | |
171 | the text in the element given by I<index>. | |
172 | The first two elements of the list give the x and y coordinates | |
173 | of the upper-left corner of the screen area covered by the text | |
174 | (specified in pixels relative to the widget) and the last two | |
175 | elements give the width and height of the area, in pixels. | |
176 | If no part of the element given by I<index> is visible on the | |
177 | screen, | |
178 | or if I<index> refers to a non-existent element, | |
179 | then the result is an empty string; if the element is | |
180 | partially visible, the result gives the full area of the element, | |
181 | including any parts that are not visible. | |
182 | ||
183 | =item I<$listbox>-E<gt>B<curselection> | |
184 | ||
185 | Returns a list containing the numerical indices of | |
186 | all of the elements in the listbox that are currently selected. | |
187 | If there are no elements selected in the listbox then an empty | |
188 | string is returned. | |
189 | ||
190 | =item I<$listbox>-E<gt>B<delete>(I<first, >?I<last>?) | |
191 | ||
192 | Deletes one or more elements of the listbox. I<First> and I<last> | |
193 | are indices specifying the first and last elements in the range | |
194 | to delete. If I<last> isn't specified it defaults to | |
195 | I<first>, i.e. a single element is deleted. | |
196 | ||
197 | =item I<$listbox>-E<gt>B<get>(I<first, >?I<last>?) | |
198 | ||
199 | If I<last> is omitted, returns the contents of the listbox | |
200 | element indicated by I<first>, | |
201 | or an empty string if I<first> refers to a non-existent element. | |
202 | If I<last> is specified, the command returns a list whose elements | |
203 | are all of the listbox elements between I<first> and I<last>, | |
204 | inclusive. | |
205 | Both I<first> and I<last> may have any of the standard | |
206 | forms for indices. | |
207 | ||
208 | =item I<$listbox>-E<gt>B<index>(I<index>) | |
209 | ||
210 | Returns the integer index value that corresponds to I<index>. | |
211 | If I<index> is B<end> the return value is a count of the number | |
212 | of elements in the listbox (not the index of the last element). | |
213 | ||
214 | =item I<$listbox>-E<gt>B<insert>(I<index, >?I<element, element, ...>?) | |
215 | ||
216 | Inserts zero or more new elements in the list just before the | |
217 | element given by I<index>. If I<index> is specified as | |
218 | B<end> then the new elements are added to the end of the | |
219 | list. Returns an empty string. | |
220 | ||
221 | =item I<$listbox>-E<gt>B<nearest>(I<y>) | |
222 | ||
223 | Given a y-coordinate within the listbox window, this command returns | |
224 | the index of the (visible) listbox element nearest to that y-coordinate. | |
225 | ||
226 | =item I<$listbox>-E<gt>B<scan>(I<option, args>) | |
227 | ||
228 | This command is used to implement scanning on listboxes. It has | |
229 | two forms, depending on I<option>: | |
230 | ||
231 | =over 8 | |
232 | ||
233 | =item I<$listbox>-E<gt>B<scanMark>(I<x, y>) | |
234 | ||
235 | Records I<x> and I<y> and the current view in the listbox | |
236 | window; used in conjunction with later B<scan dragto> commands. | |
237 | Typically this command is associated with a mouse button press in | |
238 | the widget. It returns an empty string. | |
239 | ||
240 | =item I<$listbox>-E<gt>B<scanDragto>(I<x, y>.) | |
241 | ||
242 | This command computes the difference between its I<x> and I<y> | |
243 | arguments and the I<x> and I<y> arguments to the last | |
244 | B<scan mark> command for the widget. | |
245 | It then adjusts the view by 10 times the | |
246 | difference in coordinates. This command is typically associated | |
247 | with mouse motion events in the widget, to produce the effect of | |
248 | dragging the list at high speed through the window. The return | |
249 | value is an empty string. | |
250 | ||
251 | =back | |
252 | ||
253 | =item I<$listbox>-E<gt>B<see>(I<index>) | |
254 | ||
255 | Adjust the view in the listbox so that the element given by I<index> | |
256 | is visible. | |
257 | If the element is already visible then the command has no effect; | |
258 | if the element is near one edge of the window then the listbox | |
259 | scrolls to bring the element into view at the edge; otherwise | |
260 | the listbox scrolls to center the element. | |
261 | ||
262 | =item I<$listbox>-E<gt>B<selection>(I<option, arg>) | |
263 | ||
264 | This command is used to adjust the selection within a listbox. It | |
265 | has several forms, depending on I<option>: | |
266 | ||
267 | =over 8 | |
268 | ||
269 | =item I<$listbox>-E<gt>B<selectionAnchor>(I<index>) | |
270 | ||
271 | Sets the selection anchor to the element given by I<index>. | |
272 | If I<index> refers to a non-existent element, then the closest | |
273 | element is used. | |
274 | The selection anchor is the end of the selection that is fixed | |
275 | while dragging out a selection with the mouse. | |
276 | The index B<anchor> may be used to refer to the anchor | |
277 | element. | |
278 | ||
279 | =item I<$listbox>-E<gt>B<selectionClear>(I<first, >?I<last>?) | |
280 | ||
281 | If any of the elements between I<first> and I<last> | |
282 | (inclusive) are selected, they are deselected. | |
283 | The selection state is not changed for elements outside | |
284 | this range. | |
285 | ||
286 | =item I<$listbox>-E<gt>B<selectionIncludes>(I<index>) | |
287 | ||
288 | Returns 1 if the element indicated by I<index> is currently | |
289 | selected, 0 if it isn't. | |
290 | ||
291 | =item I<$listbox>-E<gt>B<selectionSet>(I<first, >?I<last>?) | |
292 | ||
293 | Selects all of the elements in the range between | |
294 | I<first> and I<last>, inclusive, without affecting | |
295 | the selection state of elements outside that range. | |
296 | ||
297 | =back | |
298 | ||
299 | =item I<$listbox>-E<gt>B<size> | |
300 | ||
301 | Returns a decimal string indicating the total number of elements | |
302 | in the listbox. | |
303 | ||
304 | =item I<$listbox>-E<gt>B<xview>(I<args>) | |
305 | ||
306 | This command is used to query and change the horizontal position of the | |
307 | information in the widget's window. It can take any of the following | |
308 | forms: | |
309 | ||
310 | =over 8 | |
311 | ||
312 | =item I<$listbox>-E<gt>B<xview> | |
313 | ||
314 | Returns a list containing two elements. | |
315 | Each element is a real fraction between 0 and 1; together they describe | |
316 | the horizontal span that is visible in the window. | |
317 | For example, if the first element is .2 and the second element is .6, | |
318 | 20% of the listbox's text is off-screen to the left, the middle 40% is visible | |
319 | in the window, and 40% of the text is off-screen to the right. | |
320 | These are the same values passed to scrollbars via the B<-xscrollcommand> | |
321 | option. | |
322 | ||
323 | =item I<$listbox>-E<gt>B<xview>(I<index>) | |
324 | ||
325 | Adjusts the view in the window so that the character position given by | |
326 | I<index> is displayed at the left edge of the window. | |
327 | Character positions are defined by the width of the character B<0>. | |
328 | ||
329 | =item I<$listbox>-E<gt>B<xview>(B<moveto> =E<gt> I<fraction>) | |
330 | ||
331 | Adjusts the view in the window so that I<fraction> of the | |
332 | total width of the listbox text is off-screen to the left. | |
333 | I<fraction> must be a fraction between 0 and 1. | |
334 | ||
335 | =item I<$listbox>-E<gt>B<xview>(B<scroll> =E<gt> I<number, what>) | |
336 | ||
337 | This command shifts the view in the window left or right according to | |
338 | I<number> and I<what>. | |
339 | I<Number> must be an integer. | |
340 | I<What> must be either B<units> or B<pages> or an abbreviation | |
341 | of one of these. | |
342 | If I<what> is B<units>, the view adjusts left or right by | |
343 | I<number> character units (the width of the B<0> character) | |
344 | on the display; if it is B<pages> then the view adjusts by | |
345 | I<number> screenfuls. | |
346 | If I<number> is negative then characters farther to the left | |
347 | become visible; if it is positive then characters farther to the right | |
348 | become visible. | |
349 | ||
350 | =back | |
351 | ||
352 | =item I<$listbox>-E<gt>B<yview>(I<?args>?) | |
353 | ||
354 | This command is used to query and change the vertical position of the | |
355 | text in the widget's window. | |
356 | It can take any of the following forms: | |
357 | ||
358 | =over 8 | |
359 | ||
360 | =item I<$listbox>-E<gt>B<yview> | |
361 | ||
362 | Returns a list containing two elements, both of which are real fractions | |
363 | between 0 and 1. | |
364 | The first element gives the position of the listbox element at the | |
365 | top of the window, relative to the listbox as a whole (0.5 means | |
366 | it is halfway through the listbox, for example). | |
367 | The second element gives the position of the listbox element just after | |
368 | the last one in the window, relative to the listbox as a whole. | |
369 | These are the same values passed to scrollbars via the B<-yscrollcommand> | |
370 | option. | |
371 | ||
372 | =item I<$listbox>-E<gt>B<yview>(I<index>) | |
373 | ||
374 | Adjusts the view in the window so that the element given by | |
375 | I<index> is displayed at the top of the window. | |
376 | ||
377 | =item I<$listbox>-E<gt>B<yview>(B<moveto> =E<gt> I<fraction>) | |
378 | ||
379 | Adjusts the view in the window so that the element given by I<fraction> | |
380 | appears at the top of the window. | |
381 | I<Fraction> is a fraction between 0 and 1; 0 indicates the first | |
382 | element in the listbox, 0.33 indicates the element one-third the | |
383 | way through the listbox, and so on. | |
384 | ||
385 | =item I<$listbox>-E<gt>B<yview>(B<scroll> =E<gt> I<number, what>) | |
386 | ||
387 | This command adjusts the view in the window up or down according to | |
388 | I<number> and I<what>. | |
389 | I<Number> must be an integer. | |
390 | I<What> must be either B<units> or B<pages>. | |
391 | If I<what> is B<units>, the view adjusts up or down by | |
392 | I<number> lines; if it is B<pages> then | |
393 | the view adjusts by I<number> screenfuls. | |
394 | If I<number> is negative then earlier elements | |
395 | become visible; if it is positive then later elements | |
396 | become visible. | |
397 | ||
398 | =back | |
399 | ||
400 | =back | |
401 | ||
402 | =head1 DEFAULT BINDINGS | |
403 | ||
404 | Tk automatically creates class bindings for listboxes that give them | |
405 | Motif-like behavior. Much of the behavior of a listbox is determined | |
406 | by its B<selectMode> option, which selects one of four ways | |
407 | of dealing with the selection. | |
408 | ||
409 | If the selection mode is B<single> or B<browse>, at most one | |
410 | element can be selected in the listbox at once. | |
411 | In both modes, clicking button 1 on an element selects | |
412 | it and deselects any other selected item. | |
413 | In B<browse> mode it is also possible to drag the selection | |
414 | with button 1. | |
415 | ||
416 | If the selection mode is B<multiple> or B<extended>, | |
417 | any number of elements may be selected at once, including discontiguous | |
418 | ranges. In B<multiple> mode, clicking button 1 on an element | |
419 | toggles its selection state without affecting any other elements. | |
420 | In B<extended> mode, pressing button 1 on an element selects | |
421 | it, deselects everything else, and sets the anchor to the element | |
422 | under the mouse; dragging the mouse with button 1 | |
423 | down extends the selection to include all the elements between | |
424 | the anchor and the element under the mouse, inclusive. | |
425 | ||
426 | Most people will probably want to use B<browse> mode for | |
427 | single selections and B<extended> mode for multiple selections; | |
428 | the other modes appear to be useful only in special situations. | |
429 | ||
430 | In addition to the above behavior, the following additional behavior | |
431 | is defined by the default bindings: | |
432 | ||
433 | =over 4 | |
434 | ||
435 | =item [1] | |
436 | ||
437 | In B<extended> mode, the selected range can be adjusted by pressing | |
438 | button 1 with the Shift key down: this modifies the selection to | |
439 | consist of the elements between the anchor and the element under | |
440 | the mouse, inclusive. | |
441 | The un-anchored end of this new selection can also be dragged with | |
442 | the button down. | |
443 | ||
444 | =item [2] | |
445 | ||
446 | In B<extended> mode, pressing button 1 with the Control key down | |
447 | starts a toggle operation: the anchor is set to the element under | |
448 | the mouse, and its selection state is reversed. The selection state | |
449 | of other elements isn't changed. | |
450 | If the mouse is dragged with button 1 down, then the selection state | |
451 | of all elements between the anchor and the element under the mouse | |
452 | is set to match that of the anchor element; the selection state of | |
453 | all other elements remains what it was before the toggle operation | |
454 | began. | |
455 | ||
456 | =item [3] | |
457 | ||
458 | If the mouse leaves the listbox window with button 1 down, the window | |
459 | scrolls away from the mouse, making information visible that used | |
460 | to be off-screen on the side of the mouse. | |
461 | The scrolling continues until the mouse re-enters the window, the | |
462 | button is released, or the end of the listbox is reached. | |
463 | ||
464 | =item [4] | |
465 | ||
466 | Mouse button 2 may be used for scanning. | |
467 | If it is pressed and dragged over the listbox, the contents of | |
468 | the listbox drag at high speed in the direction the mouse moves. | |
469 | ||
470 | =item [5] | |
471 | ||
472 | If the Up or Down key is pressed, the location cursor (active | |
473 | element) moves up or down one element. | |
474 | If the selection mode is B<browse> or B<extended> then the | |
475 | new active element is also selected and all other elements are | |
476 | deselected. | |
477 | In B<extended> mode the new active element becomes the | |
478 | selection anchor. | |
479 | ||
480 | =item [6] | |
481 | ||
482 | In B<extended> mode, Shift-Up and Shift-Down move the location | |
483 | cursor (active element) up or down one element and also extend | |
484 | the selection to that element in a fashion similar to dragging | |
485 | with mouse button 1. | |
486 | ||
487 | =item [7] | |
488 | ||
489 | The Left and Right keys scroll the listbox view left and right | |
490 | by the width of the character B<0>. | |
491 | Control-Left and Control-Right scroll the listbox view left and | |
492 | right by the width of the window. | |
493 | Control-Prior and Control-Next also scroll left and right by | |
494 | the width of the window. | |
495 | ||
496 | =item [8] | |
497 | ||
498 | The Prior and Next keys scroll the listbox view up and down | |
499 | by one page (the height of the window). | |
500 | ||
501 | =item [9] | |
502 | ||
503 | The Home and End keys scroll the listbox horizontally to | |
504 | the left and right edges, respectively. | |
505 | ||
506 | =item [10] | |
507 | ||
508 | Control-Home sets the location cursor to the the first element in | |
509 | the listbox, selects that element, and deselects everything else | |
510 | in the listbox. | |
511 | ||
512 | =item [11] | |
513 | ||
514 | Control-End sets the location cursor to the the last element in | |
515 | the listbox, selects that element, and deselects everything else | |
516 | in the listbox. | |
517 | ||
518 | =item [12] | |
519 | ||
520 | In B<extended> mode, Control-Shift-Home extends the selection | |
521 | to the first element in the listbox and Control-Shift-End extends | |
522 | the selection to the last element. | |
523 | ||
524 | =item [13] | |
525 | ||
526 | In B<multiple> mode, Control-Shift-Home moves the location cursor | |
527 | to the first element in the listbox and Control-Shift-End moves | |
528 | the location cursor to the last element. | |
529 | ||
530 | =item [14] | |
531 | ||
532 | The space and Select keys make a selection at the location cursor | |
533 | (active element) just as if mouse button 1 had been pressed over | |
534 | this element. | |
535 | ||
536 | =item [15] | |
537 | ||
538 | In B<extended> mode, Control-Shift-space and Shift-Select | |
539 | extend the selection to the active element just as if button 1 | |
540 | had been pressed with the Shift key down. | |
541 | ||
542 | =item [16] | |
543 | ||
544 | In B<extended> mode, the Escape key cancels the most recent | |
545 | selection and restores all the elements in the selected range | |
546 | to their previous selection state. | |
547 | ||
548 | =item [17] | |
549 | ||
550 | Control-slash selects everything in the widget, except in | |
551 | B<single> and B<browse> modes, in which case it selects | |
552 | the active element and deselects everything else. | |
553 | ||
554 | =item [18] | |
555 | ||
556 | Control-backslash deselects everything in the widget, except in | |
557 | B<browse> mode where it has no effect. | |
558 | ||
559 | =item [19] | |
560 | ||
561 | The F16 key (labelled Copy on many Sun workstations) or Meta-w | |
562 | copies the selection in the widget to the clipboard, if there is | |
563 | a selection. | |
564 | ||
565 | The behavior of listboxes can be changed by defining new bindings for | |
566 | individual widgets or by redefining the class bindings. | |
567 | ||
568 | =back | |
569 | ||
570 | =head1 KEYWORDS | |
571 | ||
572 | listbox, widget | |
573 | ||
574 | =cut | |
575 |