- Inherits from:
- NSView : NSResponder : NSObject
- Conforms to:
- NSCoding
- (NSResponder)
- NSObject (NSObject)
Declared in:
- AppKit/NSMenuView.h
The NSMenuView class handles the display of menus on the user's screen. A menu view displays its menu either horizontally or vertically and allows the user to interact with the items of that menu, either to navigate through hierarchical menus or to select a particular item.
Although NSMenuView handles the display of a menu's contents, it does not store those contents itself. Instead, NSMenuView works together with an instance of NSMenu to display the menu. The NSMenu object stores the menu instance data while the NSMenuView provides the code to draw the menu items with the appropriate interface.
NSMenuView draws the basic menu background and uses menu-item cells to draw the individual menu items. Although it is responsible for drawing the menu item, a menu-item cell still uses information calculated by its menu view to position elements of the item correctly. NSMenuView calculates the positioning information by querying each of its menu-item cells for the size of each of its elements. The menu view uses this information to determine element sizes that can accommodate all of the items in the menu.
NSMenuView should not need to be subclassed for most purposes. However, if you define a custom look for your application's menus, you may need to subclass NSMenuView to implement that appearance. For example, you could override NSMenuView to implement a grid menu or to implement a popup button whose menu is displayed horizontally. If you simply want to change the appearance of the menu's items, you should subclass NSMenuItemCell instead.
When subclassing NSMenuView, you should override the sizeToFit method to calculate the geometry of your menu. Other methods invoke sizeToFit as necessary to account for changes in the menu content, such as when items are added or removed.
NSMenuView provides access to the menu geometry through the methods stateImageOffset, stateImageWidth, imageAndTitleOffset, imageAndTitleWidth, keyEquivalentOffset, and keyEquivalentWidth. If you are using instances of NSMenuItemCell for your menu-item cells, you should override these methods to return the information required by the cell. Otherwise, you can define new methods that your custom cell knows how to use.
- Initializing a menu view
- - initAsTearOff
- - initWithFrame:
- Getting and setting menu view attributes
- - setMenu:
- - menu
- - setHorizontal:
- - isHorizontal
- - setFont:
- - font
- - setHighlightedItemIndex:
- - highlightedItemIndex
- - setMenuItemCell:forItemAtIndex:
- - menuItemCellForItemAtIndex:
- - attachedMenuView
- - attachedMenu
- - isAttached
- - isTornOff
- - horizontalEdgePadding
- - setHorizontalEdgePadding:
- Notification methods
- - itemChanged:
- - itemAdded:
- - itemRemoved:
- Working with submenus
- - detachSubmenu
- - attachSubmenuForItemAtIndex:
- Calculating menu geometry
- - update
- - setNeedsSizing:
- - needsSizing
- - sizeToFit
- - stateImageOffset
- - stateImageWidth
- - imageAndTitleOffset
- - imageAndTitleWidth
- - keyEquivalentOffset
- - keyEquivalentWidth
- - innerRect
- - rectOfItemAtIndex:
- - indexOfItemAtPoint:
- - setNeedsDisplayForItemAtIndex:
- - locationForSubmenu:
- - setWindowFrameForAttachingToRect:onScreen:preferredEdge:popUpSelectedItem:
- Event handling
- - performActionWithHighlightingForItemAtIndex:
- - trackWithEvent:
+ (float)menuBarHeight
- (void)attachSubmenuForItemAtIndex:(int)index
See Also: - setWindowFrameForAttachingToRect:onScreen:preferredEdge:popUpSelectedItem:, - orderFront: (NSWindow)
- (NSMenu *)attachedMenu
See Also: - attachedMenuView, - isAttached
- (NSMenuView *)attachedMenuView
See Also: - attachedMenu, - detachSubmenu, - isAttached
- (void)detachSubmenu
See Also: - attachSubmenuForItemAtIndex:, - setHighlightedItemIndex:, - orderOut: (NSWindow)
- (NSFont *)font
See Also: - setFont:
- (int)highlightedItemIndex
See Also: - setHighlightedItemIndex:
- (float)horizontalEdgePadding
See Also: - setHorizontalEdgePadding:
- (float)imageAndTitleOffset
If any changes have been made to the menu's contents, this method invokes sizeToFit to update the menu-view information.
See Also: - imageAndTitleWidth, - stateImageOffset, - keyEquivalentOffset
- (float)imageAndTitleWidth
If any changes have been made to the menu's contents, this method invokes sizeToFit to update the menu-view information.
See Also: - imageAndTitleOffset, - stateImageWidth, - keyEquivalentWidth
- (int)indexOfItemAtPoint:(NSPoint)point
See Also: - rectOfItemAtIndex:
- (NSRect)innerRect
See Also: - bounds (NSView)
- (id)initAsTearOff
- (id)initWithFrame:(NSRect)frame
- (BOOL)isAttached
YES
if
this menu is currently attached to its parent menu.See Also: - attachedMenu, - attachedMenuView
- (BOOL)isHorizontal
YES
if
the menu is displayed horizontally, such as for a menu bar, otherwise
returns NO
.See Also: - setHorizontal:
- (BOOL)isTornOff
YES
if
this menu view's window is disassociated from its parent menu.- (void)itemAdded:(NSNotification
*)notification
To account for the newly added menu item, this method creates a new menu-item cell for the item and marks the menu view as needing to be resized.
See Also: - setNeedsSizing:
- (void)itemChanged:(NSNotification
*)notification
To account for changes to a menu item, this method marks the menu view as needing to be resized.
See Also: - setNeedsSizing:
- (void)itemRemoved:(NSNotification
*)notification
To account for changes to a menu item, this method removes the item's menu-item cell and marks the menu view as needing to be resized
See Also: - setNeedsSizing:
- (float)keyEquivalentOffset
If any changes have been made to the menu's contents, this method invokes sizeToFit to update the menu-view information.
See Also: - keyEquivalentWidth, - stateImageOffset, - imageAndTitleOffset
- (float)keyEquivalentWidth
If any changes have been made to the menu's contents, this method invokes sizeToFit to update the menu-view information.
See Also: - keyEquivalentOffset, - stateImageWidth, - imageAndTitleWidth
- (NSPoint)locationForSubmenu:(NSMenu
*)aSubmenu
If any changes have been made to the menu's contents, this method invokes sizeToFit to update the menu-view information.
See Also: - setWindowFrameForAttachingToRect:onScreen:preferredEdge:popUpSelectedItem:, - sizeToFit
- (NSMenu *)menu
See Also: - setMenu:
- (NSMenuItemCell *)menuItemCellForItemAtIndex:(int)index
See Also: - setMenuItemCell:forItemAtIndex:, - sizeToFit
- (BOOL)needsSizing
YES
if
the menu view needs to be resized due to changes in the NSMenu. See Also: - setNeedsSizing:
- (void)performActionWithHighlightingForItemAtIndex:(int)index
See Also: - performActionForItemAtIndex: (NSMenu)
- (NSRect)rectOfItemAtIndex:(int)index
If any changes have been made to the menu's contents, this method invokes sizeToFit to update the menu-view information.
See Also: - innerRect, - needsSizing, - sizeToFit
- (void)setFont:(NSFont
*)font
See Also: - font
- (void)setHighlightedItemIndex:(int)index
The rectangle of the menu item is marked as invalid and is redrawn the next time the event loop comes around. If another menu item was previously highlighted, that menu item is redrawn without highlights when the event loop comes around again.
See Also: - setNeedsDisplayForItemAtIndex:, - highlightedItemIndex
- (void)setHorizontal:(BOOL)flag
YES
,
the menu's items are displayed horizontally, otherwise the menu's
items are displayed vertically.See Also: - isHorizontal
- (void)setHorizontalEdgePadding:(float)pad
See Also: - horizontalEdgePadding
- (void)setMenu:(NSMenu
*)menu
This method adds the menu view to the new NSMenu object's list of observers. The notifications this method establishes notify this menu view when menu items in the NSMenu object are added, removed, or changed. This method removes the menu view from its previous NSMenu object's list of observers.
See Also: - setNeedsSizing:, - itemAdded:, - itemRemoved:, - itemChanged:
- (void)setMenuItemCell:(NSMenuItemCell
*)cell
forItemAtIndex:(int)index
See Also: - menuItemCellForItemAtIndex:, - setNeedsSizing:
- (void)setNeedsDisplayForItemAtIndex:(int)index
See Also: - rectOfItemAtIndex:, - setNeedsDisplayInRect: (NSView)
- (void)setNeedsSizing:(BOOL)flag
YES
when
the menu contents have changed or the menu appearance has changed. This method
is used internally; you should not need to invoke it directly unless
you are implementing a subclass that can cause the layout to become
invalid.See Also: - sizeToFit
- (void)setWindowFrameForAttachingToRect:(NSRect)screenRect
onScreen:(NSScreen *)screen
preferredEdge:(NSRectEdge)edge
popUpSelectedItem:(int)selectedItemIndex
The selectedItemIndex parameter specifies the amount by which the selected item's rectangle overlaps screenRect.
If the preferred edge cannot be honored, because there is not enough room, the opposite edge is used. If the rectangle does not completely fit either edge, this method uses the edge where there is more room.
If any changes have been made to the menu's contents, this method invokes sizeToFit to update the menu-view information.
See Also: - sizeToFit
- (void)sizeToFit
The geometry of each menu item is determined by asking its corresponding menu-item cell. The menu-item cell is obtained from the menuItemCellForItemAtIndex: method.
See Also: - setNeedsSizing:, - menuItemCellForItemAtIndex:
- (float)stateImageOffset
If any changes have been made to the menu's contents, this method invokes sizeToFit to update the menu-view information.
See Also: - horizontalEdgePadding, - setHorizontalEdgePadding:, - sizeToFit
- (float)stateImageWidth
If any changes have been made to the menu's contents, this method invokes sizeToFit to update the menu-view information.
See Also: - sizeToFit
- (BOOL)trackWithEvent:(NSEvent
*)event
You should not need to use this method directly.
- (void)update
See Also: - sizeToFit, - setNeedsSizing:, - update (NSMenu)