A collection
collects together a set of items, and provides functionality for accessing and displaying them.
The items in the collection.
A function that prints an item.
A comparison function between two items.
A function which returns the length of items.
The main use of collection
is as a part of the class choice
, which provides selection capabilities on top of the collection handling, and which is used by list panels, button panels and menus amongst others.
The items in the collection can either be arbitrary Common Lisp objects which can be printed with the print-function
, or can be instances of the CAPI class item in which case they are displayed with the text field of the item. The main difference is that non-CAPI items use the callbacks specified for the collection, whilst the CAPI items will use their callbacks in preference if they are specified.
By default, the items must be a sequence, but this can be changed by specifying an items-get-function
, an items-count-function
, and an item-map-function
. The items-get-function
should expect the items and an index, and should return the indexed item. The items-count-function
should expect the items and should return the number of them. The item-map-function
should expect the items, a function and a flag collect-results-p
, and should call the function on each of the items in return. If collect-results-p
is non- nil
, then it should also collect the results of these calls together in a list.
The following code uses push-button-panel
, a subclass of collection
.
(capi:contain (make-instance 'capi:push-button-panel
:items '(one two three)))
(capi:contain (make-instance
'capi:push-button-panel
:items '(one two three)
:print-function 'string-capitalize))
The following example provides a collection with all values from 1 to items
by providing a get-function
and a count-function
.
(capi:contain (make-instance
'capi:push-button-panel
:items 6
:items-get-function
#'(lambda (items index) (1+ index))
:items-count-function
#'(lambda (items) items)))
Here is an example demonstrating the use of CAPI items in a collections list of items to get more specific callbacks.
(defun specific-callback (data interface)
(capi:display-message "Specific callback for ~S"
data))
(defun generic-callback (data interface)
(capi:display-message "Ordinary callback for ~S"
data))
(capi:contain (make-instance
'capi:list-panel
:items (list (make-instance
'capi:item
:text "Special"
:data 1000
:selection-callback
'specific-callback)
2 3 4)
:selection-callback 'generic-callback)
:visible-min-width 200
:visible-min-height 200)