/*
* $Id: STImageTextFieldCell.m 224 2011-08-13 20:39:45Z stuart $
*
* SafariTabs
* http://stuconnolly.com/projects/safaritabs/
*
* Copyright (c) 2010 Stuart Connolly. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import "STImageTextFieldCell.h"
@implementation STImageTextFieldCell
- (id)initWithImage
:(NSImage *)image
{
if ((self = [super init])) {
_image = image;
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
STImageTextFieldCell *cell = (STImageTextFieldCell *)[super copyWithZone:zone];
cell->_image = nil;
cell->_image = [_image retain];
return cell;
}
{
return _image;
}
{
if (_image != image) {
[_image release];
_image = [image retain];
}
}
- (NSSize)cellSize
{
NSSize cellSize = [super cellSize];
cellSize.width += (_image ? [_image size].width : 0) + 3;
return cellSize;
}
- (NSRect)imageFrameForCellFrame:(NSRect)cellFrame
{
if (_image != nil) {
NSRect imageFrame;
imageFrame.size = [_image size];
imageFrame.origin = cellFrame.origin;
imageFrame.origin.x += 3;
imageFrame.origin.y
+= (CGFloat
)ceil((cellFrame.size.height
- imageFrame.size.height
) / 2);
return imageFrame;
}
return NSZeroRect;
}
- (void)editWithFrame
:(NSRect)aRect inView
:(NSView *)controlView editor
:(NSText *)textObj delegate
:(id)anObject event
:(NSEvent *)theEvent
{
NSRect textFrame, imageFrame;
NSDivideRect(aRect, &imageFrame, &textFrame, 3 + [_image size].width, NSMinXEdge);
[super editWithFrame:textFrame inView:controlView editor:textObj delegate:anObject event:theEvent];
}
- (void)selectWithFrame
:(NSRect)aRect inView
:(NSView *)controlView editor
:(NSText *)textObj delegate
:(id)anObject start
:(NSInteger
)selStart length
:(NSInteger
)selLength
{
NSRect textFrame, imageFrame;
NSDivideRect(aRect, &imageFrame, &textFrame, 3 + [_image size].width, NSMinXEdge);
[super selectWithFrame:textFrame inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
}
- (void)drawWithFrame
:(NSRect)cellFrame inView
:(NSView *)controlView
{
if (_image != nil) {
NSRect imageFrame;
NSSize imageSize = [_image size];
NSDivideRect(cellFrame, &imageFrame, &cellFrame, 3 + imageSize.width, NSMinXEdge);
if ([self drawsBackground]) {
[[self backgroundColor] set];
NSRectFill(imageFrame);
}
imageFrame.origin.x += 3;
imageFrame.size = imageSize;
imageFrame.origin.y
+= ([controlView isFlipped
]) ?
(CGFloat
)ceil((cellFrame.size.height
+ imageFrame.size.height
) / 2) : (CGFloat
)ceil((cellFrame.size.height
- imageFrame.size.height
) / 2);
[_image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
}
[super drawWithFrame:cellFrame inView:controlView];
}
- (void)dealloc
{
[_image release], _image = nil;
[super dealloc];
}
@end