Skip to content

Commit

Permalink
Fixed problem with MZTextView reported in #37
Browse files Browse the repository at this point in the history
  • Loading branch information
griff committed Mar 27, 2013
1 parent 13413a1 commit eb49b2a
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions App/src/MZTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
#import "MZTextView.h"


@interface MZTextViewObserver : NSObject {
id owner;
id observer;
NSString* keyPath;
}
@property(readonly) id owner;
@property(readonly) id observer;
@property(readonly) NSString* keyPath;

+ (id)observerWithOwner:(id)theOwner observer:(id)observer keyPath:(NSString *)keyPath;
- (id)initWithOwner:(id)theOwner observer:(id)observer keyPath:(NSString *)keyPath;
@end


/*!
* The proxy for talking to the editor
*/
Expand All @@ -28,11 +42,51 @@ @interface MZTextViewBindProxy : NSProxy {
MZTextView* textView;
id controller;
MZTextViewEditorProxy* editor;
NSMutableArray* observers;
}
- (id)initWithView:(MZTextView *)theTextView controller:(id)theController;
@end


@implementation MZTextViewObserver
@synthesize owner;
@synthesize observer;
@synthesize keyPath;

+ (id)observerWithOwner:(id)theOwner observer:(id)observer keyPath:(NSString *)keyPath;
{
return [[[self alloc] initWithOwner:theOwner observer:observer keyPath:keyPath] autorelease];
}

- (id)initWithOwner:(id)theOwner observer:(id)anObserver keyPath:(NSString *)theKeyPath;
{
self = [super init];
if(self)
{
owner = [theOwner retain];
observer = [anObserver retain];
keyPath = [theKeyPath copy];
}
return self;
}

- (BOOL)isEqual:(id)object
{
if(![object isKindOfClass:[MZTextViewObserver class]])
return NO;
MZTextViewObserver* other = (MZTextViewObserver *)object;
return owner == other.owner && observer == other.observer &&
[keyPath isEqualToString:other.keyPath];
}

- (void)observeValueForKeyPath:(NSString *)theKeyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
[observer observeValueForKeyPath:theKeyPath ofObject:owner change:change context:context];
}

@end


@implementation MZTextViewEditorProxy
@synthesize editor;

Expand All @@ -48,6 +102,17 @@ - (void)dealloc
[textView release];
[super dealloc];
}

- (BOOL)conformsToProtocol:(Protocol *)aProtocol
{
return [editor conformsToProtocol:aProtocol];
}

- (BOOL)respondsToSelector:(SEL)aSelector
{
return [editor respondsToSelector:aSelector];
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
return [editor methodSignatureForSelector:aSelector];
Expand Down Expand Up @@ -88,22 +153,55 @@ - (id)initWithView:(MZTextView *)theTextView controller:(id)theController
textView = [theTextView retain];
controller = [theController retain];
editor = [[MZTextViewEditorProxy alloc] initWithView:textView];
observers = [[NSMutableArray alloc] init];
return self;
}

- (void)dealloc
{
for(MZTextViewObserver* ob in observers)
[controller removeObserver:ob forKeyPath:ob.keyPath];
[textView release];
[controller release];
[editor release];
[observers release];
[super dealloc];
}

- (BOOL)conformsToProtocol:(Protocol *)aProtocol
{
return [controller conformsToProtocol:aProtocol];
}

- (BOOL)respondsToSelector:(SEL)aSelector
{
return [controller respondsToSelector:aSelector];
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
return [controller methodSignatureForSelector:aSelector];
}

- (void)addObserver:(NSObject *)anObserver forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context
{
MZTextViewObserver* ob = [MZTextViewObserver observerWithOwner:self observer:anObserver keyPath:keyPath];
[controller addObserver:ob forKeyPath:keyPath options:options context:context];
[observers addObject:ob];
}

- (void)removeObserver:(NSObject *)anObserver forKeyPath:(NSString *)keyPath
{
MZTextViewObserver* ob = [MZTextViewObserver observerWithOwner:self observer:anObserver keyPath:keyPath];
NSUInteger idx = [observers indexOfObject:ob];
if(idx != NSNotFound)
{
ob = [observers objectAtIndex:idx];
[controller removeObserver:ob forKeyPath:keyPath];
[observers removeObjectAtIndex:idx];
}
}

- (void)forwardInvocation:(NSInvocation *)anInvocation
{
[anInvocation setTarget:controller];
Expand Down

0 comments on commit eb49b2a

Please sign in to comment.