## 5.7 Listbox widget

### 5.7.1 Description

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. If a listbox is exporting its selection (see `exportselection` option), then it will observe the standard X11 protocols for handling the selection.

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 `lrscrollbar` and `tdscrollbar` options. They also support scanning, as described below.

Indices

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

- integer : Specifies the element as a numerical index, where 0 corresponds to the first element in the listbox.

- `active` : Indicates the element that has the location cursor. This element will be displayed with an underline when the listbox has the keyboard focus, and it is specified with the activate widget method.

- `anchor` : Indicates the anchor point for the selection, which is set with the selection `anchor` method.

- `end` : Indicates the end of the listbox. For some commands this means just after the last element; for other commands it means the last element.

- `'@'#X#','#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.

### 5.7.2 Parameters

- `1:LVS` : Specifies a list of virtual strings that represents the items in the listbox. This parameter can be only used with the `set()` and `get()` methods. `LVS` must be a list of virtual strings.

- `init:LVS` : Specifies a list of virtual strings that represents the items in the listbox. This parameter can be only used at creation time. `LVS` must be a list of virtual strings.

- `return:V` : When the window is closed, `V` is bound to a list of virtual strings representing the items in the listbox. `V` must be a free variable.

- `reload:V` : This parameter can be only used with the `get()` method and returns a list of virtual strings that represents the contents of the listbox. This is the same as the `1` option except that it is slower. However if you use weird methods for modifying the contents of the list (using the `tk` method for example) and bypassing the methods described here, you may find cases where you need to use the `reload` parameter to resynchronize the states of Tk and QTk.

- `listselection:LB` : Specifies a list of booleans. This list contains as many booleans as there are items in the listbox. For each item, the corresponding boolean in the list is true if the item is selected, false otherwise. `LB` must be a list of booleans.

- `firstselection:V` : Can be used with the `get()` method only. Binds V to the number of the first selected item.

- `background:C` or `bg:C` : Specifies the normal background color to use when displaying the widget. `C` must be a valid color (see Section 6.1).

- `borderwidth:P` : Specifies a non-negative value indicating the width of the 3-D border to draw around the outside of the widget (if such a border is being drawn; the `relief` option typically determines this). The value may also be used when drawing 3-D effects in the interior of the widget. `P` must be a valid screen distance (see Section 6.6).

- `cursor:A` : Specifies the mouse cursor to be used for the widget. `A` must be a valid cursor (see Section 6.7)

- `exportselection:B` : Specifies whether or not a selection in the widget should also be the X selection. If the selection is exported, then selecting in the widget deselects the current X selection, selecting outside the widget deselects any widget selection, and the widget will respond to selection retrieval requests when it has a selection. The default is usually for widgets to export selections. `B` must be a boolean.

- `font:F` : Specifies the font to use when drawing text inside the widget. This parameter can't be get at runtime, however it can be set. `F` must be a valid font (see Section 6.8)

- `height:I` : Specifies the desired height for the window, in lines. If zero or less, then the desired height for the window is made just large enough to hold all the elements in the listbox. `I` must be an integer.

- `highlightbackground:C` : Specifies the color to display in the traversal highlight region when the widget does not have the input focus. `C` must be a valid color (see Section 6.1)

- `highlightcolor:C` : Specifies the color to use for the traversal highlight rectangle that is drawn around the widget when it has the input focus. `C` must be a valid color (see Section 6.1)

- `highlightthickness:P` : Specifies a non-negative value indicating the width of the highlight rectangle to draw around the outside of the widget when it has the input focus. If the value is zero, no focus highlight is drawn around the widget. `P` must be a valid screen distance (see Section 6.6).

- `relief:A` : Specifies the 3-D effect desired for the widget. Must be one of these atoms : `raised`, `sunken`, `flat`, `ridge`, `solid` or `groove`. The value indicates how the interior of the widget should appear relative to its exterior; for example, `raised` means the interior of the widget should appear to protrude from the screen, relative to the exterior of the widget.

- `selectbackground:C` : Specifies the background color to use when displaying selected items. `C` must be a valid color (see Section 6.1).

- `selectborderwidth:P` : pecifies a non-negative value indicating the width of the 3-D border to draw around selected items. `P` must be a valid screen distance (see Section 6.6).

- `selectforeground:C` : Specifies the foreground color to use when displaying selected items. `C` must be a valid color (see Section 6.1).

- `setgrid:B` : Determines whether this widget controls the resizing grid for its top-level window. This option is typically used in text widgets, where the information in the widget has a natural size (the size of a character) and it makes sense for the window's dimensions to be integral numbers of these units. These natural window sizes form a grid. If the `setgrid` option is set to `true` then the widget will communicate with the window manager so that when the user interactively resizes the top-level window that contains the widget, the dimensions of the window will be displayed to the user in grid units and the window size will be constrained to integral numbers of grid units. `B` must be a boolean.

- `takefocus:B` : Determines whether the window accepts the focus during keyboard traversal (e.g., Tab and Shift-Tab). Before setting the focus to a window, the traversal mechanims consult the value of the takeFocus option. A value of `false` means that the window should be skipped entirely during keyboard traversal. `true` means that the window should receive the input focus as long as it is viewable (it and all of its ancestors are mapped). `B` must be a boolean.

- `width:I` : Specifies the desired width for the window in characters. If the font doesn't have a uniform width then the width of the character ``0'' is used in translating from character units to screen units. If zero or less, then the desired width for the window is made just large enough to hold all the elements in the listbox. `I` must be an integer.

- `selectmode:A` : Specifies one of several styles for manipulating the selection. The value of `A` may be arbitrary, but the default bindings expect it to be either one of the following atoms : `single`, `browse`, `multiple`, or `extended`; the default value is `browse`.

- `action:C` : Specifies an action to execute juste after the user changed the selection. `C` can take several different forms (see Section 4.3.1).

- `lrscrollbar:B` : Determines whether the widget has got a horizontal bar below. This option can be specified at creation time only. `B` must be a boolean.

- `tdscrollbar:B` : Determines whether the widget has got a vertical bar at the right. This option can be specified at creation time only. `B` must be a boolean.

- `scrollwidth:P` : Specifies the width of the vertical and horizontal scrollbars. `P` must be a valid screen distance (see Section 6.6)

- `glue:A` : Specifies how a widget must fit in its own available place. `A` must be an atom that is any combination of `n`, `s`, `w` and `e`. If a direction is specified, the corresponding border is glued to its neighboor or border of the frame if there is no neighboor. If a direction is not specified, the corresponding border if such that the widget takes just the width or height it needs to draw itself. If none of opposite directions is specified, the widget is centered. For more details, see Section 4.1.

- `padx:P` : Specifies how much horizontal external padding to leave on each side of the widget. This space is added outside the widget border. `P` must be a valid screen distance (see Section 6.6)

- `pady:P` : Specifies how much vertical external padding to leave on each side of the widget. This space is added outside the widget border. `P` must be a valid screen distance (see Section 6.6)

- `feature:A` : Specifies a feature name for the container object to reference the object controlling this widget. For more details, see Section 4.2. `A` must be an atom.

- `handle:V` : Specifies a variable to reference the object controlling this widget. This variable is binded when the window is built. For more details, see Section 4.2. `V` must be a free variable.

- `tooltips:VS` : Specifies a tooltip for the widget. A tooltip is a small message that appears when the mouse cursor if left still over the widget for a while. If `VS` is set to `nil`, no tooltip will appear at all. `VS` must be a virtual string.

- `look:L` : Specifies a look to use for the widget. A look specifies default values for the parameters of widgets. This parameter can be specified at creation time only. Moreover changing a look will not change the appearance of widgets that are already displayed with that look. See Section 6.2 for more details.

### 5.7.3 Interface

The object you get by an handle on the widget implements the following methods :

- `set(parameter1:value1 ... parameterX:valueX)` : Changes the value of one or more parameters. `parameterX` must be a valid parameter for the widget. `valueX` must be a valid value for the parameter.

- `get(parameter1:Variable1 ... parameterX:VariableX)` : Obtains the value of one or more parameters. `parameterX` must be a valid parameter for the widget. `VariableX` must be free variables which will receive the current value of the parameters.

- `bind(event:E
  args:LA
  action:A)` : Specifies an action `LA` to execute when the event `E` is triggered. The event can have parameters (like mouse coordinates for a mouse click). You must explicitly ask in the `LA` list the parameters you are interested for. For more details, see Section 4.3.3.

- `getFocus(force:B)` : if `B` is `false` (which is the default) : if the application currently has the input focus on window's display, this command resets the input focus for window's display to window. If the application doesn't currently have the input focus on window's display, window will be remembered as the focus for its top-level; the next time the focus arrives at the top-level, it will be redirected it to window. If `B` is `true` : sets the focus of window's display to window, even if the application doesn't currently have the input focus for the display. This command should be used sparingly, if at all. In normal usage, an application should not claim the focus for itself; instead, it should wait for the window manager to give it the focus. `B` must be a boolean.

- `setGrab(global:B)` : Sets a grab on the widget. If `B` is `true` then the grab is global, otherwise it is local (`B` false by default). If a grab was already in effect for this application then it is automatically released. If there is already a grab on the widget and it has the same global/local form as the requested grab, then the command does nothing. Local grab affects only the grabbing application: events will be reported to other applications as if the grab had never occurred. A global grab locks out all applications on the screen, so that only the given subtree of the grabbing application will be sensitive to pointer events (mouse button presses, mouse button releases, pointer motions, window entries, and window exits). During global grabs the window manager will not receive pointer events either. Warning : whe a grab is set, it is define for the whole Oz process so that it will prevent the user to do input to other windows as well. Moreover a grab is still effective even if the window that contains the grabbed widgets is hidden. `B` must be a boolean.

- `releaseGrab` : Releases the grab on the widget if there is one, otherwise does nothing.

- `getGrabStatus(1:V)` : Binds `V` to the atom `none` if there is no grab on the widget, to the atom `local` if there is a local grab and to `global` if there is a global grab. `V` must be a free variable.

- `'raise'(1:W)` : If the `W` argument is omitted then the command raises the widget so that it is above all of its siblings in the stacking order (it will not be obscured by any siblings and will obscure any siblings that overlap it). If `W` is specified then it must be an handle to another widget that is either a sibling of this widget or the descendant of a sibling of this widget. In this case the `'raise'` command will insert this widget into the stacking order just above `W`; this could end up either raising or lowering window.

- `lower(1:W)` : If the `W` argument is omitted then the command lowers the widget so that it is below all of its siblings in the stacking order (it will be obscured by any siblings and will not obscure any siblings that overlap it). If `W` is specified then it must be an handle to another widget that is either a sibling of this widget or the descendant of a sibling of this widget. In this case the `lower` command will insert this widget into the stacking order just below `W`; this could end up either raising or lowering window.

- `winfo(parameter1:value1 ... parameterX:valueX)` : This commands works like the `get` method, but for window-related information. For more details, see Section 6.9.

- `activate(I)` : Sets the active element to the one indicated by `I`. The active element is drawn with an underline when the widget has the input focus.

- `bbox(I V)` : Binds `V` to a list of four numbers describing the bounding box of the text in the element given by `I`. The first two elements of the list give the x and y coordinates of the upper-left corner of the screen area covered by the text (specified in pixels relative to the widget) and the last two elements give the width and height of the area, in pixels. If no part of the element given by index is visible on the screen then the result is an empty list; if the element is partially visible, the result gives the full area of the element, including any parts that are not visible.

- `delete(I ?J)` : Deletes one or more elements of the listbox. `I` and `J` are indices specifying the first and last elements in the range to delete. If `J` isn't specified it defaults to `I`, i.e. a single element is deleted.

- `index(A I)` : Binds I to an integer giving index value that corresponds to `A`.

- `insert(I LVS)` : Inserts the list of virtual strings `LVS` just before the element at position `I`. If `I` is the atom `end`, the list is inserted after all elements.

- `nearest(I V)` : Given `I`, a y-coordinate within the listbox window, this command binds `V` to the index of the (visible) listbox element nearest to that y-coordinate.

- `scan(...)` : This command is used to implement scanning on listboxes. It has two forms, depending on option:

  - `scan(mark X Y)` : Records X and Y and the listbox's current view; used in conjunction with later `scan(dragto ...)` commands. Typically this command is associated with a mouse button press in the widget and `X` and `Y` are the coordinates of the mouse. `X` and `Y` must be valid distances (see Section 6.6).

  - `scan(dragto X Y)` : This command computes the difference between its `X` and `Y` arguments (which are typically mouse coordinates) and the `X` and `Y` arguments to the last `scan(mark ...)` command for the widget. It then adjusts the view by 10 times the difference in coordinates. This command is typically associated with mouse motion events in the widget, to produce the effect of dragging the listbox at high speed through its window.

- `see(I)` : Adjust the view in the listbox so that the element given by `I` 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.

- `size(V)` : Binds `V` to an integer indicating the total number of elements in the listbox.