Added support for cross-browser text shadows in _CPImageAndTextView.

Reviewed by me.
This commit is contained in:
Francisco Ryan Tolmasky I committed 2009-01-27 15:41:32 -08:00
1 parent 9e7fd7f74f
commit 10b34965bd
4 files changed
+268 -114

No files matched your search

+7 -33
View File
@@ -326,19 +326,15 @@ var _CPButtonClassName = nil,
if (self)
{
[self setAlignment:CPCenterTextAlignment];
[self setVerticalAlignment:CPCenterVerticalTextAlignment];
_imagePosition = CPImageLeft;
_imageScaling = CPScaleNone;
[self setImagePosition:CPImageLeft];
[self setImageScaling:CPScaleNone];
_controlSize = CPRegularControlSize;
[self setBezelStyle:CPRoundRectBezelStyle];
[self setBordered:YES];
[self setAlignment:CPCenterTextAlignment];
}
return self;
@@ -350,20 +346,9 @@ var _CPButtonClassName = nil,
*/
- (void)setImagePosition:(CPCellImagePosition)anImagePosition
{
if (_imagePosition == anImagePosition)
return;
[super setImagePosition:anImagePosition];
_imagePosition = anImagePosition;
[_imageAndTextView setImagePosition:_imagePosition];
}
/*!
Returns the buton's image position
*/
- (CPCellImagePosition)imagePosition
{
return _imagePosition;
[_imageAndTextView setImagePosition:[self imagePosition]];
}
/*!
@@ -372,20 +357,9 @@ var _CPButtonClassName = nil,
*/
- (void)setImageScaling:(CPImageScaling)anImageScaling
{
if (_imageScaling == anImageScaling)
return;
_imageScaling = anImageScaling;
[_imageAndTextView setImageScaling:anImageScaling];
}
[super setImageScaling:anImageScaling];
/*!
Returns the button's image scaling method
*/
- (CPImageScaling)imageScaling
{
return _imageScaling;
[_imageAndTextView setImageScaling:[self imageScaling]];
}
/*!
+114 -54
View File
@@ -86,29 +86,36 @@ var CPControlBlackColor = [CPColor blackColor];
*/
@implementation CPControl : CPView
{
id _value;
BOOL _isEnabled;
id _value;
BOOL _isEnabled;
int _alignment;
// Display Properties
CPTextAlignment _alignment;
CPVerticalTextAlignment _verticalAlignment;
CPFont _font;
CPColor _textColor;
CPLineBreakMode _lineBreakMode;
CPColor _textColor;
CPFont _font;
CPCellImagePosition _imagePosition;
CPImageScaling _imageScaling;
// Target-Action Support
id _target;
SEL _action;
int _sendActionOn;
// Mouse Tracking Support
BOOL _continuousTracking;
BOOL _trackingWasWithinFrame;
unsigned _trackingMouseDownFlags;
CGPoint _previousTrackingLocation;
// Stuff
CPShadow _textShadow;
CPLineBreakMode _lineBreakMode;
id _target;
SEL _action;
int _sendActionOn;
CPDictionary _backgroundColors;
CPString _currentBackgroundColorName;
BOOL _continuousTracking;
BOOL _trackingWasWithinFrame;
unsigned _trackingMouseDownFlags;
CGPoint _previousTrackingLocation;
}
- (id)initWithFrame:(CGRect)aFrame
@@ -150,6 +157,56 @@ var CPControlBlackColor = [CPColor blackColor];
return _isEnabled;
}
/*!
Sets the receiver's horizontal text alignment
@param anAlignment the receiver's alignment
*/
- (void)setAlignment:(CPTextAlignment)anAlignment
{
_alignment = anAlignment;
}
/*!
Returns the receiver's horizontal text alignment
*/
- (CPTextAlignment)alignment
{
return _alignment;
}
/*!
Sets the receiver's vertical text alignment
@param anAlignment the receiver's alignment
*/
- (void)setVerticalAlignment:(CPVerticalTextAlignment)anAlignment
{
_verticalAlignment = anAlignment;
}
/*!
Returns the receiver's vertical text alignment
*/
- (CPVerticalTextAlignment)verticalAlignment
{
return _verticalAlignment;
}
/*!
Sets the receiver's line break mode.
@param anAlignment the receiver's line break mode.
*/
- (void)setLineBreakMode:(CPLineBreakMode)aLineBreakMode
{
_lineBreakMode = aLineBreakMode;
}
/*!
Returns the receiver's line break mode.
*/
- (CPLineBreakMode)lineBreakMode
{
return _lineBreakMode;
}
/*!
Sets the color of the receiver's text.
@@ -174,43 +231,6 @@ var CPControlBlackColor = [CPColor blackColor];
return _textColor;
}
/*!
Sets the receiver's alignment
@param anAlignment the receiver's alignment
*/
- (void)setAlignment:(CPTextAlignment)anAlignment
{
_alignment = anAlignment;
}
/*!
Returns the receiver's alignment
*/
- (CPTextAlignment)alignment
{
return _alignment;
}
- (void)setVerticalAlignment:(CPVerticalTextAlignment)anAlignment
{
_verticalAlignment = anAlignment;
}
- (CPVerticalTextAlignment)verticalAlignment
{
return _verticalAlignment;
}
- (void)setLineBreakMode:(CPLineBreakMode)aLineBreakMode
{
_lineBreakMode = aLineBreakMode;
}
- (CPLineBreakMode)lineBreakMode
{
return _lineBreakMode;
}
/*!
Sets the receiver's font
@param aFont the font for the receiver
@@ -235,6 +255,46 @@ var CPControlBlackColor = [CPColor blackColor];
return _font;
}
/*!
Sets the position of the button's image to <code>anImagePosition</code>.
@param anImagePosition the position for the button's image
*/
- (void)setImagePosition:(CPCellImagePosition)anImagePosition
{
if (_imagePosition === anImagePosition)
return;
_imagePosition = anImagePosition;
}
/*!
Returns the buton's image position
*/
- (CPCellImagePosition)imagePosition
{
return _imagePosition;
}
/*!
Sets the button's images scaling method
@param anImageScaling the image scaling method
*/
- (void)setImageScaling:(CPImageScaling)anImageScaling
{
if (_imageScaling === anImageScaling)
return;
_imageScaling = anImageScaling;
}
/*!
Returns the button's image scaling method
*/
- (CPImageScaling)imageScaling
{
return _imageScaling;
}
/*!
Sets the shadow for the receiver's text.
@param aTextShadow the text shadow
+15 -3
View File
@@ -1009,6 +1009,7 @@ var _CPMenuItemSelectionColor = nil,
_imageAndTextView = [[_CPImageAndTextView alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, 0.0)];
[_imageAndTextView setImagePosition:CPImageLeft];
[_imageAndTextView setTextShadowOffset:CGSizeMake(0.0, 1.0)];
[self addSubview:_imageAndTextView];
}
@@ -1029,6 +1030,9 @@ var _CPMenuItemSelectionColor = nil,
var frame = [_imageAndTextView frame];
frame.size.height += 1.0;
[_imageAndTextView setFrame:frame];
frame.size.height += 2 * VERTICAL_MARGIN;
x += CGRectGetWidth(frame);
@@ -1084,14 +1088,22 @@ var _CPMenuItemSelectionColor = nil,
if (_belongsToMenuBar)
[_imageAndTextView setImage:shouldHighlight ? [_menuItem alternateImage] : [_menuItem image]];
else if([_menuItem isEnabled])
else if ([_menuItem isEnabled])
{
[_imageAndTextView setTextColor:shouldHighlight ? [CPColor whiteColor] : [self textColor]];
if (shouldHighlight)
{
[self setBackgroundColor:_CPMenuItemSelectionColor];
[_imageAndTextView setTextColor:[CPColor whiteColor]];
[_imageAndTextView setTextShadowColor:[CPColor blackColor]];
}
else
{
[self setBackgroundColor:nil];
[_imageAndTextView setTextColor:[self textColor]];
[_imageAndTextView setTextShadowColor:nil];
}
var state = [_menuItem state];
+132 -24
View File
@@ -46,8 +46,9 @@ var _CPimageAndTextViewFrameSizeChangedFlag = 1 << 0,
_CPImageAndTextViewLineBreakModeChangedFlag = 1 << 5,
_CPImageAndTextViewTextColorChangedFlag = 1 << 6,
_CPImageAndTextViewFontChangedFlag = 1 << 7,
_CPImageAndTextViewImagePositionChangedFlag = 1 << 8,
_CPImageAndTextViewImageScalingChangedFlag = 1 << 9;
_CPImageAndTextViewTextShadowColorChangedFlag = 1 << 8,
_CPImageAndTextViewImagePositionChangedFlag = 1 << 9,
_CPImageAndTextViewImageScalingChangedFlag = 1 << 10;
var HORIZONTAL_MARGIN = 3.0,
VERTICAL_MARGIN = 5.0;
@@ -62,6 +63,9 @@ var HORIZONTAL_MARGIN = 3.0,
CPColor _textColor;
CPFont _font;
CPColor _textShadowColor;
CGSize _textShadowOffset;
CPCellImagePosition _imagePosition;
CPImageScaling _imageScaling;
@@ -75,6 +79,7 @@ var HORIZONTAL_MARGIN = 3.0,
#if PLATFORM(DOM)
DOMElement _DOMImageElement;
DOMELement _DOMTextElement;
DOMElement _DOMTextShadowElement;
#endif
}
@@ -84,7 +89,7 @@ var HORIZONTAL_MARGIN = 3.0,
if (self)
{
// ?
_textShadowOffset = _CGSizeMakeZero();
[self setVerticalAlignment:CPTopVerticalTextAlignment];
if (aControl)
@@ -245,6 +250,37 @@ var HORIZONTAL_MARGIN = 3.0,
return _font;
}
- (void)setTextShadowColor:(CPColor)aColor
{
if (_textShadowColor === aColor)
return;
_textShadowColor = aColor;
_flags |= _CPImageAndTextViewTextShadowColorChangedFlag;
[self setNeedsDisplay:YES];
}
- (CPColor)textShadowColor
{
return _textShadowColor;
}
- (void)setTextShadowOffset:(CGSize)anOffset
{
if (_CGSizeEqualToSize(_textShadowOffset, anOffset))
return;
_textShadowOffset = _CGSizeMakeCopy(anOffset);
[self setNeedsDisplay:YES];
}
- (CGSize)textShadowOffset
{
return _textShadowOffset;
}
- (void)setImage:(CPImage)anImage
{
if (_image == anImage)
@@ -315,23 +351,76 @@ var HORIZONTAL_MARGIN = 3.0,
// We have to set all these values now.
_flags |= _CPImageAndTextViewTextChangedFlag | _CPImageAndTextViewFontChangedFlag | _CPImageAndTextViewLineBreakModeChangedFlag;
}
var textStyle = hasDOMTextElement ? _DOMTextElement.style : nil;
// Create or destroy the DOM Text Shadow element as necessary.
var needsDOMTextShadowElement = hasDOMTextElement && !!_textShadowColor,
hasDOMTextShadowElement = !!_DOMTextShadowElement;
if (needsDOMTextShadowElement !== hasDOMTextShadowElement)
if (hasDOMTextShadowElement)
{
_DOMElement.removeChild(_DOMTextShadowElement);
_DOMTextShadowElement = NULL;
hasDOMTextShadowElement = NO;
}
else
{
_DOMTextShadowElement = document.createElement("div");
var shadowStyle = _DOMTextShadowElement.style;
shadowStyle.font = textStyle.font;
shadowStyle.position = "absolute";
shadowStyle.whiteSpace = textStyle.whiteSpace;
shadowStyle.cursor = "default";
shadowStyle.zIndex = -100;
shadowStyle.overflowX = textStyle.overflowX;
shadowStyle.overflowY = textStyle.overflowY;
shadowStyle.textOverflow = textStyle.textOverflow;
if (document.attachEvent)
shadowStyle.wordWrap = textStyle.wordWrap;
_DOMElement.appendChild(_DOMTextShadowElement);
hasDOMTextShadowElement = YES;
_flags |= _CPImageAndTextViewTextChangedFlag; //sigh...
}
var shadowStyle = hasDOMTextShadowElement ? _DOMTextShadowElement.style : nil;
if (hasDOMTextElement)
{
if (!textStyle)
var textStyle = _DOMTextElement.style;
// Update the text contents if necessary.
if (_flags & _CPImageAndTextViewTextChangedFlag)
if (CPFeatureIsCompatible(CPJavascriptInnerTextFeature))
{
_DOMTextElement.innerText = _text;
if (_DOMTextShadowElement)
_DOMTextShadowElement.innerText = _text;
}
else if (CPFeatureIsCompatible(CPJavascriptTextContentFeature))
{
_DOMTextElement.textContent = _text;
if (_DOMTextShadowElement)
_DOMTextShadowElement.textContent = _text;
}
if (_flags & _CPImageAndTextViewFontChangedFlag)
{
textStyle.font = [_font ? _font : [CPFont systemFontOfSize:12.0] cssString];
if (shadowStyle)
shadowStyle.font = textStyle.font;
}
// Update the line break mode if necessary.
if (_flags & _CPImageAndTextViewLineBreakModeChangedFlag)
{
@@ -378,6 +467,17 @@ var HORIZONTAL_MARGIN = 3.0,
break;
}
if (shadowStyle)
{
if (document.attachEvent)
shadowStyle.wordWrap = textStyle.wordWrap;
shadowStyle.whiteSpace = textStyle.whiteSpace;
shadowStyle.overflowX = textStyle.overflowX;
shadowStyle.overflowY = textStyle.overflowY;
shadowStyle.textOverflow = textStyle.textOverflow;
}
}
}
@@ -498,20 +598,12 @@ var HORIZONTAL_MARGIN = 3.0,
#if PLATFORM(DOM)
if (hasDOMTextElement)
{
var textRectX = FLOOR(_CGRectGetMinX(textRect)),
textRectY = FLOOR(_CGRectGetMinY(textRect)),
textRectWidth = FLOOR(_CGRectGetWidth(textRect)),
textRectHeight = FLOOR(_CGRectGetHeight(textRect));
textStyle.left = textRectX + "px";
textStyle.width = textRectWidth + "px";
var textRectX = _CGRectGetMinX(textRect),
textRectY = _CGRectGetMinY(textRect),
textRectWidth = _CGRectGetWidth(textRect),
textRectHeight = _CGRectGetHeight(textRect);
if (_verticalAlignment === CPTopVerticalTextAlignment)
{
textStyle.top = "0px";
textStyle.height = textRectHeight + "px";
}
else
if (_verticalAlignment !== CPTopVerticalTextAlignment)
{
if (!_textSize)
{
@@ -524,16 +616,32 @@ var HORIZONTAL_MARGIN = 3.0,
if (_verticalAlignment === CPCenterVerticalTextAlignment)
{
textStyle.top = (textRectY + (textRectHeight - _textSize.height) / 2.0) + "px";
textStyle.height = _textSize.height + "px";
textRectY = textRectY + (textRectHeight - _textSize.height) / 2.0;
textRectHeight = _textSize.height;
}
else //if (_verticalAlignment === CPBottomVerticalTextAlignment)
{
textStyle.top = (textRectY + textRectHeight - _textSize.height) + "px";
textStyle.height = _textSize.height + "px";
textRectY = textRectY + textRectHeight - _textSize.height;
textRectHeight = _textSize.height;
}
}
textStyle.top = ROUND(textRectY) + "px";
textStyle.left = ROUND(textRectX) + "px";
textStyle.width = ROUND(textRectWidth) + "px";
textStyle.height = ROUND(textRectHeight) + "px";
if (shadowStyle)
{
if (_flags & _CPImageAndTextViewTextShadowColorChangedFlag)
shadowStyle.color = [_textShadowColor cssString];
shadowStyle.top = ROUND(textRectY + _textShadowOffset.height) + "px";
shadowStyle.left = ROUND(textRectX + _textShadowOffset.width) + "px";
shadowStyle.width = ROUND(textRectWidth) + "px";
shadowStyle.height = ROUND(textRectHeight) + "px";
}
}
#endif