Up

# NSMenuView

### Authors

Fred Kiefer (`FredKiefer@gmx.de`)
David Lazaro Saz (`khelekir@encomix.es`)
Michael Hanni (`mhanni@sprintmail.com`)

Copyright: (C) 1999 Free Software Foundation, Inc.

# Software documentation for the NSMenuView class

## NSMenuView : NSView

> Declared in:
>   AppKit/NSMenuView.h

> Conforms to:
>   NSCoding
>   NSMenuView

Availability: OpenStep
The NSMenu class uses an object implementing the NSMenuView protocol to do the actual drawing.
Normally there is no good reason to write your own class implementing this protocol. However if you want to customize your menus you should implement this protocol to ensure that it works nicely together with sub/super menus not using your custom menu representation.
How menus are drawn
This class implements several menu look and feels at the same time. The looks and feels implemented are:

- Ordinary vertically stacked menus with the NeXT submenu positioning behavour.
- Vertically stacked menus with the WindowMaker submenu placement. This behaviour is selected by choosing the GSWindowMakerInterfaceStyle.
- PopupButtons are actually menus. This class implements also the behaviour for the NSPopButtons. See for the the class NSPopButton.

Instance Variables
Method summary

- +menuBarHeight
- -attachSubmenuForItemAtIndex:
- -attachedMenu
- -attachedMenuView
- -detachSubmenu
- -font
- -highlightedItemIndex
- -horizontalEdgePadding
- -imageAndTitleOffset
- -imageAndTitleWidth
- -indexOfItemAtPoint:
- -initAsTearOff
- -innerRect
- -isAttached
- -isHorizontal
- -isTornOff
- -itemAdded:
- -itemChanged:
- -itemRemoved:
- -keyEquivalentOffset
- -keyEquivalentWidth
- -locationForSubmenu:
- -menu
- -menuItemCellForItemAtIndex:
- -needsSizing
- -performActionWithHighlightingForItemAtIndex:
- -rectOfItemAtIndex:
- -setFont:
- -setHighlightedItemIndex:
- -setHorizontal:
- -setHorizontalEdgePadding:
- -setMenu:
- -setMenuItemCell:forItemAtIndex:
- -setNeedsDisplayForItemAtIndex:
- -setNeedsSizing:
- -setWindowFrameForAttachingToRect:onScreen:preferredEdge:popUpSelectedItem:
- -sizeToFit
- -stateImageOffset
- -stateImageWidth
- -trackWithEvent:
- -update

### menuBarHeight

+ (float) menuBarHeight;
Availability: OpenStep
Returns the height of the menu bar.

### attachSubmenuForItemAtIndex:

- (void) attachSubmenuForItemAtIndex: (int)index;
Availability: OpenStep
Attach submenu if the item at index is a submenu. It will figure out if the new submenu should be transient or not.

### attachedMenu

- (NSMenu*) attachedMenu;
Availability: OpenStep
Returns visible attached submenu.

### attachedMenuView

- (NSMenuView*) attachedMenuView;
Availability: OpenStep
Returns menu view associated with visible attached submenu.

### detachSubmenu

- (void) detachSubmenu;
Availability: OpenStep
Detaches currently visible submenu window from main menu.

### font

- (NSFont*) font;
Availability: OpenStep
Returns the default font used to draw the menu text.

### highlightedItemIndex

- (int) highlightedItemIndex;
Availability: OpenStep
Returns the index of the highlighted item. Returns -1 if there is no highlighted item.

### horizontalEdgePadding

- (float) horizontalEdgePadding;
Availability: OpenStep
Returns horizontal space used for padding between menu item elements (state image, title image, title, key equivalent, submenu arrow image).

### imageAndTitleOffset

- (float) imageAndTitleOffset;
Availability: OpenStep
Returns the starting horizontal position for drawing the image and title.

### imageAndTitleWidth

- (float) imageAndTitleWidth;
Availability: OpenStep
Returns the width of the image and title section. Tis section contains image and text of menu item.

### indexOfItemAtPoint:

- (int) indexOfItemAtPoint: (NSPoint)point;
Availability: OpenStep
Returns the index of the item below point. Returns -1 if mouse is not above a menu item.

### initAsTearOff

- (id) initAsTearOff;
Availability: OpenStep
Creates new instance and sets menu to torn off state with NSMenu's setTornOff:.

### innerRect

- (NSRect) innerRect;
Availability: OpenStep
Returns bounds rectangle of the menu view. It is smaller by 1 pixel in width than menu window (dark gray border at left).

### isAttached

- (BOOL) isAttached;
Availability: OpenStep
Returns `YES`, if this object is an visivle attached submenu's view. Returns `NO` otherwise.

### isHorizontal

- (BOOL) isHorizontal;
Availability: OpenStep
Returns `YES` if menu items are displayed horizontally, `NO` if vertically.

### isTornOff

- (BOOL) isTornOff;
Availability: OpenStep
Returns `YES`, if this object is associated with torn off menu (menu with a close button on title bar).

### itemAdded:

- (void) itemAdded: (NSNotification*)notification;
Availability: OpenStep
Creates new item cell for the newly created menu item, marks cell and menu view as needing to be resized. This method is invoked when NSMenuDidAddItemNotification received. The notification parameter contains index of changed menu item and can be accessed with NSMenuItemIndex key.

### itemChanged:

- (void) itemChanged: (NSNotification*)notification;
Availability: OpenStep
Marks menu item cell associated with the menu item and menu view as needing to be resized. This method is invoked when NSMenuDidChangeItemNotification received. The notification parameter contains index of changed menu item and can be accessed with NSMenuItemIndex key.

### itemRemoved:

- (void) itemRemoved: (NSNotification*)notification;
Availability: OpenStep
Removes cell associated with removed menu item, removes highlighting if removed menu item was highlighted, marks cell and menu view as needing to be resized. This method is invoked when NSMenuDidRemoveItemNotification received. The notification parameter contains index of changed menu item and can be accessed with NSMenuItemIndex key.

### keyEquivalentOffset

- (float) keyEquivalentOffset;
Availability: OpenStep
Returns the starting position for drawing the key equivalent. Key equivalent can be submenu arrow if menu item has submenu.

### keyEquivalentWidth

- (float) keyEquivalentWidth;
Availability: OpenStep
Returns the width of key equivalent text. Key equivalent can be submenu arrow if menu item has submenu

### locationForSubmenu:

- (NSPoint) locationForSubmenu: (NSMenu*)aSubmenu;
Availability: OpenStep
Returns the correct frame origin for aSubmenu based on the location of the receiver. This location may depend on the current NSInterfaceStyle.

### menu

- (NSMenu*) menu;
Availability: OpenStep
Returns the NSMenu associated with this menu view.

### menuItemCellForItemAtIndex:

- (NSMenuItemCell*) menuItemCellForItemAtIndex: (int)index;
Availability: OpenStep
Returns cell associated with item at index.

### needsSizing

- (BOOL) needsSizing;
Availability: OpenStep
Return `YES` if menu view contents changed and needs to be resized.

### performActionWithHighlightingForItemAtIndex:

- (void) performActionWithHighlightingForItemAtIndex: (int)index;
Availability: OpenStep
Description forthcoming.

### rectOfItemAtIndex:

- (NSRect) rectOfItemAtIndex: (int)index;
Availability: OpenStep
Returns frame rectangle of menu item cell. It is smaller by 1 pixel in width than menu window (dark gray border).

### setFont:

- (void) setFont: (NSFont*)font;
Availability: OpenStep
Sets the default font to use when drawing the menu text.

### setHighlightedItemIndex:

- (void) setHighlightedItemIndex: (int)index;
Availability: OpenStep
Highlights item with at index. If index is -1 all highlighing is removed.

### setHorizontal:

- (void) setHorizontal: (BOOL)flag;
Availability: OpenStep
Sets menu orientation. If `YES` menu items are displayed from left to right, if `NO` from top to bottom (vertically). By default, menu items are displayed vertically.

### setHorizontalEdgePadding:

- (void) setHorizontalEdgePadding: (float)pad;
Availability: OpenStep
Sets amount of pixels added between menu item elements to pad.

### setMenu:

- (void) setMenu: (NSMenu*)menu;
Availability: OpenStep
Sets the menu to be displayed in to menu. Also this method adds this menu view to the menu's list of observers, mark view to force recalculation of layout with setNeedsSizing:YES, and updates itself with update method.

### setMenuItemCell: forItemAtIndex:

- (void) setMenuItemCell: (NSMenuItemCell*)cell forItemAtIndex: (int)index;
Availability: OpenStep
Replaces item cell at index with cell. Highlighting of item is preserved.

### setNeedsDisplayForItemAtIndex:

- (void) setNeedsDisplayForItemAtIndex: (int)index;
Availability: OpenStep
Calls setNeedsDisplayInRect: for rectangle occupied by item at index.

### setNeedsSizing:

- (void) setNeedsSizing: (BOOL)flag;
Availability: OpenStep
Sets the flag whether layout needs to be resized. Set it to `YES` if menu contents changed and layout needs to be recalculated. This method is used internally. Yout should not invoke it directly from applications.

### setWindowFrameForAttachingToRect: onScreen: preferredEdge: popUpSelectedItem:

- (void) setWindowFrameForAttachingToRect: (NSRect)screenRect onScreen: (NSScreen*)screen preferredEdge: (NSRectEdge)edge popUpSelectedItem: (int)selectedItemIndex;
Availability: OpenStep
Resize menu view frame to be appropriate in size to attach to screenRect at preferredEdge. For popup's menu, if selectedItemIndex is other than -1, position view so selected item covers the NSPopUpButton.
NOTE: preffered edge positioning doesn't implemented yet!

### sizeToFit

- (void) sizeToFit;
Availability: OpenStep
Description forthcoming.

### stateImageOffset

- (float) stateImageOffset;
Availability: OpenStep
Returns the starting horizontal position for drawing the state image.

### stateImageWidth

- (float) stateImageWidth;
Availability: OpenStep
Returns the width of the state image.

### trackWithEvent:

- (BOOL) trackWithEvent: (NSEvent*)event;
Availability: OpenStep
This method is responsible for tracking the mouse while this menu is on the screen and the user is busy navigating the menu or one of it submenus. Responsible does not mean that this method does it all. For submenus for example it will call, indirectly, itself for submenu under consideration. It will return `YES` if user released mouse, not above a submenu item. `NO` in all other circumstances. Implementation detail:

- It use periodic events to update the highlight state and attach / detach submenus.
- The flag justAttachedNewSubmenu is set to `YES` when a new submenu is attached. The effect is that the highlighting / attaching / detaching is supressed for this menu. This is done so the user is given a change to move the mouse pointer into the newly attached submenu. Otherwise it would immediately be removed as the mouse pointer move over another item. The logic for resetting the flag is rather adhoc.
- the flag subMenusNeedRemoving means that we will remove all the submenus after we are done. This flag is used to clean up the submenus when the user has opened a submenu by clicking and wants to close it again by clicking on the hihglighted item.
- When the user released the mouse this method will cleanup all the transient menus. Not only its own, but also its attached menu and all its transient super menus.
- The clean up is done BEFORE the action is executed. This is needed otherwise `hiding' the application leaves a dangling menu. If this is not acceptable, there should be another mechanism of handling the hiding. BTW besides the `hiding' the application, model panels are also a problem when the menu is not cleared before executing the action.

### update

- (void) update;
Availability: OpenStep
Adds title view for application menu and submenus, removes title view if menu is owned by NSPopUpButton, adds close button to title view for torn off menus and removes it for other type of menu.

## Instance Variables for NSMenuView Class

### _cellSize

@protected NSSize _cellSize;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _font

@protected NSFont* _font;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _highlightedItemIndex

@protected int _highlightedItemIndex;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _horizontal

@protected BOOL _horizontal;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _horizontalEdgePad

@protected float _horizontalEdgePad;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _imageAndTitleOffset

@protected float _imageAndTitleOffset;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _imageAndTitleWidth

@protected float _imageAndTitleWidth;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _itemCells

@protected NSMutableArray* _itemCells;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _keyEqOffset

@protected float _keyEqOffset;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _keyEqWidth

@protected float _keyEqWidth;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _needsSizing

@protected BOOL _needsSizing;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _pad1

@protected char _pad1;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _pad2

@protected char _pad2;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _stateImageOffset

@protected float _stateImageOffset;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

### _stateImageWidth

@protected float _stateImageWidth;
Availability: OpenStep
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.

Up