xref: /inferno-os/man/9/listbox (revision 46439007cf417cbd9ac8049bb4122c890097a0fa)
LISTBOX 9
NAME
listbox - Create and manipulate listbox widgets
SYNOPSIS
\f5listbox pathName ?options?
STANDARD OPTIONS
.EX -background -highlightcolor -selectforeground -borderwidth -highlightthickness -takefocus -font -relief -width -foreground -selectbackground -xscrollcommand -height -selectborderwidth -yscrollcommand
"WIDGET-SPECIFIC OPTIONS"

-height dist Specifies the desired height for the window.

-selectmode val Specifies one of several styles for manipulating the selection. The value of the option may be arbitrary, but the default bindings expect it to be either \f5single, \f5browse, \f5multiple, or \f5extended; the default value is \f5single.

-width dist Specifies the desired width for the window.

DESCRIPTION
The \f5listbox command creates a new window (given by the pathName argument) and makes it into a listbox widget. Additional options, described above, may be specified on the command line to configure aspects of the listbox such as its colours, font, text, and relief. The \f5listbox command returns its pathName argument. At the time this command is invoked, there must not exist a window named pathName.

A listbox is a widget that displays a list of strings, one per line. When first created, a new listbox has no elements. Elements may be added or deleted using widget commands described below. In addition, one or more elements may be selected as described below.

It is not necessary for all the elements to be displayed in the listbox window at once; commands described below may be used to change the view in the window. Listboxes allow scrolling in both directions using the standard \f5-xscrollcommand and \f5-yscrollcommand options.

INDICES
Many of the widget commands for listboxes take one or more indices as arguments. An index specifies a particular element of the listbox, in any of the following ways:

12 number Specifies the element as a numerical index, where 0 corresponds to the first element in the listbox.

12 \f5active Indicates the element that has the location cursor. This element will be displayed with a highlight rectangle when the listbox has the keyboard focus, and it is specified with the \f5activate widget command.

12 \f5anchor Indicates the anchor point for the selection, which is set with the \f5selection anchor widget command.

12 \f5end Indicates the end of the listbox. For some commands this means just after the last element; for other commands it means the last element.

12 \f5@x\f5,y Indicates the element that covers the point in the listbox window specified by x and y (in pixel coordinates). If no element covers that point, then the closest element to that point is used.

In the widget command descriptions below, arguments named index, first, and last always contain text indices in one of the above forms.

"WIDGET COMMAND"
The \f5listbox command creates a new Tk command whose name is pathName. This command may be used to invoke various operations on the widget. It has the following general form:

.EX pathName option ?arg arg ...?

Option and the args determine the exact behaviour of the command. The following commands are possible for listbox widgets:

pathName \f5activate index Sets the active element to the one indicated by index. The active element is drawn with an underline when the widget has the input focus, and its index may be retrieved with the index \f5active.

pathName \f5cget option Returns the current value of the configuration option given by option. Option may have any of the values accepted by the \f5listbox command.

pathName \f5configure ?option? ?value option value ...? Query or modify the configuration options of the widget. If no option is specified, returns a list of all of the available options for pathName. If one or more option-value pairs are specified, then the command modifies the given widget option(s) to have the given value(s); in this case the command returns an empty string. Option may have any of the values accepted by the \f5listbox command.

pathName \f5curselection Returns a list containing the numerical indices of all of the elements in the listbox that are currently selected. If there are no elements selected in the listbox then an empty string is returned.

pathName \f5delete first ?last? Deletes one or more elements of the listbox. First and last are indices specifying the first and last elements in the range to delete. If last isn't specified it defaults to first, i.e. a single element is deleted.

pathName \f5get first ?last? If last is omitted, returns the contents of the listbox element indicated by first. If last is specified, the command returns a list whose elements are all of the listbox elements between first and last, inclusive. Both first and last may have any of the standard forms for indices.

pathName \f5index index Returns a decimal string giving the integer index value that corresponds to index.

pathName \f5insert index ?element element ...? Inserts zero or more new elements in the list just before the element given by index. If index is specified as \f5end then the new elements are added to the end of the list. Returns an empty string.

pathName \f5nearest y Given a y-coordinate within the listbox window, this command returns the index of the (visible) listbox element nearest to that y-coordinate.

pathName \f5see index Adjust the view in the listbox so that the element given by index is visible. If the element is already visible then the command has no effect; if the element is near one edge of the window then the listbox scrolls to bring the element into view at the edge; otherwise the listbox scrolls to center the element.

pathName \f5selection option arg This command is used to adjust the selection within a listbox. It has several forms, depending on option:

pathName \f5selection anchor index Sets the selection anchor to the element given by index. The selection anchor is the end of the selection that is fixed while dragging out a selection with the mouse. The index \f5anchor may be used to refer to the anchor element.

pathName \f5selection clear first ?last? If any of the elements between first and last (inclusive) are selected, they are deselected. The selection state is not changed for elements outside this range.

pathName \f5selection includes index Returns 1 if the element indicated by index is currently selected, 0 if it isn't.

pathName \f5selection set first ?last? Selects all of the elements in the range between first and last, inclusive, without affecting the selection state of elements outside that range.

pathName \f5size Returns a decimal string indicating the total number of elements in the listbox.

pathName \f5xview args This command is used to query and change the horizontal position of the information in the widget's window. It can take any of the following forms:

pathName \f5xview Returns a list containing two elements. Each element is a real fraction between 0 and 1; together they describe the horizontal span that is visible in the window. For example, if the first element is .2 and the second element is .6, 20% of the listbox's text is off-screen to the left, the middle 40% is visible in the window, and 40% of the text is off-screen to the right. These are the same values passed to scrollbars via the \f5-xscrollcommand option.

pathName \f5xview index Adjusts the view in the window so that the character position given by index is displayed at the left edge of the window. Character positions are defined by the width of the character \f50.

pathName \f5xview moveto fraction Adjusts the view in the window so that fraction of the total width of the listbox text is off-screen to the left. fraction must be a fraction between 0 and 1.

pathName \f5xview scroll number what This command shifts the view in the window left or right according to number and what. Number must be an integer. What must be either \f5units or \f5pages. If what is \f5units, the view adjusts left or right by number character units (the width of the \f50 character) on the display; if it is \f5pages then the view adjusts by number screenfuls. If number is negative then characters farther to the left become visible; if it is positive then characters farther to the right become visible.

pathName \f5yview ?args? This command is used to query and change the vertical position of the text in the widget's window. It can take any of the following forms:

pathName \f5yview Returns a list containing two elements, both of which are real fractions between 0 and 1. The first element gives the position of the listbox element at the top of the window, relative to the listbox as a whole (0.5 means it is halfway through the listbox, for example). The second element gives the position of the listbox element just after the last one in the window, relative to the listbox as a whole. These are the same values passed to scrollbars via the \f5-yscrollcommand option.

pathName \f5yview index Adjusts the view in the window so that the element given by index is displayed at the top of the window.

pathName \f5yview moveto fraction Adjusts the view in the window so that the element given by fraction appears at the top of the window. Fraction is a fraction between 0 and 1; 0 indicates the first element in the listbox, 0.33 indicates the element one-third the way through the listbox, and so on.

pathName \f5yview scroll number what This command adjusts the view in the window up or down according to number and what. Number must be an integer. What must be either \f5units or \f5pages. If what is \f5units, the view adjusts up or down by number lines; if it is \f5pages then the view adjusts by number screenfuls. If number is negative then earlier elements become visible; if it is positive then later elements become visible.

"DEFAULT BINDINGS"
If the selection mode is \f5single or \f5browse, 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 \f5browse mode it is also possible to drag the selection with button 1.

If the selection mode is \f5multiple or \f5extended, any number of elements may be selected at once, including discontiguous ranges. In \f5multiple mode, clicking button 1 on an element toggles its selection state without affecting any other elements. In \f5extended 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 \f5browse mode for single selections and \f5extended mode for multiple selections; the other modes appear to be useful only in special situations.

The behaviour of listboxes can be changed by defining new bindings for individual widgets. The default bindings do a grab set when button 1 is pressed and a grab release when button 1 is released. Care must be taken when overriding either or both of these defaults to ensure that grabbing is consistent.

BUGS
At least one entry is required for the widget to indicate that it has keyboard focus.
SEE ALSO
options (9), types (9)