Skip to content

Commit

Permalink
Andy Balholm's Undo in text fields patch
Browse files Browse the repository at this point in the history
  • Loading branch information
cjwl committed Jul 19, 2009
1 parent a4aac16 commit ed850b0
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 34 deletions.
20 changes: 20 additions & 0 deletions AppKit/AppKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,14 @@
FE6BE4490F446B92007D7F3F /* NSToolbarItemGroup.m in Sources */ = {isa = PBXBuildFile; fileRef = FE6BE4450F446B92007D7F3F /* NSToolbarItemGroup.m */; };
FE6BE44A0F446B92007D7F3F /* NSToolbarItemGroup.h in Headers */ = {isa = PBXBuildFile; fileRef = FE6BE4440F446B92007D7F3F /* NSToolbarItemGroup.h */; settings = {ATTRIBUTES = (Public, ); }; };
FE6BE44B0F446B92007D7F3F /* NSToolbarItemGroup.m in Sources */ = {isa = PBXBuildFile; fileRef = FE6BE4450F446B92007D7F3F /* NSToolbarItemGroup.m */; };
FE6D918C101381F600BA6BAD /* NSCellUndoManager.h in Headers */ = {isa = PBXBuildFile; fileRef = FE6D918A101381F600BA6BAD /* NSCellUndoManager.h */; };
FE6D918D101381F600BA6BAD /* NSCellUndoManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FE6D918B101381F600BA6BAD /* NSCellUndoManager.m */; };
FE6D918E101381F600BA6BAD /* NSCellUndoManager.h in Headers */ = {isa = PBXBuildFile; fileRef = FE6D918A101381F600BA6BAD /* NSCellUndoManager.h */; };
FE6D918F101381F600BA6BAD /* NSCellUndoManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FE6D918B101381F600BA6BAD /* NSCellUndoManager.m */; };
FE6D9190101381F600BA6BAD /* NSCellUndoManager.h in Headers */ = {isa = PBXBuildFile; fileRef = FE6D918A101381F600BA6BAD /* NSCellUndoManager.h */; };
FE6D9191101381F600BA6BAD /* NSCellUndoManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FE6D918B101381F600BA6BAD /* NSCellUndoManager.m */; };
FE6D9192101381F600BA6BAD /* NSCellUndoManager.h in Headers */ = {isa = PBXBuildFile; fileRef = FE6D918A101381F600BA6BAD /* NSCellUndoManager.h */; };
FE6D9193101381F600BA6BAD /* NSCellUndoManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FE6D918B101381F600BA6BAD /* NSCellUndoManager.m */; };
FE6EDE340DB797D9005503A1 /* KGExceptions.h in Headers */ = {isa = PBXBuildFile; fileRef = FE6EDE330DB797D9005503A1 /* KGExceptions.h */; };
FE6EE27E0DB91EA8005503A1 /* VGmath.h in Headers */ = {isa = PBXBuildFile; fileRef = FE6EE27C0DB91EA8005503A1 /* VGmath.h */; settings = {ATTRIBUTES = (Private, ); }; };
FE6EE28E0DB91FDF005503A1 /* KGSurface.h in Headers */ = {isa = PBXBuildFile; fileRef = FE6EE28C0DB91FDF005503A1 /* KGSurface.h */; settings = {ATTRIBUTES = (Private, ); }; };
Expand Down Expand Up @@ -3567,6 +3575,8 @@
FE66F0050E513AE8006C5796 /* CGImageProperties.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CGImageProperties.m; sourceTree = "<group>"; };
FE6BE4440F446B92007D7F3F /* NSToolbarItemGroup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSToolbarItemGroup.h; sourceTree = "<group>"; };
FE6BE4450F446B92007D7F3F /* NSToolbarItemGroup.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSToolbarItemGroup.m; sourceTree = "<group>"; };
FE6D918A101381F600BA6BAD /* NSCellUndoManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSCellUndoManager.h; sourceTree = "<group>"; };
FE6D918B101381F600BA6BAD /* NSCellUndoManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSCellUndoManager.m; sourceTree = "<group>"; };
FE6DC16A0BA85BEC0082AC66 /* NSOpenGLPixelBuffer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSOpenGLPixelBuffer.h; sourceTree = "<group>"; };
FE6DC16B0BA85BEC0082AC66 /* NSOpenGLPixelBuffer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSOpenGLPixelBuffer.m; sourceTree = "<group>"; };
FE6DC3DC0BA9A6EB0082AC66 /* NSDatePicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSDatePicker.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3786,6 +3796,8 @@
6E2B533B0976075200DA0954 /* NSCachedImageRep.m */,
6E2B54160976075200DA0954 /* NSCell.h */,
6E2B533D0976075200DA0954 /* NSCell.m */,
FE6D918A101381F600BA6BAD /* NSCellUndoManager.h */,
FE6D918B101381F600BA6BAD /* NSCellUndoManager.m */,
6E2B54180976075200DA0954 /* NSClipView.h */,
6E2B533F0976075200DA0954 /* NSClipView.m */,
6E2B55430976075300DA0954 /* NSColor */,
Expand Down Expand Up @@ -5190,6 +5202,7 @@
FE2C7F560FDC4C8E00D5DFB5 /* NSStringKVCFunctions.h in Headers */,
A1CC701D100534AB00DB9974 /* NSCachingBinder.h in Headers */,
A1CC70441005371100DB9974 /* NSTextBinder.h in Headers */,
FE6D918E101381F600BA6BAD /* NSCellUndoManager.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -5553,6 +5566,7 @@
FE2C7F550FDC4C8E00D5DFB5 /* NSStringKVCFunctions.h in Headers */,
A1CC701F100534AB00DB9974 /* NSCachingBinder.h in Headers */,
A1CC70421005371100DB9974 /* NSTextBinder.h in Headers */,
FE6D9190101381F600BA6BAD /* NSCellUndoManager.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -5911,6 +5925,7 @@
FEA661230FF5C6F200E2C03B /* KGFont_gdi.h in Headers */,
A1CC7019100534AB00DB9974 /* NSCachingBinder.h in Headers */,
A1CC70481005371100DB9974 /* NSTextBinder.h in Headers */,
FE6D918C101381F600BA6BAD /* NSCellUndoManager.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -6197,6 +6212,7 @@
FE2C7F580FDC4C8E00D5DFB5 /* NSStringKVCFunctions.h in Headers */,
A1CC701B100534AB00DB9974 /* NSCachingBinder.h in Headers */,
A1CC70461005371100DB9974 /* NSTextBinder.h in Headers */,
FE6D9192101381F600BA6BAD /* NSCellUndoManager.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -6956,6 +6972,7 @@
FEB07FA10FF9B37C00C27509 /* KGFont+PDF.m in Sources */,
A1CC701E100534AB00DB9974 /* NSCachingBinder.m in Sources */,
A1CC70451005371100DB9974 /* NSTextBinder.m in Sources */,
FE6D918F101381F600BA6BAD /* NSCellUndoManager.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -7287,6 +7304,7 @@
FEB07FA20FF9B37C00C27509 /* KGFont+PDF.m in Sources */,
A1CC7020100534AB00DB9974 /* NSCachingBinder.m in Sources */,
A1CC70431005371100DB9974 /* NSTextBinder.m in Sources */,
FE6D9191101381F600BA6BAD /* NSCellUndoManager.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -7652,6 +7670,7 @@
FEB07FA00FF9B37C00C27509 /* KGFont+PDF.m in Sources */,
A1CC701A100534AB00DB9974 /* NSCachingBinder.m in Sources */,
A1CC70491005371100DB9974 /* NSTextBinder.m in Sources */,
FE6D918D101381F600BA6BAD /* NSCellUndoManager.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -7888,6 +7907,7 @@
FEB07FA30FF9B37C00C27509 /* KGFont+PDF.m in Sources */,
A1CC701C100534AB00DB9974 /* NSCachingBinder.m in Sources */,
A1CC70471005371100DB9974 /* NSTextBinder.m in Sources */,
FE6D9193101381F600BA6BAD /* NSCellUndoManager.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
10 changes: 10 additions & 0 deletions AppKit/NSCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#import <Foundation/NSLocale.h>
#import <Foundation/NSNumberFormatter.h>
#import "NSCellUndoManager.h"
#import <AppKit/NSTextView.h>

@implementation NSCell

Expand Down Expand Up @@ -749,6 +751,14 @@ -(void)_setupFieldEditorWithFrame:(NSRect)frame controlView:(NSView *)view edito
}
[[view window] makeFirstResponder:editor];
[editor setDelegate:delegate];

if ([editor isKindOfClass:[NSTextView class]]) {
NSCellUndoManager * undoManager = [[NSCellUndoManager alloc] init];
[undoManager setNextUndoManager:[[view window] undoManager]];
[(NSTextView *)editor _setFieldEditorUndoManager:undoManager];
[undoManager release];
[(NSTextView *)editor setAllowsUndo:YES];
}
}

-(void)editWithFrame:(NSRect)frame inView:(NSView *)view editor:(NSText *)editor delegate:(id)delegate event:(NSEvent *)event {
Expand Down
28 changes: 28 additions & 0 deletions AppKit/NSCellUndoManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright (c) 2009 Andy Balholm
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

// An undo manager for text field cells. It maintains a private undo stack for typing events,
// but forwards undo messages to another undo manager when its own undo stack is empty.

#import <Foundation/NSUndoManager.h>


@interface NSCellUndoManager : NSUndoManager
{
NSUndoManager * _nextUndoManager;
}

- (BOOL)canUndo;
- (void)undo;

- (BOOL)canRedo;
- (void)redo;

- (void)setNextUndoManager:(NSUndoManager *)manager;

@end
75 changes: 75 additions & 0 deletions AppKit/NSCellUndoManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* Copyright (c) 2009 Andy Balholm
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#import "NSCellUndoManager.h"

#import <Foundation/Foundation.h>


@implementation NSCellUndoManager

- (void)dealloc
{
[_nextUndoManager release];
[super dealloc];
}

- (BOOL)canUndo
{
return [super canUndo] || [_nextUndoManager canUndo];
}

- (void)undo
{
if ([super canUndo])
{
[super undo];
}
else
{
[_nextUndoManager undo];
}
}

- (BOOL)canRedo
{
return [_nextUndoManager canRedo] || [super canRedo];
}

- (void)redo
{
if ([_nextUndoManager canRedo])
{
[_nextUndoManager redo];
}
else
{
[super redo];
}
}

- (void)setNextUndoManager:(NSUndoManager *)manager
{
[manager retain];
[_nextUndoManager release];
_nextUndoManager = manager;
}

- (void)forwardInvocation:(NSInvocation *)invocation
{
[super forwardInvocation:invocation];
[_nextUndoManager clearRedoStackIfStateIsNormal];
}

- (void)registerUndoWithTarget:(id)target selector:(SEL)selector object:(id)object
{
[super registerUndoWithTarget:target selector:selector object:object];
[_nextUndoManager clearRedoStackIfStateIsNormal];
}

@end
15 changes: 15 additions & 0 deletions AppKit/NSTextView.subproj/NSTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

/*****************************************************************************************
* Note to programmers modifying or subclassing NSTextView: *
* *
* NSTextView uses undo coalescing to join adjacent keystrokes into one undo operation. *
* So you need to call breakUndoCoalescing before registering any other undo operations. *
*****************************************************************************************/


#import <AppKit/NSText.h>
#import <AppKit/NSTextInput.h>
#import <AppKit/NSDragging.h>
Expand Down Expand Up @@ -84,6 +92,11 @@ APPKIT_EXPORT NSString *NSOldSelectedCharacterRange;
NSString *_userCompletionHint; // original "hint" text which started completion
NSArray *_userCompletions; // current list of completions. shouldn't change while modal
int _userCompletionSelectedItem; // index within completion array

NSUndoManager *_fieldEditorUndoManager;
NSString *_undoString; // The text that is being replaced by the current typing operation
NSRange _undoRange; // The range of text that was entered in the current typing operation
BOOL _processingKeyEvent;
}

-initWithFrame:(NSRect)frame textContainer:(NSTextContainer *)container;
Expand Down Expand Up @@ -161,6 +174,8 @@ APPKIT_EXPORT NSString *NSOldSelectedCharacterRange;
-(NSDragOperation)dragOperationForDraggingInfo:(id <NSDraggingInfo>)info type:(NSString *)type;
-(void)cleanUpAfterDragOperation;

-(void)_setFieldEditorUndoManager:(NSUndoManager *)undoManager;
-(void)breakUndoCoalescing;
@end

@interface NSObject(NSTextView_undoManager)
Expand Down
Loading

0 comments on commit ed850b0

Please sign in to comment.