- Inherits from:
- com.apple.yellow.foundation.NSObject
- Package:
- com.apple.yellow.application
This class defines an object that manages an application's menus. An NSMenu object displays a list of items that a user can choose from. When an item is clicked, the NSMenu object may either issue a command directly (by sending an action message to a target object) or bring up another menu (a submenu) that offers further choices. An NSMenu object's choices are implemented with NSMenuItem objects. Each menu item can be configured either to send its action message to a target or to open a submenu.
It's typically more convenient to use Interface Builder to construct your application's menus-see Interface Builder's Help for more information about using this application. NSMenu and NSMenuItem provide you with additional flexibility to construct or modify your application's menus dynamically at run time.
Exactly one NSMenu created by the application is designated as the main menu for the application (with NSApplication's setMainMenu: method).
A free-standing main menu is displayed on top of all other windows whenever the application is active; the user can move it by dragging its title bar. When a submenu is opened, it appears attached to the right of its supermenu with a title bar, allowing the user to drag it away from its supermenu so that it remains on the screen. A detached submenu displays a close button to allow the user to dismiss it (the main menu, of course, never displays a close button). If the user moves a menu window while a submenu is attached, the submenu follows its supermenu. If a menu window lies partly off-screen, when the user tracks the mouse pointer to the edge of the screen, by holding down the mouse button and dragging the mouse pointer, the menu temporarily shifts onto the screen (along with any attached super- or submenus), allowing the user to access all of its items.
The submenus of a menu bar are typical drop-down menus, and submenus of these appear to the right or left, depending on the available screen space.
NSMenu supports the assignment of keyboard equivalents (command-key accelerators) to its menu items. Any menu item, except those that open submenus, can have a key equivalent, but whether they should have one depends on the user interface guidelines of the host system's. Unlike keyboard equivalents, mnemonics only function when their menu is active, and they can be assigned to menu items which open submenus.
The items that appear on menus belong to the NSMenuItem class, which simply adopts the NSMenuItem protocol and adds little other behavior. See the specifications of the NSMenuItem and NSMenuValidation protocols for more information.
By default, menus are autoenabled. "Autoenabling" refers to the ability of a menu to enable or disable its items after a user event by querying other objects in an application for the appropriate state. (A disabled menu item has a gray title and does not respond to mouse clicks or key equivalents.) For instance, if the user selects some text in a scroll view, the object responsible for managing that text could receive-as the target of menu items such as Cut, Copy, and Paste-the message validateMenuItem for each of those items (see informal protocol NSMenuValidation). It would implement this message to evaluate the current context and return whether the menu item should be enabled.
An NSMenu object locates the "validator" object for a menu item by testing for the existence of the following objects, in the given order:
By following these steps, the NSMenu ensures that the object receiving the action message is asked to validate the menu state.
If the NSMenu object cannot locate a "validator," it disables the menu item. If the validator responds to validateMenuItem , NSMenu asks it for the enabled state. If the validator does not respond to the message, NSMenu enables the menu item.
You can turn off autoenabling by sending setAutoenablesItems to the NSMenu
object with an argument of false
.
You should do this if your application explicitly controls the state
of each of its menu items (see the NSMenuItem protocol method setEnabled: ).
Although autoenabling occurs automatically upon each user event,
you can request it for other purposes with the update method.
- Constructors
- NSMenu
- Setting up menu commands
- insertItemAtIndex
- addItem
- removeItem
- removeItemAtIndex
- itemChanged
- Finding menu items
- itemWithTag
- itemWithTitle
- itemAtIndex
- numberOfItems
- itemArray
- Finding indices of menu items
- indexOfItem
- indexOfItemWithTitle
- indexOfItemWithTag
- indexOfItemWithTargetAndAction
- indexOfItemWithRepresentedObject
- indexOfItemWithSubmenu
- Managing submenus
- setSubmenuForItem
- submenuAction
- attachedMenu
- isAttached
- isTornOff
- locationForSubmenu
- supermenu
- setSupermenu
- Enabling and disabling menu items
- autoenablesItems
- setAutoenablesItems
- update
- Handling keyboard equivalents
- performKeyEquivalent
- Simulating mouse clicks
- performActionForItemAtIndex
- Setting the title
- setTitle
- title
- Setting the representing object
- setMenuRepresentation
- menuRepresentation
- Updating menu layout
- menuChangedMessagesEnabled
- setMenuChangedMessagesEnabled
- sizeToFit
- Displaying context-sensitive help
- helpRequested
public NSMenu()
public NSMenu(String aTitle)
public void addItem(NSMenuItem newItem)
public NSMenuItem addItem(
String aString,
NSSelector aSelector,
String keyEquiv)
null
.public NSMenu attachedMenu()
null
if
there's no such object.public boolean autoenablesItems()
See Also: setAutoenablesItems
public Object contextMenuRepresentation()
public void helpRequested(NSEvent event)
See Also: showContextHelpForObject (NSHelpManager)
public int indexOfItem(NSMenuItem anObject)
See Also: insertItemAtIndex, itemAtIndex
public int indexOfItemWithRepresentedObject(Object anObject)
See Also: insertItemAtIndex, itemAtIndex
public int indexOfItemWithSubmenu(NSMenu anObject)
See Also: insertItemAtIndex, itemAtIndex
public int indexOfItemWithTag(int aTag)
See Also: insertItemAtIndex, itemAtIndex
public int indexOfItemWithTargetAndAction(
Object anObject,
NSSelector actionSelector)
null
,
the first menu item in the receiver that has target anObject is
returned. If no menu item matching these criteria is in the menu,
the method returns -1.See Also: insertItemAtIndex, itemAtIndex
public int indexOfItemWithTitle(String aTitle)
See Also: insertItemAtIndex, itemAtIndex
public void insertItemAtIndex(
NSMenuItem newItem,
int index)
public NSMenuItem insertItemAtIndex(
String aString,
NSSelector aSelector,
String keyEquiv,
int index)
null
.See Also: addItem, itemAtIndex, removeItem
public boolean isAttached()
true
if
the receiver is currently attached to another menu, false otherwise.public boolean isTornOff()
false
if
the receiver is off-screen or attached to another menu (or if it's
the main menu), true
otherwise.public NSArray itemArray()
See Also: numberOfItems
public NSMenuItem itemAtIndex(int index)
public void itemChanged(NSMenuItem anObject)
public NSMenuItem itemWithTag(int aTag)
public NSMenuItem itemWithTitle(String aString)
public NSPoint locationForSubmenu(NSMenu aSubmenu)
public boolean menuChangedMessagesEnabled()
true
if
messages are being sent to the application's windows upon each
change to the menu, false
otherwise.See Also: setMenuChangedMessagesEnabled
public Object menuRepresentation()
See Also: setMenuRepresentation
public int numberOfItems()
See Also: itemArray
public void performActionForItemAtIndex(int index)
public boolean performKeyEquivalent(NSEvent theEvent)
public void removeItem(NSMenuItem anItem)
public void removeItemAtIndex(int index)
public void setAutoenablesItems(boolean flag)
true
,
menu items are automatically enabled and disabled. If flag is false
,
menu items are not automatically enabled or disabled. See the NSMenuValidation
protocol specification for more information.See Also: autoenablesItems
public void setContextMenuRepresentation(Object menuRep)
public void setMenuChangedMessagesEnabled(boolean flag)
false
, make
changes to the menu, and invoke the method again with true
.
This has the effect of batching changes and applying them all at
once.See Also: menuChangedMessagesEnabled
public void setMenuRepresentation(Object menuRep)
See Also: menuRepresentation
public void setSubmenuForItem(
NSMenu aMenu,
NSMenuItem anItem)
public void setSupermenu(NSMenu supermenu)
See Also: supermenu
public void setTearOffMenuRepresentation(Object menuRep)
public void setTitle(String aString)
See Also: title
public void sizeToFit()
public void submenuAction(Object sender)
public NSMenu supermenu()
null
if it has none.public Object tearOffMenuRepresentation()
public String title()
See Also: setTitle
public void update()
Key | Value |
"MenuItem" | The menu item that was chosen. |
This notification is posted just after the application invokes the action method (carried as instance data by the menu item) in the menu item's target object or, if no target is specified, in the first object in the responder chain that implements the action method.
Key | Value |
"MenuItem" | The menu item that was chosen. |
This notification is posted just after the application invokes the action method (carried as instance data by the menu item) in the menu item's target object or, if no target is specified, in the first object in the responder chain that implements the action method.