From 29e9de724510ccda961e5fc871be06147d943d29 Mon Sep 17 00:00:00 2001 From: Nick Takayama Date: Mon, 20 Oct 2008 13:56:57 +0800 Subject: [PATCH] unified patch to add delegate functions to CPTextField and CPControl Signed-off-by: Nick Takayama --- AppKit/CPControl.j | 31 +++++++++++++++++++ AppKit/CPTextField.j | 79 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 105 insertions(+), 5 deletions(-) diff --git a/AppKit/CPControl.j b/AppKit/CPControl.j index 421b89b..1ceff31 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,33 @@ 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 3c76043..012d3b9 100644 --- a/AppKit/CPTextField.j +++ b/AppKit/CPTextField.j @@ -108,6 +108,11 @@ var _CPTextFieldSquareBezelColor = nil; id _placeholderString; CPLineBreakMode _lineBreakMode; + + id _delegate; + + CPString _textDidChangeValue; + #if PLATFORM(DOM) DOMElement _DOMTextElement; #endif @@ -166,6 +171,34 @@ var _CPTextFieldSquareBezelColor = nil; return self; } +- (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 +{ + return _delegate; +} + // Setting the Bezel Style /* Sets whether the textfield will have a bezeled border. @@ -294,7 +327,7 @@ var _CPTextFieldSquareBezelColor = nil; //element.onblur = function() { objj_debug_print_backtrace(); } //element.select(); - element.onkeypress = function(aDOMEvent) + element.onkeydown = function(aDOMEvent) { aDOMEvent = aDOMEvent || window.event; @@ -310,12 +343,39 @@ var _CPTextFieldSquareBezelColor = nil; [self sendAction:[self action] to:[self target]]; [[self window] makeFirstResponder:nil]; } + + //all other key presses might trigger the delegate method controlTextDidChange: + //record the current string value before we allow this keydown to propagate + _textDidChangeValue = [self stringValue]; + + return true; + }; - // If current value is the placeholder value, remove it to allow user to update. - if ([_value lowercaseString] == [[self placeholderString] lowercaseString]) - [self setStringValue:@""]; + //inspect keyup to detect changes in order to trigger controlTextDidChange: delegate method + element.onkeyup = function(aDOMEvent) + { + //check if we should fire a notification for CPControlTextDidChange + if ([self stringValue] != _textDidChangeValue) + { + _textDidChangeValue = [self stringValue]; + //call to CPControls methods for posting the notification + [self textDidChange:[CPNotification notificationWithName:CPControlTextDidChangeNotification object:self userInfo:nil]]; + + } + + return true; + + }; + + // If current value is the placeholder value, remove it to allow user to update. + if ([string lowercaseString] == [[self placeholderString] lowercaseString]) + [self setStringValue:@""]; + + //post CPControlTextDidBeginEditingNotification + [self textDidBeginEditing:[CPNotification notificationWithName:CPControlTextDidBeginEditingNotification object:self userInfo:nil]]; + [[CPDOMWindowBridge sharedDOMWindowBridge] _propagateCurrentDOMEvent:YES]; #endif @@ -328,6 +388,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]; @@ -336,6 +400,9 @@ var _CPTextFieldSquareBezelColor = nil; [self setStringValue:[self placeholderString]]; #endif + //post CPControlTextDidEndEditingNotification + [self textDidEndEditing:[CPNotification notificationWithName:CPControlTextDidBeginEditingNotification object:self userInfo:nil]]; + return YES; } @@ -492,7 +559,7 @@ var _CPTextFieldSquareBezelColor = nil; _placeholderString = aStringValue; //if there is no set value, automatically display the placeholder - if (!_value) + if (!_value || _value === "") [self setStringValue:aStringValue]; } @@ -521,6 +588,8 @@ var _CPTextFieldSquareBezelColor = nil; #endif } + + @end var CPTextFieldIsSelectableKey = @"CPTextFieldIsSelectableKey", -- 1.6.0.2