All Manuals > LispWorks IDE User Guide > 21 Example: Using The Interface Builder

NextPrevUpTopContentsIndex

21.6 Defining the callbacks

This section shows you how to create the callback functions you need to define in order to complete the working example.

  1. In an Editor tool, choose File > New or click to create a new file.
  2. Choose File > Save or click to save the file. Save it in the same directory you saved ib-example.lisp, and call this new file ib-callbacks.lisp.
  3. In the editor, specify the package for the callback definitions by typing the following into the ib-callbacks.lisp file:
  4. (in-package "COMMON-LISP-USER")
  5. Enter the function definitions given in the rest of this section.
  6. Choose File > Save or click to save the file when you have entered all the function definitions.

The functions that you need to define in this file are divided into the following categories:

21.6.1 Callbacks to update the display pane

One main function, update-selection, serves to update the display pane whenever selections are made in the graph pane or the list panel.

(defun update-selection (type data interface)
  (setf (capi:display-pane-text (selection-reader interface)) 
        (format nil "~A ~A" data type)))

The following three functions are the callbacks specified whenever a select, retract or extend action is performed in either the list panel or the graph pane. Each function is named according to the type of callback it is used for, and it simply calls update-selection with an additional argument denoting the callback type.

(defun update-selection-select (&rest args)
  (apply 'update-selection "selected" args))
(defun update-selection-retract (&rest args)
  (apply 'update-selection "deselected" args))
(defun update-selection-extend (&rest args)
  (apply 'update-selection "extended" args))

21.6.2 Callbacks to display data in a dialog

As with update-selection, one main function serves to display the data from any action in a dialog.

(defun display-in-dialog (type data interface)
  (capi:display-message
   "~S: ~A ~S"
   (capi:interface-title interface) type data))

The function display-selection-in-dialog is the action callback for both the graph pane and the list panel. It calls display-in-dialog, specifying one of the required arguments.

(defun display-selection-in-dialog (&rest args)
  (apply 'display-in-dialog "selected" args))

Note: Although only one action callback is specified in the example interface, the relevant functions have been defined in this modular way to allow for the possibility of extending the interface. For instance, you may decide at a later date that you want to display the information for an extended selection in a dialog, rather than in the display pane. You could do this by defining a new callback which calls display-in-dialog, passing it an appropriate argument.

21.6.3 Callbacks for menu items

Both menu items in the interface need a callback function. As with other callback functions, these are specified by defining a general callback, display-pane-selection, which displays, in a dialog, the current selection of any pane.

(defun display-pane-selection (reader data interface)
  (declare (ignore data))
  (capi:display-message "~S: ~S selected"
                        (capi:capi-object-name 
                         (funcall reader interface)) 
                        (capi:choice-selected-items 
                         (funcall reader interface))))

The following two functions call display-pane-selection, passing the reader of a pane as an argument. These functions are specified as the callbacks for the two menu items.

(defun display-graph-selection (&rest args)
  (apply 'display-pane-selection 'graph-reader args))
(defun display-list-selection (&rest args)
  (apply 'display-pane-selection 'list-reader args))

As with the other callback functions, specifying the callbacks in this way allows for easy extension of the example.

21.6.4 Other miscellaneous functions

Graph panes require a function which is used to plot information, called the children function. The value of the ROOTS attribute of a graph is passed as an argument to the children function in order to start the plot. The example interface uses the following simple children function. You already defined this if you have followed the example, but add it also in ib-callbacks.lisp:

(defun children-function (x)
  (when (< x 8)
    (list (* x 2) (1+ (* x 2)))))

Note: The ROOTS attribute of a graph pane has a default value of (1). This is generated automatically by the Interface Builder.

Finally, the function test-ib-example is used to create an instance of the example interface.

(defun test-ib-example ()
  (capi:display (make-instance 'ib-example 
                               :best-height 300
                               :best-width 200)))

LispWorks IDE User Guide (Unix version) - 13 Sep 2017

NextPrevUpTopContentsIndex