diff --git a/AppKit/CPButton.j b/AppKit/CPButton.j index cb8cb6c95..4a2c48293 100644 --- a/AppKit/CPButton.j +++ b/AppKit/CPButton.j @@ -311,7 +311,7 @@ var _CPButtonClassName = nil, */ - (void)setButtonType:(CPButtonType)aButtonType { - if (aButtonType == CPSwitchButton) + if (aButtonType === CPSwitchButton) { [self setBordered:NO]; [self setImage:nil]; @@ -326,6 +326,10 @@ var _CPButtonClassName = nil, if (self) { + + + [self setVerticalAlignment:CPCenterVerticalTextAlignment]; + _imagePosition = CPImageLeft; _imageScaling = CPScaleNone; @@ -351,7 +355,7 @@ var _CPButtonClassName = nil, _imagePosition = anImagePosition; - [self drawContentsWithHighlight:_isHighlighted]; + [_imageAndTextView setImagePosition:_imagePosition]; } /*! @@ -373,7 +377,7 @@ var _CPButtonClassName = nil, _imageScaling = anImageScaling; - [self drawContentsWithHighlight:_isHighlighted]; + [_imageAndTextView setImageScaling:anImageScaling]; } /*! @@ -392,7 +396,7 @@ var _CPButtonClassName = nil, { [super setTextColor:aColor]; - [self drawContentsWithHighlight:_isHighlighted]; + [_imageAndTextView setTextColor:[self textColor]]; } /*! @@ -403,7 +407,7 @@ var _CPButtonClassName = nil, { [super setFont:aFont]; - [self drawContentsWithHighlight:_isHighlighted]; + [_imageAndTextView setFont:[self font]]; } // Setting the state @@ -461,7 +465,7 @@ var _CPButtonClassName = nil, { [super setAlignment:anAlignment]; - [self drawContentsWithHighlight:_isHighlighted]; + [_imageAndTextView setAlignment:[self alignment]]; } /*! @@ -470,12 +474,12 @@ var _CPButtonClassName = nil, */ - (void)setImage:(CPImage)anImage { - if (_image == anImage) + if (_image === anImage) return; _image = anImage; - [self drawContentsWithHighlight:_isHighlighted]; + [self setNeedsDisplay:YES]; } /*! @@ -514,7 +518,7 @@ var _CPButtonClassName = nil, _title = aTitle; - [self drawContentsWithHighlight:_isHighlighted]; + [self setNeedsDisplay:YES]; } /*! @@ -549,11 +553,46 @@ var _CPButtonClassName = nil, [_imageAndTextView setFrameSize:size]; } +- (CGRect)contentRectForBounds:(CGRect)aRect +{ + if (_isBordered) + { + if (_bezelStyle == CPHUDBezelStyle) + aRect.size.height -= 4.0; + else if (_bezelStyle == CPRoundRectBezelStyle) + aRect.size.height -= 2.0; + else if (_bezelStyle == CPTexturedRoundedBezelStyle) + aRect.size.height -= 2.0; + } + + return aRect; +} + +- (void)drawRect:(CGRect)aRect +{ + if (!_imageAndTextView) + { + _imageAndTextView = [[_CPImageAndTextView alloc] initWithFrame:[self contentRectForBounds:aRect] control:self]; + + [self addSubview:_imageAndTextView]; + } + else + [_imageAndTextView setFrame:[self contentRectForBounds:aRect]]; + + [_imageAndTextView setText:_isHighlighted && _alternateTitle ? _alternateTitle : _title]; + [_imageAndTextView setImage:_isHighlighted && _alternateImage ? _alternateImage : _image]; +} + /*! Compacts the button's frame to fit its contents. */ - (void)sizeToFit -{ +{ if (!_imageAndTextView) + { + _imageAndTextView = [[_CPImageAndTextView alloc] initWithFrame:[self contentRectForBounds:[self bounds]] control:self]; + + [self addSubview:_imageAndTextView]; + } [_imageAndTextView sizeToFit]; var frame = [_imageAndTextView frame], @@ -570,17 +609,6 @@ var _CPButtonClassName = nil, [self setFrameSize:CGSizeMake(CGRectGetWidth(frame), height)]; } -/*! - Sets the frame size for the button. - @param the new frame size -*/ -- (void)setFrameSize:(CGSize)aSize -{ - [super setFrameSize:aSize]; - - [self tile]; -} - /*! Highlights the receiver based on aFlag. @param If YES the button will highlight, NO the button will unhighlight. @@ -590,7 +618,7 @@ var _CPButtonClassName = nil, _isHighlighted = aFlag; [self drawBezelWithHighlight:aFlag]; - [self drawContentsWithHighlight:aFlag]; + [self setNeedsDisplay:YES]; } /*! @@ -752,32 +780,6 @@ var _CPButtonClassName = nil, [self setBackgroundColorWithName:shouldHighlight ? CPControlHighlightedBackgroundColor : CPControlNormalBackgroundColor]; } -/* @ignore */ -- (void)drawContentsWithHighlight:(BOOL)isHighlighted -{ - if (!_title && !_image && !_alternateTitle && !_alternateImage && !_imageAndTextView) - return; - - if (!_imageAndTextView) - { - _imageAndTextView = [[_CPImageAndTextView alloc] initWithFrame:[self bounds]]; - - [self addSubview:_imageAndTextView]; - - [self tile]; - } - - [_imageAndTextView setVerticalAlignment:CPCenterVerticalTextAlignment]; - [_imageAndTextView setFont:[self font]]; - [_imageAndTextView setTextColor:[self textColor]]; - [_imageAndTextView setAlignment:[self alignment]]; - [_imageAndTextView setImagePosition:_imagePosition]; - [_imageAndTextView setImageScaling:_imageScaling]; - - [_imageAndTextView setText:isHighlighted && _alternateTitle ? _alternateTitle : _title]; - [_imageAndTextView setImage:isHighlighted && _alternateImage ? _alternateImage : _image]; -} - - (void)viewDidMoveToWindow { if (_bezelBorderNeedsUpdate) diff --git a/AppKit/CPControl.j b/AppKit/CPControl.j index 33469a577..15ef1b0a9 100644 --- a/AppKit/CPControl.j +++ b/AppKit/CPControl.j @@ -91,10 +91,13 @@ var CPControlBlackColor = [CPColor blackColor]; BOOL _isEnabled; int _alignment; + CPVerticalTextAlignment _verticalAlignment; CPFont _font; CPColor _textColor; CPShadow _textShadow; + CPLineBreakMode _lineBreakMode; + id _target; SEL _action; int _sendActionOn; @@ -114,6 +117,8 @@ var CPControlBlackColor = [CPColor blackColor]; if (self) { + [self setVerticalAlignment:CPTopVerticalTextAlignment]; + _sendActionOn = CPLeftMouseUpMask; _trackingMouseDownFlags = 0; @@ -169,6 +174,15 @@ 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 */ @@ -177,13 +191,24 @@ var CPControlBlackColor = [CPColor blackColor]; return _alignment; } -/*! - Sets the receiver's alignment - @param anAlignment the receiver's alignment -*/ -- (void)setAlignment:(CPTextAlignment)anAlignment +- (void)setVerticalAlignment:(CPVerticalTextAlignment)anAlignment { - _alignment = anAlignment; + _verticalAlignment = anAlignment; +} + +- (CPVerticalTextAlignment)verticalAlignment +{ + return _verticalAlignment; +} + +- (void)setLineBreakMode:(CPLineBreakMode)aLineBreakMode +{ + _lineBreakMode = aLineBreakMode; +} + +- (CPLineBreakMode)lineBreakMode +{ + return _lineBreakMode; } /*! diff --git a/AppKit/CPPopUpButton.j b/AppKit/CPPopUpButton.j index 4c7f21503..dcdda109e 100644 --- a/AppKit/CPPopUpButton.j +++ b/AppKit/CPPopUpButton.j @@ -65,6 +65,7 @@ var CPPopUpButtonArrowsImage = nil; [self setImagePosition:CPImageLeft]; [self setAlignment:CPLeftTextAlignment]; + [self setLineBreakMode:CPLineBreakByTruncatingTail]; [self setMenu:[[CPMenu alloc] initWithTitle:@""]]; } @@ -698,6 +699,11 @@ var CPPopUpButtonArrowsImage = nil; [self sendAction:action to:target]; } +- (CGRect)contentRectForBounds:(CGRect)bounds +{ + return CGRectMake(3.0, 2.0, CGRectGetWidth(bounds) - 16.0, CGRectGetHeight(bounds) - 5.0); +} + @end var CPPopUpButtonMenuKey = @"CPPopUpButtonMenuKey", diff --git a/AppKit/_CPImageAndTextView.j b/AppKit/_CPImageAndTextView.j index 2d8318a0d..69131c386 100644 --- a/AppKit/_CPImageAndTextView.j +++ b/AppKit/_CPImageAndTextView.j @@ -73,27 +73,39 @@ var HORIZONTAL_MARGIN = 3.0, unsigned _flags; #if PLATFORM(DOM) - DOMElement _DOMImageElement; - DOMELement _DOMTextElement; + DOMElement _DOMImageElement; + DOMELement _DOMTextElement; #endif } -- (id)initWithFrame:(CGRect)aFrame +- (id)initWithFrame:(CGRect)aFrame control:(CPControl)aControl { self = [super initWithFrame:aFrame]; if (self) { - _lineBreakMode = CPLineBreakByClipping; - _textColor = nil; - - [self setAlignment:CPCenterTextAlignment]; + // ? [self setVerticalAlignment:CPTopVerticalTextAlignment]; - [self setFont:[CPFont systemFontOfSize:12.0]]; - [self setImagePosition:CPNoImage]; - [self setImageScaling:CPScaleNone]; - _textColor = nil; + if (aControl) + { + [self setLineBreakMode:[aControl lineBreakMode]]; + [self setTextColor:[aControl textColor]]; + [self setAlignment:[aControl alignment]]; + [self setVerticalAlignment:[aControl verticalAlignment]]; + [self setFont:[aControl font]]; + [self setImagePosition:[aControl imagePosition]]; + [self setImageScaling:[aControl imageScaling]]; + } + else + { + [self setLineBreakMode:CPLineBreakByClipping]; + //[self setTextColor:[aControl textColor]]; + [self setAlignment:CPCenterTextAlignment]; + [self setFont:[CPFont systemFontOfSize:12.0]]; + [self setImagePosition:CPNoImage]; + [self setImageScaling:CPScaleNone]; + } _textSize = NULL; } @@ -101,6 +113,11 @@ var HORIZONTAL_MARGIN = 3.0, return self; } +- (id)initWithFrame:(CGRect)aFrame +{ + return [self initWithFrame:aFrame control:nil]; +} + - (void)setAlignment:(CPTextAlignment)anAlignment { if (_alignment === anAlignment) @@ -489,37 +506,33 @@ var HORIZONTAL_MARGIN = 3.0, textStyle.left = textRectX + "px"; textStyle.width = textRectWidth + "px"; - switch (_verticalAlignment) + if (_verticalAlignment === CPTopVerticalTextAlignment) { - case CPTopVerticalTextAlignment: textStyle.top = "0px"; - textStyle.height = textRectHeight + "px"; - - break; - - case CPCenterVerticalTextAlignment: if (0 && _lineBreakMode !== CPLineBreakByCharWrapping && _lineBreakMode !== CPLineBreakByWordWrapping) - { - textStyle.lineHeight = textRectHeight + "px"; - textStyle.height = textRectHeight + "px"; - } - - else - { - if (!_textSize) - _textSize = [_text sizeWithFont:_font inWidth:textRectWidth]; - - textStyle.top = (textRectY + (textRectHeight - _textSize.height) / 2.0) + "px"; - textStyle.height = _textSize.height + "px"; - } - - break; - - case CPBottomVerticalTextAlignment: if (!_textSize) - _textSize = [_text sizeWithFont:_font inWidth:textRectWidth]; - - textStyle.top = (textRectY + textRectHeight - _textSize.height) + "px"; - textStyle.height = _textSize.height + "px"; - - break; + textStyle.top = "0px"; + textStyle.height = textRectHeight + "px"; + } + else + { + if (!_textSize) + { + if (_lineBreakMode === CPLineBreakByCharWrapping || + _lineBreakMode === CPLineBreakByWordWrapping) + _textSize = [_text sizeWithFont:_font inWidth:textRectWidth]; + else + _textSize = [_text sizeWithFont:_font]; + } + + if (_verticalAlignment === CPCenterVerticalTextAlignment) + { + textStyle.top = (textRectY + (textRectHeight - _textSize.height) / 2.0) + "px"; + textStyle.height = _textSize.height + "px"; + } + + else //if (_verticalAlignment === CPBottomVerticalTextAlignment) + { + textStyle.top = (textRectY + textRectHeight - _textSize.height) + "px"; + textStyle.height = _textSize.height + "px"; + } } } #endif