Updated CPTextField.

Reviewed by me.
This commit is contained in:
Francisco Tolmasky committed 2009-02-10 02:41:38 -08:00
1 parent 50baf5fd50
commit 0f34966f43
2 files changed
+281 -196

No files matched your search

+281 -149
View File
@@ -23,6 +23,7 @@
@import "CPControl.j"
@import "CPStringDrawing.j"
#include "CoreGraphics/CGGeometry.j"
#include "Platform/Platform.h"
#include "Platform/DOM/CPDOMDisplayServer.h"
@@ -103,9 +104,6 @@ var _CPTextFieldSquareBezelColor = nil,
BOOL _isEditable;
BOOL _isSelectable;
BOOL _isBordered;
BOOL _isBezeled;
CPTextFieldBezelStyle _bezelStyle;
BOOL _drawsBackground;
CPLineBreakMode _lineBreakMode;
@@ -117,9 +115,20 @@ var _CPTextFieldSquareBezelColor = nil,
CPString _textDidChangeValue;
#if PLATFORM(DOM)
DOMElement _DOMTextElement;
#endif
//
CPView _bezelView;
_CPImageAndTextView _contentView;
// NS-style Display Properties
CPTextFieldBezelStyle _bezelStyle;
BOOL _isBordered;
CPControlSize _controlSize;
}
+ (id)themedAttributes
{
return [CPDictionary dictionaryWithObjects:[_CGInsetMakeZero(), _CGInsetMakeZero(), nil]
forKeys:[@"bezel-inset", @"content-inset", @"bezel-color"]];
}
/* @ignore */
@@ -157,7 +166,8 @@ var _CPTextFieldSquareBezelColor = nil,
_placeholderString = "";
_sendActionOn = CPKeyUpMask | CPKeyDownMask;
/*
#if PLATFORM(DOM)
_DOMTextElement = document.createElement("div");
_DOMTextElement.style.position = "absolute";
@@ -180,38 +190,46 @@ var _CPTextFieldSquareBezelColor = nil,
_DOMElement.appendChild(_DOMTextElement);
#endif
[self setAlignment:CPLeftTextAlignment];
*/
[self setValue:CPLeftTextAlignment forThemedAttributeName:@"alignment"];
}
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];
#pragma mark Controlling Editability and Selectability
/*!
Sets whether or not the receiver text field can be edited
*/
- (void)setEditable:(BOOL)shouldBeEditable
{
_isEditable = shouldBeEditable;
}
- (id)delegate
/*!
Returns <code>YES</code> if the textfield is currently editable by the user.
*/
- (BOOL)isEditable
{
return _delegate;
return _isEditable;
}
/*!
Sets whether the field's text is selectable by the user.
@param aFlag <code>YES</code> makes the text selectable
*/
- (void)setSelectable:(BOOL)aFlag
{
_isSelectable = aFlag;
}
/*!
Returns <code>YES</code> if the field's text is selectable by the user.
*/
- (BOOL)isSelectable
{
return _isSelectable;
}
// Setting the Bezel Style
@@ -248,14 +266,14 @@ var _CPTextFieldSquareBezelColor = nil,
_bezelStyle = aBezelStyle;
#if PLATFORM(DOM)
/*#if PLATFORM(DOM)
if (aBezelStyle == CPTextFieldRoundedBezel)
_DOMTextElement.style.paddingLeft = ROUNDEDBEZEL_HORIZONTAL_PADDING - 1.0 + "px";
else
_DOMTextElement.style.paddingLeft = "0px";
#endif
[self _updateBackground];
[self _updateBackground];*/
}
/*!
@@ -395,6 +413,7 @@ var _CPTextFieldSquareBezelColor = nil,
/* @ignore */
- (BOOL)becomeFirstResponder
{
[_contentView setHidden:YES];
var string = [self stringValue];
[self setStringValue:""];
@@ -402,7 +421,7 @@ var _CPTextFieldSquareBezelColor = nil,
#if PLATFORM(DOM)
var element = [[self class] _inputElement];
element.value = string;
element.value = "hey there hot shot";//string;
element.style.color = _DOMElement.style.color;
element.style.font = _DOMElement.style.font;
element.style.zIndex = 1000;
@@ -424,7 +443,26 @@ var _CPTextFieldSquareBezelColor = nil,
}
_DOMElement.appendChild(element);
window.setTimeout(function() { element.focus(); }, 0.0);
// [anEvent _DOMEvent].
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("mousedown", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
var canceled = !element.dispatchEvent(evt);/*
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("mousedown", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
var canceled = !element.dispatchEvent(evt);
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("mousemove", true, true, window,
1, 0, 0, 20, 0, false, false, false, false, 0, null);
/*if(canceled) {
// A handler called preventDefault
alert("canceled");
} else {
// None of the handlers called preventDefault
alert("not canceled");
}*/
// window.setTimeout(function() { element.focus(); }, 0.0);
element.onblur = function ()
{
@@ -519,7 +557,7 @@ var _CPTextFieldSquareBezelColor = nil,
{
if (![self isEditable])
return [[self nextResponder] mouseDown:anEvent];
[super mouseDown:anEvent];
}
/*
@@ -531,26 +569,11 @@ var _CPTextFieldSquareBezelColor = nil,
[super mouseUp:anEvent];
}
*/
/*!
Sets whether or not the receiver text field can be edited
*/
- (void)setEditable:(BOOL)shouldBeEditable
{
_isEditable = shouldBeEditable;
}
/*!
Returns <code>YES</code> if the textfield is currently editable by the user.
*/
- (BOOL)isEditable
{
return _isEditable;
}
- (void)setFrameSize:(CGSize)aSize
{
[super setFrameSize:aSize];
/*
#if PLATFORM(DOM)
if (_isBezeled && _bezelStyle == CPTextFieldRoundedBezel)
{
@@ -561,92 +584,7 @@ var _CPTextFieldSquareBezelColor = nil,
CPDOMDisplayServerSetStyleSize(_DOMTextElement, _frame.size.width - 2.0 * HORIZONTAL_PADDING, _frame.size.height - TOP_PADDING - BOTTOM_PADDING);
}
#endif
}
/*!
Returns <code>YES</code> if the field's text is selectable by the user.
*/
- (BOOL)isSelectable
{
return _isSelectable;
}
/*!
Sets whether the field's text is selectable by the user.
@param aFlag <code>YES</code> makes the text selectable
*/
- (void)setSelectable:(BOOL)aFlag
{
_isSelectable = aFlag;
}
/*!
Sets the alignment of the text in the field.
@param anAlignment
*/
- (void)setAlignment:(CPTextAlignment)anAlignment
{
if ([self alignment] == anAlignment)
return;
[super setAlignment:anAlignment];
#if PLATFORM(DOM)
switch ([self alignment])
{
case CPLeftTextAlignment: _DOMTextElement.style.textAlign = "left";
break;
case CPRightTextAlignment: _DOMTextElement.style.textAlign = "right";
break;
case CPCenterTextAlignment: _DOMTextElement.style.textAlign = "center";
break;
case CPJustifiedTextAlignment: _DOMTextElement.style.textAlign = "justify";
break;
case CPNaturalTextAlignment: _DOMTextElement.style.textAlign = "";
break;
}
#endif
}
/*!
Sets the way line breaks occur in the text field.
@param aLineBreakMode the line break style
*/
- (void)setLineBreakMode:(CPLineBreakMode)aLineBreakMode
{
_lineBreakMode = aLineBreakMode;
#if PLATFORM(DOM)
switch (aLineBreakMode)
{
case CPLineBreakByTruncatingTail: _DOMTextElement.style.textOverflow = "ellipsis";
_DOMTextElement.style.whiteSpace = "nowrap";
_DOMTextElement.style.overflow = "hidden";
if (document.attachEvent)
_DOMTextElement.style.wordWrap = "normal";
break;
case CPLineBreakByWordWrapping: if (document.attachEvent)
{
_DOMTextElement.style.whiteSpace = "pre";
_DOMTextElement.style.wordWrap = "break-word";
}
else
{
_DOMTextElement.style.whiteSpace = "-o-pre-wrap";
_DOMTextElement.style.whiteSpace = "-pre-wrap";
_DOMTextElement.style.whiteSpace = "-moz-pre-wrap";
_DOMTextElement.style.whiteSpace = "pre-wrap";
}
_DOMTextElement.style.overflow = "hidden";
_DOMTextElement.style.textOverflow = "clip";
break;
}
#endif
}
/*!
@@ -672,7 +610,7 @@ var _CPTextFieldSquareBezelColor = nil,
- (void)setObjectValue:(id)aValue
{
[super setObjectValue:aValue];
/*
#if PLATFORM(DOM)
var displayString = "";
@@ -692,6 +630,26 @@ var _CPTextFieldSquareBezelColor = nil,
else if (CPFeatureIsCompatible(CPJavascriptTextContentFeature))
_DOMTextElement.textContent = displayString;
#endif
*/
}
/*!
Sets a placeholder string for the receiver. The placeholder is displayed until editing begins,
and after editing ends, if the text field has an empty string value
*/
-(void)setPlaceholderString:(CPString)aStringValue
{
if (_placeholderString === aStringValue)
return;
_placeholderString = aStringValue;
// Only update things if we need to show the placeholder
if ([self _shouldShowPlaceholderString])
{
[self setNeedsLayout];
[self setNeedsDisplay:YES];
}
}
/*!
@@ -703,24 +661,26 @@ var _CPTextFieldSquareBezelColor = nil,
}
/*!
Sets a placeholder string for the receiver. The placeholder is displayed until editing begins,
and after editing ends, if the text field has an empty string value
@ignore
*/
-(void)setPlaceholderString:(CPString)aStringValue
- (BOOL)_shouldShowPlaceholderString
{
_placeholderString = aStringValue;
//if there is no set value, automatically display the placeholder
if (!_value)
[self setStringValue:aStringValue];
var string = [self stringValue];
return (!string || [string length] === 0) && [_placeholderString length] > 0;
}
/*!
Adjusts the text field's size in the application.
*/
- (void)sizeToFit
{
#if PLATFORM(DOM)
var size = [(_value || " ") sizeWithFont:[self font]],
contentInset = [self isBezeled] ? [self currentValueForThemedAttributeName:@"content-inset"] : _CGInsetMakeZero();
[self setFrameSize:CGSizeMake(size.width + contentInset.left + contentInset.right, size.height + contentInset.top + contentInset.bottom)];
/*#if PLATFORM(DOM)
var size = [(_value || " ") sizeWithFont:[self font]];
if (_isBezeled && _bezelStyle == CPTextFieldRoundedBezel)
@@ -731,7 +691,7 @@ var _CPTextFieldSquareBezelColor = nil,
{
[self setFrameSize:CGSizeMake(size.width + 2 * HORIZONTAL_PADDING, size.height + TOP_PADDING + BOTTOM_PADDING)];
}
#endif
#endif*/
}
/*!
@@ -747,10 +707,184 @@ var _CPTextFieldSquareBezelColor = nil,
#endif
}
#pragma mark Setting the Delegate
- (void)setDelegate:(id)aDelegate
{
var defaultCenter = [CPNotificationCenter defaultCenter];
//unsubscribe the existing delegate if it exists
if (_delegate)
{
[defaultCenter removeObserver:_delegate name:CPControlTextDidBeginEditingNotification object:self];
[defaultCenter removeObserver:_delegate name:CPControlTextDidChangeNotification object:self];
[defaultCenter removeObserver:_delegate name:CPControlTextDidEndEditingNotification object:self];
}
_delegate = aDelegate;
if ([_delegate respondsToSelector:@selector(controlTextDidBeginEditing:)])
[defaultCenter
addObserver:_delegate
selector:@selector(controlTextDidBeginEditing:)
name:CPControlTextDidBeginEditingNotification
object:self];
if ([_delegate respondsToSelector:@selector(controlTextDidChange:)])
[defaultCenter
addObserver:_delegate
selector:@selector(controlTextDidChange:)
name:CPControlTextDidChangeNotification
object:self];
if ([_delegate respondsToSelector:@selector(controlTextDidEndEditing:)])
[defaultCenter
addObserver:_delegate
selector:@selector(controlTextDidEndEditing:)
name:CPControlTextDidEndEditingNotification
object:self];
}
- (id)delegate
{
return _delegate;
}
- (CGRect)contentRectForBounds:(CGRect)bounds
{
if (![self isBezeled])
return bounds;
var contentInset = [self currentValueForThemedAttributeName:@"content-inset"];
if (!contentInset)
return bounds;
bounds.origin.x += contentInset.left;
bounds.origin.y += contentInset.top;
bounds.size.width -= contentInset.left + contentInset.right;
bounds.size.height -= contentInset.top + contentInset.bottom;
return bounds;
}
- (CGRect)bezelRectForBounds:(CFRect)bounds
{
if (![self isBezeled])
return _CGRectMakeZero();
var bezelInset = [self currentValueForThemedAttributeName:@"bezel-inset"];
if (!_CGInsetIsEmpty(bezelInset))
return bounds;
bounds.origin.x += bezelInset.left;
bounds.origin.y += bezelInset.top;
bounds.size.width -= bezelInset.left + bezelInset.right;
bounds.size.height -= bezelInset.top + bezelInset.bottom;
return bounds;
}
- (CGRect)rectForEphemeralSubviewNamed:(CPString)aName
{
if (aName === "bezel-view")
return [self bezelRectForBounds:[self bounds]];
else if (aName === "content-view")
return [self contentRectForBounds:[self bounds]];
return [super rectForEphemeralSubviewNamed:aName];
}
- (CPView)createEphemeralSubviewNamed:(CPString)aName
{
if (aName === "bezel-view")
{
var view = [[CPView alloc] initWithFrame:_CGRectMakeZero()];
[view setHitTests:NO];
return view;
}
else
{
var view = [[_CPImageAndTextView alloc] initWithFrame:_CGRectMakeZero()];
//[view setImagePosition:CPNoImage];
return view;
}
return [super createEphemeralSubviewNamed:aName];
}
- (void)layoutSubviews
{
var bezelView = [self layoutEphemeralSubviewNamed:@"bezel-view"
positioned:CPWindowBelow
relativeToEphemeralSubviewNamed:@"content-view"];
if (bezelView)
[bezelView setBackgroundColor:[self currentValueForThemedAttributeName:@"bezel-color"]];
var contentView = [self layoutEphemeralSubviewNamed:@"content-view"
positioned:CPWindowAbove
relativeToEphemeralSubviewNamed:@"bezel-view"];
if (contentView)
{
if ([self _shouldShowPlaceholderString])
{
[contentView setText:[self placeholderString]];
[contentView setTextColor:[CPColor grayColor]];
}
else
{
[contentView setText:[self stringValue]];
[contentView setTextColor:[self currentValueForThemedAttributeName:@"text-color"]];
}
[contentView setFont:[self currentValueForThemedAttributeName:@"font"]];
[contentView setAlignment:[self currentValueForThemedAttributeName:@"alignment"]];
[contentView setVerticalAlignment:[self currentValueForThemedAttributeName:@"vertical-alignment"]];
[contentView setLineBreakMode:[self currentValueForThemedAttributeName:@"line-break-mode"]];
[contentView setTextShadowColor:[self currentValueForThemedAttributeName:@"text-shadow-color"]];
[contentView setTextShadowOffset:[self currentValueForThemedAttributeName:@"text-shadow-offset"]];
}
}
@end
/*
@implementation CPTextField (Theming)
- (void)viewDidChangeTheme
{
[super viewDidChangeTheme];
var theme = [self theme];
[_bezelInset setTheme:theme];
[_contentInset setTheme:theme];
[_bezelColor setTheme:theme];
}
- (CPDictionary)themedValues
{
var values = [super themedValues];
[values setObject:_bezelInset forKey:@"bezel-inset"];
[values setObject:_contentInset forKey:@"content-isnet"];
[values setObject:_bezelColor forKey:@"bezel-color"];
return values;
}
@end
*/
var CPTextFieldIsEditableKey = "CPTextFieldIsEditableKey",
CPTextFieldIsSelectableKey = "CPTextFieldIsSelectableKey",
CPTextFieldIsBorderedKey = "CPTextFieldIsBorderedKey",
@@ -807,7 +941,6 @@ var CPTextFieldIsEditableKey = "CPTextFieldIsEditableKey",
[self setBezelStyle:[aCoder decodeIntForKey:CPTextFieldBezelStyleKey]];
[self setDrawsBackground:[aCoder decodeBoolForKey:CPTextFieldDrawsBackgroundKey]];
[self setLineBreakMode:[aCoder decodeIntForKey:CPTextFieldLineBreakModeKey]];
[self setTextFieldBackgroundColor:[aCoder decodeObjectForKey:CPTextFieldBackgroundColorKey]];
[self setPlaceholderString:[aCoder decodeObjectForKey:CPTextFieldPlaceholderStringKey]];
@@ -832,7 +965,6 @@ var CPTextFieldIsEditableKey = "CPTextFieldIsEditableKey",
[aCoder encodeInt:_bezelStyle forKey:CPTextFieldBezelStyleKey];
[aCoder encodeBool:_drawsBackground forKey:CPTextFieldDrawsBackgroundKey];
[aCoder encodeInt:_lineBreakMode forKey:CPTextFieldLineBreakModeKey];
[aCoder encodeObject:_textFieldBackgroundColor forKey:CPTextFieldBackgroundColorKey];
[aCoder encodeObject:_placeholderString forKey:CPTextFieldPlaceholderStringKey];
-47
View File
@@ -1,47 +0,0 @@
#define THEMED_VALUE(VALUE, LOWERCASEVALUE) \
- (void)set##VALUE:(id)aValue\
{\
var currentValue = [_##LOWERCASEVALUE valueForControlState:_controlState];\
[_##LOWERCASEVALUE setValue:aValue];\
if ([_##LOWERCASEVALUE valueForControlState:_controlState] === currentValue)\
return;\
[self setNeedsDisplay:YES];\
[self setNeedsLayout];\
}\
- (id)LOWERCASEVALUE\
{\
return [_##LOWERCASEVALUE value];\
}\
#define THEMED_STATED_VALUE(VALUE, LOWERCASEVALUE) \
- (void)set##VALUE:(id)aValue forControlState:(CPControlState)aControlState\
{\
var currentValue = [_##LOWERCASEVALUE valueForControlState:_controlState];\
[_##LOWERCASEVALUE setValue:aValue forControlState:aControlState];\
if ([_##LOWERCASEVALUE valueForControlState:_controlState] === currentValue)\
return;\
[self setNeedsDisplay:YES];\
[self setNeedsLayout];\
}\
- (id)LOWERCASEVALUE##ForControlState:(CPControlState)aControlState\
{\
return [_##LOWERCASEVALUE valueForControlState:aControlState];\
}\
- (void)set##VALUE:(id)aValue\
{\
var currentValue = [_##LOWERCASEVALUE valueForControlState:_controlState];\
[_##LOWERCASEVALUE setValue:aValue];\
if ([_##LOWERCASEVALUE valueForControlState:_controlState] === currentValue)\
return;\
[self setNeedsDisplay:YES];\
[self setNeedsLayout];\
}\
- (id)LOWERCASEVALUE\
{\
return [_##LOWERCASEVALUE value];\
}\
- (id)current##VALUE\
{\
return [_##LOWERCASEVALUE valueForControlState:_controlState];\
}