- Inherits from:
- NSButtonCell : NSActionCell : NSCell : NSObject
- Conforms to:
- NSCoding
- (NSCell)
- NSCopying (NSCell)
- NSObject (NSObject)
Declared in:
- AppKit/NSMenuItemCell.h
NSMenuItemCell is a class that handles the measurement and display of a single menu item in its encompassing frame. Instances of NSMenuItemCell work in conjunction with an NSMenuView object to control the overall appearance of the menu.
An instance of NSMenuItemCell uses the information contained in an NSMenuItem object to obtain the basic geometry of the menu item. Using this geometry, the menu-item cell calculates the minimal space required for drawing each of its 3 components: the state image, the title text or image, and the key equivalent. Once this information has been calculated, the cell's menu view gathers the information and uses it to determine the optimal size for the menu. The cell then bases its drawing calculations on this optimal size information.
Menu-item cells have two sets of methods for returning two different sets of measurements. The first set of methods return the minimal space required by the menu item's individual components. These methods include imageWidth, titleWidth, keyEquivalentWidth, and stateImageWidth.
The second set of methods return the drawing rectangle to be used by the cell when drawing a given component. The methods imageRectForBounds:, keyEquivalentRectForBounds:, stateImageRectForBounds:, and titleRectForBounds: return the rectangle for each component, taking into account the NSMenuView adjustments. These adjusted rectangles allow the menu-item components to line up within the menu, forming coherent columns.
You can subclass NSMenuItemCell to change the way menu items are drawn by your application. For example, your subclass can display extra information for each menu item or change the basic appearance (background color, borders, adornments, and so on) of the menu item. If you create a custom subclass of NSMenuItemCell, you may also need to subclass NSMenuView to handle any additional information provided by your menu-item cell. If you simply want to change the way the cell is drawn, you can do that by overriding the menu-item cell's drawWithFrame:inView: method.
- Getting and setting menu item attributes
- - isHighlighted
- - setHighlighted:
- - menuItem
- - setMenuItem:
- Calculating menu item sizes
- - calcSize
- - needsSizing
- - setNeedsSizing:
- - imageWidth
- - titleWidth
- - keyEquivalentWidth
- - stateImageWidth
- Getting the menu item's drawing rectangle
- - imageRectForBounds:
- - keyEquivalentRectForBounds:
- - stateImageRectForBounds:
- - titleRectForBounds:
- Drawing the menu item
- - drawBorderAndBackgroundWithFrame:inView:
- - drawImageWithFrame:inView:
- - drawKeyEquivalentWithFrame:inView:
- - drawSeparatorItemWithFrame:inView:
- - drawStateImageWithFrame:inView:
- - drawTitleWithFrame:inView:
- (void)calcSize
Typically this method is invoked automatically when necessary. You should not need to invoke it directly.
See Also: - needsSizing
- (void)drawBorderAndBackgroundWithFrame:(NSRect)cellFrame
inView:(NSView *)controlView
See Also: drawWithFrame:inView: (NSCell)
- (void)drawImageWithFrame:(NSRect)cellFrame
inView:(NSView *)controlView
Typically this method is invoked by the cell's drawWithFrame: method. You should not need to invoke it directly. Subclasses may override this method to control the drawing of the image.
- (void)drawKeyEquivalentWithFrame:(NSRect)cellFrame
inView:(NSView *)controlView
Typically this method is invoked by the cell's drawWithFrame: method. You should not need to invoke it directly. Subclasses may override this method to control the drawing of the key equivalent.
- (void)drawSeparatorItemWithFrame:(NSRect)cellFrame
inView:(NSView *)controlView
You should not need to invoke this method directly. Subclasses may override this method to control the drawing of the separator.
See Also: - drawKeyEquivalentWithFrame:inView:, - drawTitleWithFrame:inView:, - isFlipped (NSView)
- (void)drawStateImageWithFrame:(NSRect)cellFrame
inView:(NSView *)controlView
Typically this method is invoked by the cell's drawWithFrame: method. You should not need to invoke it directly. Subclasses may override this method to control the drawing of the state image.
- (void)drawTitleWithFrame:(NSRect)cellFrame
inView:(NSView *)controlView
Typically this method is invoked by the cell's drawWithFrame: method. You should not need to invoke it directly. Subclasses may override this method to control the drawing of the title.
- (NSRect)imageRectForBounds:(NSRect)cellFrame
See Also: - stateImageRectForBounds:, - imageRectForBounds:, - titleRectForBounds:, - keyEquivalentRectForBounds:
- (float)imageWidth
See Also: - stateImageWidth, - calcSize, - needsSizing
- (BOOL)isHighlighted
YES
if
the cell currently draws its menu item with a highlighted appearance.See Also: - setHighlighted:
- (NSRect)keyEquivalentRectForBounds:(NSRect)cellFrame
See Also: - keyEquivalent (NSMenuItem), - stateImageRectForBounds:, - imageRectForBounds:, - titleRectForBounds:, - keyEquivalentRectForBounds:
- (float)keyEquivalentWidth
See Also: - calcSize, - needsSizing
- (NSMenuItem *)menuItem
See Also: - setMenuItem:
- (NSMenuView *)menuView
- (BOOL)needsDisplay
- (BOOL)needsSizing
YES
if
the size of the menu item needs to be calculated, otherwise returns NO
.See Also: - setNeedsSizing:, - calcSize
- (void)setHighlighted:(BOOL)flag
You should not need to call this method directly. It is invoked by the NSMenuView's setHighlightedItemIndex: method to provide user feedback during menu tracking or when the user selects a menu item (either by clicking or using a key equivalent).
See Also: - isHighlighted, - setHighlightedItemIndex: (NSMenuView)
- (void)setMenuItem:(NSMenuItem
*)item
See Also: - menuItem
- (void)setMenuView:(NSMenuView
*)menuView
- (void)setNeedsDisplay:(BOOL)flag
- (void)setNeedsSizing:(BOOL)flag
YES
,
the next attempt to obtain any size-related information from this
menu-item cell invokes the calcSize method to
recalculate the information. If flag is NO
,
the next attempt to obtain size-related information returns the
currently cached values.Subclasses that drastically change the way a menu item is drawn may need to invoke this method to recalculate the menu-item information. Other parts of your application should not need to invoke this method directly. The cell invokes this method as necessary when the contents of its menu item changes.
See Also: - needsSizing
- (NSRect)stateImageRectForBounds:(NSRect)cellFrame
See Also: - stateImageRectForBounds:, - imageRectForBounds:, - titleRectForBounds:, - keyEquivalentRectForBounds:
- (float)stateImageWidth
To change the state of the cell's menu item, use NSMenuItem's setState: method.
See Also: - calcSize, - needsSizing, - setState: (NSMenuItem)
- (NSRect)titleRectForBounds:(NSRect)cellFrame
See Also: - stateImageRectForBounds:, - imageRectForBounds:, - titleRectForBounds:, - keyEquivalentRectForBounds:
- (float)titleWidth
See Also: - calcSize, - needsSizing