From 1d645ddbc50d13a060197ea42d2d4ac91167f9b8 Mon Sep 17 00:00:00 2001 From: Nick Takayama Date: Mon, 13 Oct 2008 13:25:56 +0800 Subject: [PATCH] implementing the main delegate methods for CPControl and CPTextField not implementing shouldBeginEditing or shouldEndEditing yet Signed-off-by: Nick Takayama --- AppKit/CPControl.j | 28 ++++++++++++++++++++++++++++ AppKit/CPTextField.j | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/AppKit/CPControl.j b/AppKit/CPControl.j index 421b89b..e946f47 100644 --- a/AppKit/CPControl.j +++ b/AppKit/CPControl.j @@ -73,6 +73,10 @@ CPControlSelectedBackgroundColor = @"CPControlSelectedBackgroundColor"; CPControlHighlightedBackgroundColor = @"CPControlHighlightedBackgroundColor"; CPControlDisabledBackgroundColor = @"CPControlDisabledBackgroundColor"; +CPControlTextDidBeginEditingNotification=@"CPControlTextDidBeginEditingNotification"; +CPControlTextDidChangeNotification=@"CPControlTextDidChangeNotification"; +CPControlTextDidEndEditingNotification=@"CPControlTextDidEndEditingNotification"; + var CPControlBlackColor = [CPColor blackColor]; /* @@ -386,6 +390,30 @@ var CPControlBlackColor = [CPColor blackColor]; [super setBackgroundColor:[self backgroundColorForName:aName]]; } +- (void)textDidBeginEditing:(CPNotification)note { + //this looks to prevent false propagation of notifications for other objects + if([note object] != self) + return; + + [[CPNotificationCenter defaultCenter] postNotificationName:CPControlTextDidBeginEditingNotification object:self userInfo:[CPDictionary dictionaryWithObject:[note object] forKey:@"CPFieldEditor"]]; +} + +- (void)textDidChange:(CPNotification)note { + //this looks to prevent false propagation of notifications for other objects + if([note object] != self) + return; + + [[CPNotificationCenter defaultCenter] postNotificationName:CPControlTextDidChangeNotification object:self userInfo:[CPDictionary dictionaryWithObject:[note object] forKey:@"CPFieldEditor"]]; +} + +- (void)textDidEndEditing:(CPNotification)note { + //this looks to prevent false propagation of notifications for other objects + if([note object] != self) + return; + + [[CPNotificationCenter defaultCenter] postNotificationName:CPControlTextDidEndEditingNotification object:self userInfo:[CPDictionary dictionaryWithObject:[note object] forKey:@"CPFieldEditor"]]; +} + /* Ð doubleValue Ð setDoubleValue: diff --git a/AppKit/CPTextField.j b/AppKit/CPTextField.j index d25e532..aff5455 100644 --- a/AppKit/CPTextField.j +++ b/AppKit/CPTextField.j @@ -173,7 +173,25 @@ var _CPTextFieldSquareBezelColor = nil; - (void)setDelegate:(id)aDelegate { + var center = [CPNotificationCenter defaultCenter]; + + //unsubscribe the existing delegate if it exists + if (_delegate) + { + [center removeObserver:_delegate name:CPControlTextDidBeginEditingNotification object:self]; + [center removeObserver:_delegate name:CPControlTextDidChangeNotification object:self]; + [center removeObserver:_delegate name:CPControlTextDidEndEditingNotification object:self]; + } + _delegate = aDelegate; + + if ([_delegate respondsToSelector:@selector(controlTextDidBeginEditing:)]) + [center addObserver:_delegate selector:@selector(controlTextDidBeginEditing:) name:CPControlTextDidBeginEditingNotification object:self]; + if ([_delegate respondsToSelector:@selector(controlTextDidChange:)]) + [center addObserver:_delegate selector:@selector(controlTextDidChange:) name:CPControlTextDidChangeNotification object:self]; + if ([_delegate respondsToSelector:@selector(controlTextDidEndEditing:)]) + [center addObserver:_delegate selector:@selector(controlTextDidEndEditing:) name:CPControlTextDidEndEditingNotification object:self]; + } - (id)delegate @@ -335,12 +353,14 @@ var _CPTextFieldSquareBezelColor = nil; //inspect keyup to detect changes in order to trigger controlTextDidChange: delegate method element.onkeyup = function(aDOMEvent) { - //all other key presses trigger the delegate method controlTextDidChange: , as would happen in NSTextField + //check if we should fire a notification for CPControlTextDidChange if ([self stringValue] != _textDidChangeValue) { _textDidChangeValue = [self stringValue]; - if ([_delegate respondsToSelector:@selector(controlTextDidChange:)]) - window.setTimeout(function() {[_delegate controlTextDidChange:self]}, 0.0); + + //call to CPControls methods for posting the notification + [self textDidChange:[CPNotification notificationWithName:CPControlTextDidChangeNotification object:self userInfo:nil]]; + } return true; @@ -351,6 +371,9 @@ var _CPTextFieldSquareBezelColor = nil; if ([string lowercaseString] == [[self placeholderString] lowercaseString]) [self setStringValue:@""]; + //post CPControlTextDidBeginEditingNotification + [self textDidBeginEditing:[CPNotification notificationWithName:CPControlTextDidBeginEditingNotification object:self userInfo:nil]]; + [[CPDOMWindowBridge sharedDOMWindowBridge] _propagateCurrentDOMEvent:YES]; #endif @@ -363,6 +386,10 @@ var _CPTextFieldSquareBezelColor = nil; #if PLATFORM(DOM) var element = [[self class] _inputElement]; + //nil out dom handlers + element.onkeyup = nil; + element.onkeydown = nil; + _DOMElement.removeChild(element); [self setStringValue:element.value]; @@ -371,6 +398,9 @@ var _CPTextFieldSquareBezelColor = nil; [self setStringValue:[self placeholderString]]; #endif + //post CPControlTextDidEndEditingNotification + [self textDidEndEditing:[CPNotification notificationWithName:CPControlTextDidBeginEditingNotification object:self userInfo:nil]]; + return YES; } @@ -557,6 +587,8 @@ var _CPTextFieldSquareBezelColor = nil; #endif } + + @end var CPTextFieldIsSelectableKey = @"CPTextFieldIsSelectableKey", -- 1.5.4.5