VocalEasel/Sources/VLKeyValueUndo.mm

66 lines
1.7 KiB
Plaintext
Raw Permalink Normal View History

2006-12-04 07:04:24 +00:00
//
2007-04-27 06:41:34 +00:00
// File: VLKeyValueUndo.mm - Automatically handle undo functionality for
// key-value paths
2006-12-04 07:04:24 +00:00
//
2007-04-27 06:41:34 +00:00
// Author(s):
//
// (MN) Matthias Neeracher
//
2011-09-03 20:34:53 +00:00
// Copyright © 2007-2011 Matthias Neeracher
2006-12-04 07:04:24 +00:00
//
#import "VLKeyValueUndo.h"
2011-09-03 20:34:53 +00:00
#import "VLDocument.h"
2006-12-04 07:04:24 +00:00
@implementation VLKeyValueUndo
- (id)initWithOwner:(id)o keysAndNames:(NSDictionary *)kn update:(VLKeyValueUpdateHook)hook
2006-12-04 07:04:24 +00:00
{
owner = o;
keysAndNames = [kn retain];
updateHook = Block_copy(hook);
2006-12-04 07:04:24 +00:00
for (NSEnumerator * e = [keysAndNames keyEnumerator];
NSString * key = [e nextObject];
)
[owner addObserver:self forKeyPath:key
options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew
context:[keysAndNames objectForKey:key]];
2006-12-04 07:04:24 +00:00
return self;
}
- (id)initWithOwner:(id)o keysAndNames:(NSDictionary *)kn
2006-12-04 07:04:24 +00:00
{
return [self initWithOwner:o keysAndNames:kn update:nil];
2007-04-16 05:35:52 +00:00
}
2006-12-04 07:04:24 +00:00
2007-04-16 05:35:52 +00:00
- (void) dealloc
{
for (NSEnumerator * e = [keysAndNames keyEnumerator];
NSString * key = [e nextObject];
)
[owner removeObserver:self forKeyPath:key];
2006-12-04 07:04:24 +00:00
[keysAndNames release];
[updateHook release];
2006-12-04 07:04:24 +00:00
[super dealloc];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
id oldVal = [change objectForKey:NSKeyValueChangeOldKey];
id newVal = [change objectForKey:NSKeyValueChangeNewKey];
if (![oldVal isEqual:newVal]) {
NSUndoManager * undo = [owner undoManager];
NSString * name = [keysAndNames objectForKey:keyPath];
[undo registerUndoWithTarget:owner selector:@selector(setValuesForKeysWithDictionary:)
object: [NSDictionary dictionaryWithObjectsAndKeys: oldVal, keyPath, nil]];
[undo setActionName: name];
if (updateHook)
updateHook(keyPath);
2006-12-04 07:04:24 +00:00
}
}
@end