- Inherits from:
- NSView : NSResponder : NSObject
- Package:
- com.apple.yellow.application
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.
- Constructors
- NSMenuView
- Getting and setting menu view attributes
- setMenu
- menu
- setHorizontal
- isHorizontal
- setFont
- font
- setHighlightedItemIndex
- highlightedItemIndex
- setMenuItemCellForItemAtIndex
- 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
- Event handling
- performActionWithHighlightingForItemAtIndex
- trackWithEvent
public NSMenuView()
public NSMenuView(boolean flag)
public static float menuBarHeight()
public void attachSubmenuForItemAtIndex(int index)
See Also: setWindowFrameForAttachingToRect, orderFront (NSWindow)
public NSMenu attachedMenu()
See Also: attachedMenuView, isAttached
public NSMenuView attachedMenuView()
See Also: attachedMenu, detachSubmenu, isAttached
public void detachSubmenu()
See Also: attachSubmenuForItemAtIndex, setHighlightedItemIndex, orderOut (NSWindow)
public NSFont font()
See Also: setFont
public int highlightedItemIndex()
See Also: setHighlightedItemIndex
public float horizontalEdgePadding()
See Also: setHorizontalEdgePadding
public 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
public 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
public int indexOfItemAtPoint(NSPoint point)
See Also: rectOfItemAtIndex
public NSRect innerRect()
See Also: bounds (NSView)
public boolean isAttached()
true
if
this menu is currently attached to its parent menu.See Also: attachedMenu, attachedMenuView
public boolean isHorizontal()
true
if
the menu is displayed horizontally, such as for a menu bar, otherwise
returns false
.See Also: setHorizontal
public boolean isTornOff()
true
if
this menu view's window is disassociated from its parent menu.public 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
public 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
public 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
public 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
public 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
public 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, sizeToFit
public NSMenu menu()
See Also: setMenu
public NSMenuItemCell menuItemCellForItemAtIndex(int index)
See Also: setMenuItemCellForItemAtIndex, sizeToFit
public boolean needsSizing()
true
if
the menu view needs to be resized due to changes in the NSMenu. See Also: setNeedsSizing
public void performActionWithHighlightingForItemAtIndex(int index)
See Also: performActionForItemAtIndex (NSMenu)
public 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
public void setFont(NSFont font)
See Also: font
public 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
public void setHorizontal(boolean flag)
true
,
the menu's items are displayed horizontally, otherwise the menu's
items are displayed vertically.See Also: isHorizontal
public void setHorizontalEdgePadding(float pad)
See Also: horizontalEdgePadding
public 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
public void setMenuItemCellForItemAtIndex(
NSMenuItemCell cell,
int index)
See Also: menuItemCellForItemAtIndex, setNeedsSizing
public void setNeedsDisplayForItemAtIndex(int index)
See Also: rectOfItemAtIndex, setNeedsDisplayInRect: (NSView)
public void setNeedsSizing(boolean flag)
true
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
public void setWindowFrameForAttachingToRect(
NSRect screenRect,
NSScreen screen,
int edge,
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
public 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
public 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
public 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
public boolean trackWithEvent(NSEvent event)
You should not need to use this method directly.
public void update()
See Also: sizeToFit, setNeedsSizing, update (NSMenu)