1998-05-21 13:41:55 +00:00
|
|
|
|
/* Implementatikon for <NSUndoManager> for GNUStep
|
|
|
|
|
Copyright (C) 1998 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
|
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
|
License along with this library; if not, write to the Free
|
1999-09-09 02:56:20 +00:00
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
1998-05-21 13:41:55 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2000-03-24 05:40:19 +00:00
|
|
|
|
#include "config.h"
|
1998-05-21 13:41:55 +00:00
|
|
|
|
#include <Foundation/NSObject.h>
|
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
|
#include <Foundation/NSNotification.h>
|
|
|
|
|
#include <Foundation/NSInvocation.h>
|
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
|
#include <Foundation/NSRunLoop.h>
|
|
|
|
|
#include <Foundation/NSUndoManager.h>
|
|
|
|
|
|
|
|
|
|
/* Public notifications */
|
|
|
|
|
NSString *NSUndoManagerCheckpointNotification =
|
|
|
|
|
@"NSUndoManagerCheckpointNotification";
|
|
|
|
|
NSString *NSUndoManagerDidOpenUndoGroupNotification =
|
|
|
|
|
@"NSUndoManagerDidOpenUndoGroupNotification";
|
|
|
|
|
NSString *NSUndoManagerDidRedoChangeNotification =
|
|
|
|
|
@"NSUndoManagerDidRedoChangeNotification";
|
|
|
|
|
NSString *NSUndoManagerDidUndoChangeNotification =
|
|
|
|
|
@"NSUndoManagerDidUndoChangeNotification";
|
|
|
|
|
NSString *NSUndoManagerWillCloseUndoGroupNotification =
|
|
|
|
|
@"NSUndoManagerWillCloseUndoGroupNotification";
|
|
|
|
|
NSString *NSUndoManagerWillRedoChangeNotification =
|
|
|
|
|
@"NSUndoManagerWillRedoChangeNotification";
|
|
|
|
|
NSString *NSUndoManagerWillUndoChangeNotification =
|
|
|
|
|
@"NSUndoManagerWillUndoChangeNotification";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Private class for grouping undo/redo actions.
|
|
|
|
|
*/
|
|
|
|
|
@interface PrivateUndoGroup : NSObject
|
|
|
|
|
{
|
|
|
|
|
PrivateUndoGroup *parent;
|
|
|
|
|
NSMutableArray *actions;
|
|
|
|
|
}
|
|
|
|
|
- (NSMutableArray*) actions;
|
|
|
|
|
- (void) addInvocation: (NSInvocation*)inv;
|
|
|
|
|
- (id) initWithParent: (PrivateUndoGroup*)parent;
|
|
|
|
|
- (void) orphan;
|
|
|
|
|
- (PrivateUndoGroup*) parent;
|
1998-07-28 09:45:09 +00:00
|
|
|
|
- (void) perform;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
- (BOOL) removeActionsForTarget: (id)target;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation PrivateUndoGroup
|
|
|
|
|
|
|
|
|
|
- (NSMutableArray*) actions
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return actions;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) addInvocation: (NSInvocation*)inv
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
if (actions == nil)
|
|
|
|
|
{
|
|
|
|
|
actions = [[NSMutableArray alloc] initWithCapacity: 2];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[actions addObject: inv];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(actions);
|
|
|
|
|
RELEASE(parent);
|
|
|
|
|
[super dealloc];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithParent: (PrivateUndoGroup*)p
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
self = [super init];
|
|
|
|
|
if (self)
|
|
|
|
|
{
|
|
|
|
|
parent = RETAIN(p);
|
|
|
|
|
actions = nil;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return self;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) orphan
|
|
|
|
|
{
|
1999-09-28 19:35:09 +00:00
|
|
|
|
DESTROY(parent);
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (PrivateUndoGroup*) parent
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return parent;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-07-28 09:45:09 +00:00
|
|
|
|
- (void) perform
|
1998-05-21 13:41:55 +00:00
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
if (actions != nil)
|
|
|
|
|
{
|
|
|
|
|
unsigned i = [actions count];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
|
1999-07-03 19:59:44 +00:00
|
|
|
|
while (i-- > 0)
|
|
|
|
|
{
|
|
|
|
|
[[actions objectAtIndex: i] invoke];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) removeActionsForTarget: (id)target
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
if (actions != nil)
|
|
|
|
|
{
|
|
|
|
|
unsigned i = [actions count];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
|
1999-07-03 19:59:44 +00:00
|
|
|
|
while (i-- > 0)
|
|
|
|
|
{
|
|
|
|
|
NSInvocation *inv = [actions objectAtIndex: i];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
|
1999-07-03 19:59:44 +00:00
|
|
|
|
if ([inv target] == target)
|
|
|
|
|
{
|
|
|
|
|
[actions removeObjectAtIndex: i];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
if ([actions count] > 0)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return NO;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Private catagory for the method used to handle default grouping
|
|
|
|
|
*/
|
|
|
|
|
@interface NSUndoManager (Private)
|
|
|
|
|
- (void) _loop: (id)arg;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation NSUndoManager (Private)
|
|
|
|
|
- (void) _loop: (id)arg
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_groupsByEvent)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_group != nil)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
[self endUndoGrouping];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[self beginUndoGrouping];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The main part for the NSUndoManager implementation.
|
|
|
|
|
*/
|
|
|
|
|
@implementation NSUndoManager
|
|
|
|
|
|
|
|
|
|
- (void) beginUndoGrouping
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
PrivateUndoGroup *parent;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_isUndoing == NO)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
|
postNotificationName: NSUndoManagerCheckpointNotification
|
|
|
|
|
object: self];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
parent = (PrivateUndoGroup*)_group;
|
|
|
|
|
_group = [[PrivateUndoGroup alloc] initWithParent: parent];
|
|
|
|
|
if (_group == nil)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_group = parent;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"beginUndoGrouping failed to greate group"];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RELEASE(parent);
|
1998-05-21 13:41:55 +00:00
|
|
|
|
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
|
postNotificationName: NSUndoManagerDidOpenUndoGroupNotification
|
|
|
|
|
object: self];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) canRedo
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
|
postNotificationName: NSUndoManagerCheckpointNotification
|
|
|
|
|
object: self];
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if ([_redoStack count] > 0)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
return YES;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) canUndo
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if ([_undoStack count] > 0)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
return YES;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_group != nil && [[_group actions] count] > 0)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
return YES;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return NO;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[[NSRunLoop currentRunLoop] cancelPerformSelector: @selector(_loop:)
|
|
|
|
|
target: self
|
|
|
|
|
argument: nil];
|
1999-09-16 07:21:34 +00:00
|
|
|
|
RELEASE(_redoStack);
|
|
|
|
|
RELEASE(_undoStack);
|
|
|
|
|
RELEASE(_actionName);
|
|
|
|
|
RELEASE(_group);
|
|
|
|
|
RELEASE(_modes);
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[super dealloc];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) disableUndoRegistration
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_disableCount++;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) enableUndoRegistration
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_disableCount > 0)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_disableCount--;
|
|
|
|
|
_registeredUndo = NO; /* No operations since registration enabled. */
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"enableUndoRegistration without disable"];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) endUndoGrouping
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
PrivateUndoGroup *g;
|
|
|
|
|
PrivateUndoGroup *p;
|
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_group == nil)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"endUndoGrouping without beginUndoGrouping"];
|
|
|
|
|
}
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
|
postNotificationName: NSUndoManagerCheckpointNotification
|
|
|
|
|
object: self];
|
1999-09-16 07:21:34 +00:00
|
|
|
|
g = (PrivateUndoGroup*)_group;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
p = RETAIN([g parent]);
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_group = p;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[g orphan];
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
|
postNotificationName: NSUndoManagerWillCloseUndoGroupNotification
|
|
|
|
|
object: self];
|
|
|
|
|
if (p == nil)
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_isUndoing)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_levelsOfUndo > 0 && [_redoStack count] == _levelsOfUndo)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[_redoStack removeObjectAtIndex: 0];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[_redoStack addObject: g];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_levelsOfUndo > 0 && [_undoStack count] == _levelsOfUndo)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[_undoStack removeObjectAtIndex: 0];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[_undoStack addObject: g];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
else if ([g actions] != nil)
|
|
|
|
|
{
|
|
|
|
|
NSArray *a = [g actions];
|
|
|
|
|
unsigned i;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
|
1999-07-03 19:59:44 +00:00
|
|
|
|
for (i = 0; i < [a count]; i++)
|
|
|
|
|
{
|
|
|
|
|
[p addInvocation: [a objectAtIndex: i]];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(g);
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) forwardInvocation: (NSInvocation*)anInvocation
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_disableCount == 0)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_nextTarget == nil)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"forwardInvocation without perparation"];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_group == nil)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"forwardInvocation without beginUndoGrouping"];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[anInvocation setTarget: _nextTarget];
|
|
|
|
|
_nextTarget = nil;
|
|
|
|
|
[_group addInvocation: anInvocation];
|
|
|
|
|
if (_isUndoing == NO)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[_redoStack removeAllObjects];
|
1998-07-28 09:45:09 +00:00
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_registeredUndo = YES;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (int) groupingLevel
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
PrivateUndoGroup *g = (PrivateUndoGroup*)_group;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
int level = 0;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
|
1999-07-03 19:59:44 +00:00
|
|
|
|
while (g != nil)
|
|
|
|
|
{
|
|
|
|
|
level++;
|
|
|
|
|
g = [g parent];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return level;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) groupsByEvent
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
return _groupsByEvent;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
self = [super init];
|
|
|
|
|
if (self)
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_actionName = @"";
|
|
|
|
|
_redoStack = [[NSMutableArray alloc] initWithCapacity: 16];
|
|
|
|
|
_undoStack = [[NSMutableArray alloc] initWithCapacity: 16];
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[self setRunLoopModes:
|
|
|
|
|
[NSArray arrayWithObjects: NSDefaultRunLoopMode, nil]];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return self;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isRedoing
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
return _isRedoing;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isUndoing
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
return _isUndoing;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isUndoRegistrationEnabled
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_disableCount == 0)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
return YES;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) levelsOfUndo
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
return _levelsOfUndo;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) prepareWithInvocationTarget: (id)target
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_nextTarget = target;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return self;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) redo
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_isUndoing || _isRedoing)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"redo while undoing or redoing"];
|
|
|
|
|
}
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
|
postNotificationName: NSUndoManagerCheckpointNotification
|
|
|
|
|
object: self];
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if ([_redoStack count] > 0)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
PrivateUndoGroup *oldGroup;
|
|
|
|
|
PrivateUndoGroup *groupToRedo;
|
|
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
|
postNotificationName: NSUndoManagerWillRedoChangeNotification
|
|
|
|
|
object: self];
|
1999-09-16 07:21:34 +00:00
|
|
|
|
groupToRedo = [_redoStack objectAtIndex: [_redoStack count] - 1];
|
1999-09-28 19:35:09 +00:00
|
|
|
|
IF_NO_GC([groupToRedo retain]);
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[_redoStack removeObjectAtIndex: [_redoStack count] - 1];
|
|
|
|
|
oldGroup = _group;
|
|
|
|
|
_group = nil;
|
|
|
|
|
_isRedoing = YES;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[self beginUndoGrouping];
|
|
|
|
|
[groupToRedo perform];
|
|
|
|
|
RELEASE(groupToRedo);
|
|
|
|
|
[self endUndoGrouping];
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_isRedoing = NO;
|
|
|
|
|
_group = oldGroup;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
|
postNotificationName: NSUndoManagerDidRedoChangeNotification
|
|
|
|
|
object: self];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) redoActionName
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
if ([self canRedo] == NO)
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
return _actionName;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) redoMenuItemTitle
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return [self redoMenuTitleForUndoActionName: [self redoActionName]];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) redoMenuTitleForUndoActionName: (NSString*)name
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
if (name)
|
|
|
|
|
{
|
|
|
|
|
if ([name isEqual: @""])
|
|
|
|
|
{
|
|
|
|
|
return @"Redo";
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return [NSString stringWithFormat: @"Redo %@", name];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return name;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) registerUndoWithTarget: (id)target
|
|
|
|
|
selector: (SEL)aSelector
|
|
|
|
|
object: (id)anObject
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_disableCount == 0)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
NSMethodSignature *sig;
|
|
|
|
|
NSInvocation *inv;
|
|
|
|
|
PrivateUndoGroup *g;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_group == nil)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"registerUndo without beginUndoGrouping"];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
g = _group;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
sig = [target methodSignatureForSelector: aSelector];
|
|
|
|
|
inv = [NSInvocation invocationWithMethodSignature: sig];
|
|
|
|
|
[inv setTarget: target];
|
|
|
|
|
[inv setSelector: aSelector];
|
|
|
|
|
[inv setArgument: &anObject atIndex: 2];
|
|
|
|
|
[g addInvocation: inv];
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_isUndoing == NO)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[_redoStack removeAllObjects];
|
1998-07-28 09:45:09 +00:00
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_registeredUndo = YES;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) removeAllActions
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[_redoStack removeAllObjects];
|
|
|
|
|
[_undoStack removeAllObjects];
|
|
|
|
|
_isRedoing = NO;
|
|
|
|
|
_isUndoing = NO;
|
|
|
|
|
_disableCount = 0;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) removeAllActionsWithTarget: (id)target
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
unsigned i;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
i = [_redoStack count];
|
1999-07-03 19:59:44 +00:00
|
|
|
|
while (i-- > 0)
|
|
|
|
|
{
|
|
|
|
|
PrivateUndoGroup *g;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
g = [_redoStack objectAtIndex: i];
|
1999-07-03 19:59:44 +00:00
|
|
|
|
if ([g removeActionsForTarget: target] == NO)
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[_redoStack removeObjectAtIndex: i];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
i = [_undoStack count];
|
1999-07-03 19:59:44 +00:00
|
|
|
|
while (i-- > 0)
|
|
|
|
|
{
|
|
|
|
|
PrivateUndoGroup *g;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
|
g = [_undoStack objectAtIndex: i];
|
1999-07-03 19:59:44 +00:00
|
|
|
|
if ([g removeActionsForTarget: target] == NO)
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[_undoStack removeObjectAtIndex: i];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSArray*) runLoopModes
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
return _modes;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setActionName: (NSString*)name
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (name != nil && _actionName != name)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
ASSIGNCOPY(_actionName, name);
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setGroupsByEvent: (BOOL)flag
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_groupsByEvent != flag)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_groupsByEvent = flag;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setLevelsOfUndo: (unsigned)num
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_levelsOfUndo = num;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
if (num > 0)
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
while ([_undoStack count] > num)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[_undoStack removeObjectAtIndex: 0];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
while ([_redoStack count] > num)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[_redoStack removeObjectAtIndex: 0];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setRunLoopModes: (NSArray*)newModes
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_modes != newModes)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
ASSIGN(_modes, newModes);
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[[NSRunLoop currentRunLoop] cancelPerformSelector: @selector(_loop:)
|
|
|
|
|
target: self
|
|
|
|
|
argument: nil];
|
|
|
|
|
[[NSRunLoop currentRunLoop] performSelector: @selector(_loop:)
|
|
|
|
|
target: self
|
|
|
|
|
argument: nil
|
|
|
|
|
order: 0
|
1999-09-16 07:21:34 +00:00
|
|
|
|
modes: _modes];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) undo
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
if ([self groupingLevel] == 1)
|
|
|
|
|
{
|
|
|
|
|
[self endUndoGrouping];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_group != nil)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"undo with nested groups"];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[self undoNestedGroup];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) undoActionName
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
if ([self canUndo] == NO)
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
return _actionName;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) undoMenuItemTitle
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return [self undoMenuTitleForUndoActionName: [self undoActionName]];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) undoMenuTitleForUndoActionName: (NSString*)name
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
if (name)
|
|
|
|
|
{
|
|
|
|
|
if ([name isEqual: @""])
|
|
|
|
|
{
|
|
|
|
|
return @"Undo";
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return [NSString stringWithFormat: @"Undo %@", name];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return name;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) undoNestedGroup
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
PrivateUndoGroup *oldGroup;
|
|
|
|
|
PrivateUndoGroup *groupToUndo;
|
1998-05-21 13:41:55 +00:00
|
|
|
|
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
|
postNotificationName: NSUndoManagerCheckpointNotification
|
|
|
|
|
object: self];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
#if 0
|
|
|
|
|
/*
|
|
|
|
|
* The documentation says we should raise an exception - but I can't
|
|
|
|
|
* make sense of it - raising an exception seems to break everything.
|
|
|
|
|
* It would make more sense to raise an exception if NO undo operations
|
|
|
|
|
* had been registered.
|
|
|
|
|
*/
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_registeredUndo)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"undoNestedGroup with registered undo ops"];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_isUndoing || _isRedoing)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"undoNestedGroup while undoing or redoing"];
|
|
|
|
|
}
|
1999-09-16 07:21:34 +00:00
|
|
|
|
if (_group != nil && [_undoStack count] == 0)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
|
postNotificationName: NSUndoManagerWillUndoChangeNotification
|
|
|
|
|
object: self];
|
1999-09-16 07:21:34 +00:00
|
|
|
|
oldGroup = _group;
|
|
|
|
|
_group = nil;
|
|
|
|
|
_isUndoing = YES;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
if (oldGroup)
|
|
|
|
|
{
|
|
|
|
|
groupToUndo = oldGroup;
|
|
|
|
|
oldGroup = RETAIN([oldGroup parent]);
|
|
|
|
|
[groupToUndo orphan];
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[_redoStack addObject: groupToUndo];
|
1999-07-03 19:59:44 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
|
groupToUndo = [_undoStack objectAtIndex: [_undoStack count] - 1];
|
1999-09-28 19:35:09 +00:00
|
|
|
|
IF_NO_GC([groupToUndo retain]);
|
1999-09-16 07:21:34 +00:00
|
|
|
|
[_undoStack removeObjectAtIndex: [_undoStack count] - 1];
|
1999-07-03 19:59:44 +00:00
|
|
|
|
}
|
|
|
|
|
[self beginUndoGrouping];
|
|
|
|
|
[groupToUndo perform];
|
|
|
|
|
RELEASE(groupToUndo);
|
|
|
|
|
[self endUndoGrouping];
|
1999-09-16 07:21:34 +00:00
|
|
|
|
_isUndoing = NO;
|
|
|
|
|
_group = oldGroup;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
|
postNotificationName: NSUndoManagerDidUndoChangeNotification
|
|
|
|
|
object: self];
|
1998-05-21 13:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|