How to validate the Undo menu item?
How to validate the Undo menu item?
- Subject: How to validate the Undo menu item?
- From: Bill Cheeseman <email@hidden>
- Date: Tue, 23 Apr 2002 15:04:08 -0400
How do I validate the Undo menu item so that it will be disabled in certain
circumstances? The usual validateMenuItem: technique doesn't seem to work
very well with the Undo menu item.
This is for a document-based application. It implements undo in the standard
way: Several action methods in the window controller call corresponding
accessor methods in the model object. The accessor methods register with the
undo manager and set data values, then post notifications telling the window
controller to update a table view, then return to the action method where
special undo menu item names are set. Each action method sets a different
undo menu item name.
After executing one of these action methods, suppose I want to undo it. I
click in the table view to make the table view first responder, then I go to
the Edit menu and choose the Undo menu item, which now has one of the
special undo menu item names. If the table view is in a certain state at
this time, and the undo menu item name contains certain text, I want the
Undo menu item to be disabled; but if the table view is in any other state
or the undo menu item name contains any other text, I want the Undo menu
item to be enabled.
In my subclass of NSTableView, I implement validateMenuItem:. I have logged
the various undo menu item names and table view states that it encounters,
and it appears to work as intended, as far as it goes.
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
SEL action = [menuItem action];
if (action == @selector(undo:)) {
NSString *undoActionName = [[[[self delegate] document] undoManager]
undoActionName];
if ([self isEditing] && [undoActionName
hasPrefix:NSLocalizedString(@"Add Antique ID ", @"Name of undo/redo menu
item after antique record was added")]) {
return NO;
}
}
return YES;
}
However, in order to force the application to call validateMenuItem:, I have
to implement the undo: action method in my table view subclass, too. This is
necessary so that Cocoa's menu auto-enabling routines will find it as they
walk the responder chain when I click on the menu bar, and then they will
know to call validateMenuItem:. But I don't know what to do in this undo:
method. I suppose I would normally call performSelector:withObject: to force
it to execute the method that I had previously registered with the undo
manager as the next undo action. But this would require me to duplicate the
logic of the undo manager to decide which method to perform in those cases
where the Undo menu item is not supposed to be disabled, or it would require
me to dig into the private undo stack to extract the method to perform. The
former is a lot of work (if it can be done at all), and the latter is
probably required anyway because the value I need to pass in the withObject:
parameter is hidden in the undo stack.
I don't see any way my undo: method can tell Cocoa to continue walking the
responder chain to find the "real" undo method. Besides, undo doesn't seem
to work that way; it looks like it calls performSelector: on Cocoa's private
undo stack to call the registered undo action on the top of the stack. But
if that's the case, where does Interface Builder find the undo: action that
comes pre-connected to First Responder in the document-based application
template, anyway, and what does it do? This is all very confusing.
Have I overlooked an easy way to do this? Do I have to somehow manually
disable the Undo menu item in the specified circumstances?
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
The AppleScript Sourcebook -
http://www.AppleScriptSourcebook.com
Vermont Recipes -
http://www.stepwise.com/Articles/VermontRecipes
Croquet Club of Vermont -
http://members.valley.net/croquetvermont
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.