MOExtendedMenuItem


Abstract

MOExtendedMenuItem is a subclass of NSMenuItem that adds support for a "default" state.

Discussion

MOExtendedMenuItem is a subclass of NSMenuItem that adds support for a "default" state. This helps to solve the problem of what happens to menus that change title or other properties via validation methods when there's sometimes no valid target for the items.

For example, you might have a "Save" command that sends a -saveDocument: action to the first responder. Wouldn't it be cool if the menu item actually said "Save "? You can easily do this by adding a -validateMenuItem: method to your document class that sets the menu item's title. The problem is that when there is no active document, the menu item will be disabled, but it will still say "Save " with the document name of the last active document!

MOExtendedMenuItem and MOExtendedMenu cooperate to prevent this. MOExtendedMenuItem has a -defaultTitle (as well as other default state). MOExtendedMenu will revert each item in it to the default state at the beginning of its -update method. If a -validateMenuItem: changes it again, cool, but if not, the default state sticks.

MOExtendedMenuItem will automatically set its default state during awakeFromNib. So all you have to do is make sure that your menu items have their generic default state in your main nib.

You can use MOExtendedMenuItem in a couple ways. You could individually mark your menus and items in a nib file as being the MOExtendedMenuItem and MOExtendedMenu subclasses. Or you can simply call +[MOExtendedMenuItem enableRevertToDefaultItemState] in your application's main() before calling NSApplicationMain and it will arrange to have everything work automatically.



Methods

isRevertToDefaultItemStateEnabled
Returns whether automatic use of MOExtendedMenuItem is enabled.
enableRevertToDefaultItemState
Enables automatic use of MOExtendedMenuItem.
defaultTitle
Returns the default title.
setDefaultTitle:
Sets the default title.
defaultKeyEquivalent
Returns the default key equivalent.
setDefaultKeyEquivalent:
Sets the default key equivalent.
defaultKeyEquivalentModifierMask
Returns the default key equivalent modifier mask.
setDefaultKeyEquivalentModifierMask:
Sets the default key equivalent modifier mask.
defaultImage
Returns the default image.
setDefaultImage:
Sets the default image.
defaultState
Returns the default state.
setDefaultState:
Sets the default state.
resetToDefaults
Restores the item to its default state.

defaultImage

Returns the default image.
- ( NSImage *) defaultImage;

Returns the default image.

method result
The default image.

defaultKeyEquivalent

Returns the default key equivalent.
- ( NSString *) defaultKeyEquivalent;

Returns the default key equivalent.

method result
The default key equivalent.

defaultKeyEquivalentModifierMask

Returns the default key equivalent modifier mask.
- ( unsigned int ) defaultKeyEquivalentModifierMask;

Returns the default key equivalent modifier mask.

method result
The default key equivalent modifier mask.

defaultState

Returns the default state.
- ( int ) defaultState;

Returns the default state.

method result
The default state.

defaultTitle

Returns the default title.
- ( NSString *) defaultTitle;

Returns the default title.

method result
The default title.

enableRevertToDefaultItemState

Enables automatic use of MOExtendedMenuItem.
+ ( void ) enableRevertToDefaultItemState;

Enables automatic use of MOExtendedMenuItem. When it is enabled, all menus and menu items unarchived from nib files will use the MOExtendedMenuItem and MOExtendedMenu subclasses.


isRevertToDefaultItemStateEnabled

Returns whether automatic use of MOExtendedMenuItem is enabled.
+ ( BOOL ) isRevertToDefaultItemStateEnabled;

Returns whether automatic use of MOExtendedMenuItem is enabled. When it is enabled, all menus and menu items unarchived from nib files will use the MOExtendedMenuItem and MOExtendedMenu subclasses.

method result
Whether automatic use of MOExtendedMenuItem is enabled.

resetToDefaults

Restores the item to its default state.
- ( void ) resetToDefaults;

Restores the item to its default state. This is called for each item in a MOExtendedMenu at the beginning of each -update.


setDefaultImage:

Sets the default image.
- ( void ) setDefaultImage:
        (NSImage *) newDefaultImage;

Sets the default image.

Parameter Descriptions
newDefaultImage
The default image.

setDefaultKeyEquivalent:

Sets the default key equivalent.
- ( void ) setDefaultKeyEquivalent:
        (NSString *) newDefaultKeyEquivalent;

Sets the default key equivalent.

Parameter Descriptions
newDefaultKeyEquivalent
The default key equivalent.

setDefaultKeyEquivalentModifierMask:

Sets the default key equivalent modifier mask.
- ( void ) setDefaultKeyEquivalentModifierMask:
        (unsigned int ) newDefaultKeyEquivalentModifierMask;

Sets the default key equivalent modifier mask.

Parameter Descriptions
newDefaultKeyEquivalentModifierMask
The default key equivalent modifier mask.

setDefaultState:

Sets the default state.
- ( void ) setDefaultState:
        (int ) newDefaultState;

Sets the default state.

Parameter Descriptions
newDefaultState
The default state.

setDefaultTitle:

Sets the default title.
- ( void ) setDefaultTitle:
        (NSString *) title;

Sets the default title.

Parameter Descriptions
title
The default title.

(Last Updated 3/20/2005)