MENU statement
Syntax
To create or alter a menu:
MENU menuID, itemID, state [,string$]
To unhighlight the menu bar: MENU
Revised
February, 2002 (Release 6)
Description:
Use this statement to do any of the following:
- Add a new menu to the menu bar.
- Enable or disable a menu.
- Add a new item to an existing menu.
- Enable or disable a menu item.
- Add or remove a checkmark from a menu item.
- Change the text of a menu item.
- Specify a hierarchical submenu to be attached to a menu item
- Unhighlight the menu bar.
To add a new menu to the menu bar:
- Set the menuIDparameter to a number which is not already in use by an existing menu. Use a number in the range 1 through 31.
- Set the itemIDparameter to zero.
- Set the stateparameter either to_enableor_disable, depending on whether you want the menu to be initially enabled or dimmed (you can change this state later).
- Set the string$parameter to the text that you want to appear as the new menu's title.
This creates a new empty menu (see below to learn how to add items to the menu). The value you choose for menuID will determine the new menu's position on the menu bar; menus are automatically positioned from left to right in increasing order of their menuID numbers. Almost always, you'll want to assign your menus consecutive numbers starting with 1.
To enable or disable (dim) an existing menu:
- Set the menuIDparameter to the ID number of an existing menu.
- Set the itemIDparameter to zero.
- Set the state parameter to _enableor_disable.
- Do not specify the string$parameter (if you do, all the menu's items will go away!)
To add a new item to an existing menu:
- Set the menuIDparameter to the ID number of an existing menu.
- Set the itemIDparameter to a positive number which is not being used by any other item in the menu. This number determines the item's position in the menu; items are numbered consecutively from top to bottom starting with 1. If you "skip" an item, then either a blank space or a grey dividing line will appear in that position, depending on what version of System software you're using. Note that a grey dividing line between items has its own item ID number. You can create a grey dividing line by using the meta character "-" in thestring$parameter.
- Set the stateparameter to_enable,_disableor_checked, depending on what you want the item's initial state to be (you can change this state later).
- Set the string$parameter to the text that you want to appear in the item. Note that when you're adding a new item, certain special characters instring$won't appear in the item text but have other special meanings. Consult the "Meta Characters" table below.
To enable, disable (dim), or checkmark an existing item:
- Set the menuIDanditemIDparameters to an existing item in an existing menu.
- Set the stateparameter to_enable,_disableor_checked. Note that setting state to_enableor to_disablewill remove any existing checkmark on the item.
To change the text of an existing item:
- Set the menuIDanditemIDparameters to an existing item in an existing menu.
- Set the string$parameter to the desired text. Note that when you change the text of an existing item, all the characters instring$will appear in the item text, and none will be interpreted as "meta characters."
To specify a hierarchical submenu to be attached to a menu item:
- Set the menuIDparameter to the ID number of an existing menu; this is the "parent" menu which will contain the submenu.
- Set the itemIDparameter to a positive number which is not being used by any other item in the menu. This is the "parent" item to which the submenu will be attached.
- Set the stateparameter to the ID number of the submenu. This should be a number in the range 32 through 235 which is not being used by any other menu.
- Set the string$parameter to a string which ends with these two characters:"/" + CHR$(&1B).
Note: The above procedure will attach the submenu to the parent menu item, but it doesn't install the submenu. To install the submenu, you also need to call the Toolbox procedure InsertMenu. See the examples below.
To unhighlight the menu bar:
- Execute the MENUstatement without any parameters. The menu bar is automatically highlighted every time the user selects a menu item, and it remains highlighted until your program unhighlights it. By unhighlighting the menu bar, your program lets the user know that the action associated with that menu item has completed.
Meta Characters
The characters in this table have special meanings when they appear in the string$ parameter when you're adding a new menu item. Note that when you change the text of an existing item, all the characters in string$ will appear in the item text, and none will be interpreted as meta characters. The exception to this rule is a string that starts with a minus sign. The minus sign is a flag used by most menu definitions do draw a divider line. If your item needs to contain a minus sign, you may still display the item properly if you put a space before the character.
IMAGE WAS HERE
Creating Hierarchical Menus
You can use the following function to add a new menu item and attach a new hierarchical menu to it. You should set childMenuID to some number in the range 32 through 235 which is not being used by any existing menu.
LOCAL FN MakeHierMenu(parentMenuID,parentMenuItem,¬
    itemString$,childMenuID)
  title$ = "!"+chr$(childMenuID)+itemString$ + "/" + CHR$(&1B)
  MENU parentMenuID,parentMenuItem,,title$
  CALL INSERTMENU(FN NEWMENU(childMenuID,""), -1) 
END FN
After you have called FN MakeHierMenu, you can use the MENU statement to add new items to the hierarchical menu (set the menuID parameter to the value of childMenuID).
Items in the Apple Menu
You should use the APPLE MENU statement to add items to the top of the Apple Menu. After adding these items, you can use the MENU statement (with the menuID parameter set to _appleMenu) to alter the items (for example to enable or dim them).
Items in the Help Menu
You can add items to the bottom of the Help Menu by getting a handle for the Help Menu and then calling the AppendMenu procedure. You also need to find out the item number of your first Help item for use by your menu event handler (any existing items are handled by the Help Manager):
DIM AS INT OSErr, @ firstCustomHelpItem
DIM AS HANDLE @ hmHandle
#IF carbonlib
   OSErr = FN HMGETHELPMENU(hmHandle, firstCustomHelpItem)
#ELSE
   OSErr = FN HMGETHELPMENUHANDLE(hmHandle)
   firstCustomHelpItem = FN COUNTMITEMS(hmHandle)+1
#ENDIF
CALL APPENDMENU(hmHandle, "My Help")
After adding items to the Help Menu, you can use the MENU statement (with the menuID parameter set to _kHMHelpMenuID ) to alter the items.
Note:
Do not use the MENU statement to add new items to the Help Menu; use AppendMenu instead.
Removing Menus
Call the DeleteMenu procedure to remove a menu created by the MENU statement:
CALL DELETEMENU(menuID)
This may cause other menus in the menu bar to slide to the left to fill the gap; however, they still retain their original menu ID numbers.
Removing Menu Items
To remove all the items from a menu you created, use the MENU statement, specifying zero in the itemID parameter, and specifying a menu title in the string$ parameter.
To remove an individual item, use the GetMHandle function and the DelMenuItem procedure:
CALL DELMENUITEM(FN GETMHANDLE(menuID), itemID)
Note that this will renumber any items below the deleted item, as they move up to fill in the gap. Menu item numbers are always numbered consecutively starting with 1.
Example:
The following lines create a complete menu which also contains a hierarchical menu. This example makes use of the MakeHierMenu function defined above.
MENU 3,0,_enable,"Game"
MENU 3,1,_enable,"See High Scores/H"
MENU 3,2,_enable,"Reset High Scores/R"
MENU 3,3,_disable,"-"
FN MakeHierMenu(3,4,"Scenarios",100)
'Items in hierarchical menu:
MENU 100,1,_checked,"Level 1"
MENU 100,2,_enable,"Level 2"
MENU 100,3,_enable,"Level 3"
'It takes two MENU statements to include a
'special character like "!" in the text:
MENU 3,5,_enable,"dummy"   'This adds the item
MENU 3,5,_enable,"Play Now!"   'This alters the item
IMAGE WAS HERE
Contextual Menus
At about the time that the Appearance Manager came on to the scene, programmers began to use contextual menus. A contextual menu appears when the user clicks at a specific area in a window while holding down the control key. When this type of action takes place, you will receive (Appearance Manager Runtime only) a DIALOG(0) message of _cntxtMenuClick. DIALOG(_cntxtMenuClick) will be the window number of the window. At this point you may need to react by showing a menu under the cursor.
IMAGE WAS HERE
The following function builds and displays a menu and might be called in reaction to a contextual menu click.
LOCAL FN DoContextMenu( wNum as long )
  DIM @ selectionType AS LONG
  DIM @ menuID AS SHORT
  DIM @ menuItem AS SHORT
  DIM mHndl AS HANDLE
  DIM err AS OSSTATUS
  DIM helpItemString AS STR255
  mHndl = FN NEWMENU(255, "X")
  LONG IF mHndl
    INSERTMENU( mHndl, -1 )
    APPENDMENU( mHndl, ¬
      "ContextualMenu click in window" + str$( wNum ) )
    helpItemString = "My Custom Help"
    err = FN CONTEXTUALMENUSELECT( mHndl, ¬
      #gFBTheEvent.where, _nil, _kCMHelpItemNoHelp, ¬
      @helpItemString, #_nil, @selectionType, ¬
      @menuID, @menuItem )
    /*
    In this function, we don't actually do anything with the
    selectionType, menuID, or menuItem returned, but we
    could react to it right here
    */
    DISPOSEMENU( mHndl )
  END IF
END FN
See Also
MENU; MENU function; ON MENU FN; APPLE MENU; DEF CHECKONEITEM