Re: Correct implementation of undoManagerForTextView
Re: Correct implementation of undoManagerForTextView
- Subject: Re: Correct implementation of undoManagerForTextView
- From: Dustin Voss <email@hidden>
- Date: Wed, 3 Mar 2004 13:08:30 -0800
On 3 Mar, 2004, at 10:32 AM, Jeremy Dronfield wrote:
My app has two separate text views (one in the main window, another in
a drawer). Each has its own separate delegate, and I want each to have
its own undo manager. (So that they can separately trigger an Unsaved
Changes warning.) The trouble is, I can't figure out the correct
implementation of the NSTextView delegate method:
- (NSUndoManager *)undoManagerForTextView:(NSTextView *)aTextView
{
return (what?);
}
There's no illumination in the FM, and I'm further confused by a
reference by Bill Cheeseman (Omni archives) to this as an NSWindow
delegate method, which it clearly isn't.
Firstly, the undo manager is not in charge of unsaved changes warnings.
Your model should keep track of this itself. NSDocument keeps track by
observing the NSUndoManager it maintains, and other parts of the
framework check with NSDocument to see if there are unsaved changes.
Secondly, there is an NSWindow delegate method that supplies an undo
manager. If a text view needs an undo manager, it follows the responder
chain, and will get one of these (in order):
* One returned by the NSTextView delegate's -undoManagerForTextView:
* One returned by the NSWindow delegate's -windowWillReturnUndoManager:
* One automatically created by NSWindow.
Thirdly, depending on how your app is laid out, are you sure you want
two undo managers? Remember that the undo manager controls the undo
stack. Let's say the user makes a change A in the first text view,
change B in the second, and change C in the first. With two undo
managers, he will be be able to undo C then A when he is in the first
text view, but if he switches to the second, he will only be able to
undo B.
If you want two undo managers, and these text views are not part of
your model, then just let them use NSWindow's default undo manager. The
main window will get one and the drawer will get one. You can track the
dirty status yourself, either by noting changes to the views, or by
observing the NSUndoManagers as NSDocument does.
If you want one undo manager, but the text views are not part of your
model, then use the main window's default undo manager, and implement
-windowWillReturnUndoManager: in your drawer's delegate to return the
main window's undo manager. If that doesn't work (NSDrawer might not
check with the delegate), then implement -undoManagerForTextView: to
return the main window's undo manager.
If the text views are part of your model, then they should use your
model's undo manager. The next question is whether you want another,
non-model undo manager for other controls in the window --
specifically, for undoable text fields. You probably do not, as they
probably relate to your model as well. In this case, implement
-windowWillReturnUndoManager:, not -undoManagerForTextView:, and return
the model's undo manager. The text view will end up with that one.
If you do want a non-model undo manager for the other controls,
implement -undoManagerForTextView: to return your model's undo manager,
and let the other controls use NSWindow's default undo manager.
I think I've covered all the bases. :)
_______________________________________________
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.