All Manuals > CAPI User Guide > 9 Creating Menus


9.2 Grouping menu items together

The menu-component class lets you group related items together in a menu. This allows similar menu items to share properties, such as callbacks, and to be visually separated from other items in the menus. Menu components are actually choices.

Here is a simple example of a menu component. This creates a menu called Items , which has four items. Menu 1 and Menu 2 are ordinary menu items, but Item 1 and Item 2 are created from a menu component, and are therefore grouped together in the menu.

 (setq component (make-instance 'menu-component
                     :items '("item 1" "item2")
                     :print-function 'string-capitalize
                     :callback 'test-callback))
(contain (make-instance 'menu
                        :title "Items"
                        (list "menu 1" component "menu 2")
                        :print-function 'string-capitalize
                        :callback 'hello)
         :width 150
         :height 0)

Figure 9.1 A menu

Menu components allow you to specify, via the :interaction keyword, selectable menu items -- either as multiple-selection or single-selection items. This is like having radio buttons or check boxes as items in a menu, and is a popular technique among many GUI-based applications.

The following example shows you how to include a panel of radio buttons in a menu.

(setq radio (make-instance 'menu-component 
                :interaction :single-selection 
                :items '("This" "That")
                :callback 'hello))
(setq commands (make-instance 'menu 
                :title "Commands" 
                 (list "Command 1" radio "Command 2")
                :callback 'test-callback))
(contain commands)

Figure 9.2 Radio buttons included in a menu

The menu items This and That are radio buttons, only one of which may be selected at a time. The other menu items are just ordinary commands, as you saw in the previous examples. Note that the CAPI automatically groups the items which are parts of a menu component so that they are separated from other items in the menu.

This example also illustrates the use of more than one callback in a menu, which of course is the usual case when you are developing real applications. Choosing either of the radio buttons displays one message on the screen, and choosing either Command1 or Command2 returns the arguments of the callback.

Checked menu items can be created by specifying :multiple-selection to the :interaction keyword, as illustrated below.

(setq letters (make-instance 'menu-component
                  :interaction :multiple-selection
                  :items (list "Alpha" "Beta")))
(contain (make-instance 'menu
                  :title "Greek"
                  :items (list letters)
                  :callback 'test-callback))

Figure 9.3 An example of checked menu items

Note how the items in the menu component inherit the callback given to the parent, eliminating the need to specify a separate callback for each item or component in the menu.

Within a menu or component, you can specify alternatives for a main menu item that are invoked by modifer keys. See Alternative menu items for more information.

CAPI User Guide (Unix version) - 30 Aug 2011