From 3175b0e9384121808973934d7a9fa4ccb37bad4a Mon Sep 17 00:00:00 2001 From: Pedro Cuenca Date: Sun, 3 Jun 2012 15:39:59 +0200 Subject: [PATCH 1/2] * Do not import Cocoa.h so the code compiles on iPhone. --- GCUndoManager.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/GCUndoManager.h b/GCUndoManager.h index aa83c12..67ec7cd 100644 --- a/GCUndoManager.h +++ b/GCUndoManager.h @@ -12,8 +12,6 @@ // 2011/01/11 - fix to ensure submitting tasks in response to a checkpoint notification is correctly handled // 2011/07/08 - added NSUndoManagerDidCloseUndoGroupNotification for 10.7 (Lion) compatibility -#import - // internal undo manager state is one of these constants typedef enum From 50e718981cd1dba2cf85467e8da7809e0d10b7e4 Mon Sep 17 00:00:00 2001 From: Pedro Cuenca Date: Sun, 3 Jun 2012 16:53:27 +0200 Subject: [PATCH 2/2] * Post a notification when an action is about to be dropped because the 'levelsOfUndo' limit was reached. The userInfo dictionary associated to the notification contains the to-be-dropped action for the GCUndoManagerActionKey key. Two notifications are, in fact, posted: GCUndoManagerWillDropUndoActionNotification when dropping undo actions, or GCUndoManagerWillDropRedoActionNotification when dropping redo actions. The latter is only posted when setting 'levelsOfUndo' to a number greater than the current count of the redo stack. --- GCUndoManager.h | 3 +++ GCUndoManager.m | 27 ++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/GCUndoManager.h b/GCUndoManager.h index 67ec7cd..f6cce86 100644 --- a/GCUndoManager.h +++ b/GCUndoManager.h @@ -29,6 +29,9 @@ typedef enum } GCUndoTaskCoalescingKind; +extern NSString * const GCUndoManagerWillDropUndoActionNotification; +extern NSString * const GCUndoManagerWillDropRedoActionNotification; +extern NSString * const GCUndoManagerActionKey; @class GCUndoGroup, GCUndoManagerProxy, GCConcreteUndoTask; diff --git a/GCUndoManager.m b/GCUndoManager.m index 373eb4e..87417b7 100644 --- a/GCUndoManager.m +++ b/GCUndoManager.m @@ -12,6 +12,9 @@ // on 10.6 so that a wider range of methods can be submitted as undo tasks. Unlike 10.6 however, it does not bypass um's -forwardInvocation: // method, so subclasses still work when -forwardInvocaton: is overridden. +NSString * const GCUndoManagerWillDropUndoActionNotification = @"GCUndoManagerWillDropUndoActionNotification"; +NSString * const GCUndoManagerWillDropRedoActionNotification = @"GCUndoManagerWillDropRedoActionNotification"; +NSString * const GCUndoManagerActionKey = @"GCUndoManagerActionKey"; @interface GCUndoManagerProxy : NSProxy { @@ -145,7 +148,7 @@ - (void) endUndoGrouping mIsRemovingTargets = YES; while([self numberOfUndoActions] > [self levelsOfUndo]) - [mUndoStack removeObjectAtIndex:0]; + [self dropOldestUndoAction]; mIsRemovingTargets = NO; } @@ -317,6 +320,24 @@ - (void) setGroupsByEvent:(BOOL) groupByEvent +- (void) dropOldestUndoAction +{ + NSDictionary * userInfo = [NSDictionary dictionaryWithObject:[mUndoStack objectAtIndex:0] forKey:GCUndoManagerActionKey]; + [[NSNotificationCenter defaultCenter] postNotificationName:GCUndoManagerWillDropUndoActionNotification object:self userInfo:userInfo]; + [mUndoStack removeObjectAtIndex:0]; +} + + + +- (void) dropOldestRedoAction +{ + NSDictionary * userInfo = [NSDictionary dictionaryWithObject:[mRedoStack objectAtIndex:0] forKey:GCUndoManagerActionKey]; + [[NSNotificationCenter defaultCenter] postNotificationName:GCUndoManagerWillDropRedoActionNotification object:self userInfo:userInfo]; + [mRedoStack removeObjectAtIndex:0]; +} + + + - (NSUInteger) levelsOfUndo { return mLevelsOfUndo; @@ -335,10 +356,10 @@ - (void) setLevelsOfUndo:(NSUInteger) levels mIsRemovingTargets = YES; while([self numberOfUndoActions] > levels) - [mUndoStack removeObjectAtIndex:0]; + [self dropOldestUndoAction]; while([self numberOfRedoActions] > levels) - [mRedoStack removeObjectAtIndex:0]; + [self dropOldestRedoAction]; mIsRemovingTargets = NO; }