5 Pop-Up Menus

5.2 Creating and using pop-up menus

To make a pop-up menu, you first must specify a list of options to the function make-pop-up-menu. This function returns the pop-up menu, which is given to the function pop-up-menu-choose to display. The font that is used in the display is contained in the variable *default-pop-up-menu-font*; see Chapter 4, "Fonts" for more information. The functionpop-up-menu-choose activates the menu and returns the menu choice that is made.

You can create and display a simple pop-up menu as follows:

(setq *menu1* (make-pop-up-menu '(Chocolate Vanilla Strawberry)))
(pop-up-menu-choose *menu1*)
To select an option from the currently displayed*menu1*, move the mouse cursor over the option you wish to select and click any mouse button. The pop-up menu immediately disappears, and Lisp returns the value of the item you selected and a keyword that corresponds to the mouse button you used to make the selection.

If you do not wish to select any of the items displayed by a pop-up menu, simply move the mouse cursor off of the menu, and the pop-up menu disappears. If the pop-up menu has a default value, that value is returned; otherwise,nil is returned.

In the previous example, the returned value is the item on the pop-up menu that is selected. You can specify an alternate return value for each item if you wish. When the choice-list argument to the functionmake-pop-up-menu is a list of conses, the car of each item is a string that is used as the display choice in the pop-up menu. The cdr of each item is used as the return value if that item is chosen.

(setq *menu2*
      (make-pop-up-menu
        '(("Chocolate" . flavor1)
          ("Vanilla" . flavor2)
          ("Strawberry" . flavor3))))

(pop-up-menu-choose *menu2*)

When you select an item, the menu disappears and Lisp returns the cdr of the element you selected.

Often you wish to invoke certain Lisp code that depends on the item selected in a pop-up menu. In the next example, the code that is executed changes when you choose a different option from the pop-up menu. There are three flavor options, and each one displays a different message.

(defun ice-cream ()
  (let ((selection (pop-up-menu-choose *menu2*)))
    (case selection
      (flavor1 (format t "Here is your cone.~%"))
      (flavor2 (format t "Sorry, we are out of this flavor today."))
      (flavor3 (format t "Special on this flavor today!")))))

Pop-up menus can invoke that code that displays other pop-up menus, as in the next example:

(setq *menu3*
      (make-pop-up-menu
        '(("sugar cone" . sugar) ("regular cone" . regular))
        'regular))
(defun ice-cream ()
  (let ((flavor (pop-up-menu-choose *menu2*))
        (cone nil))
    (if (or (eq flavor 'flavor1) (eq flavor 'flavor3))
        (setq cone (pop-up-menu-choose *menu3*))
        (format t "Sorry, we are out of this flavor today."))
    (values flavor cone)))

A default choice is given for the pop-up menu*menu3*. If you do not make a choice for*menu3*, this default value is returned.


The Window Tool Kit - 9 SEP 1996

Generated with Harlequin WebMaker