Re: Custom Cell not working in NSTableView
Re: Custom Cell not working in NSTableView
- Subject: Re: Custom Cell not working in NSTableView
- From: Todd Freese <email@hidden>
- Date: Wed, 16 Aug 2006 20:06:20 -0500
Sure, here is the code for the custom cell.
@implementation SourceListTextCell
- (void)dealloc {
[image release];
image = nil;
[super dealloc];
}
- (SourceListTextCell *)copyWithZone:(NSZone *)zone {
SourceListTextCell *cell = (SourceListTextCell *)[super
copyWithZone:zone];
cell->image = [image retain];
return cell;
}
- (void)setImage:(NSImage *)anImage {
if (anImage != image) {
[image release];
image = [anImage retain];
}
}
- (NSImage *)image {
return image;
}
- (NSRect)imageFrameForCellFrame:(NSRect)cellFrame {
if (image != nil) {
NSRect imageFrame;
imageFrame.size = [image size];
imageFrame.origin = cellFrame.origin;
imageFrame.origin.x += 3;
imageFrame.origin.y += ceil((cellFrame.size.height -
imageFrame.size.height) / 2);
return imageFrame;
}
else
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:(int)selStart
length:(int)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];
}
- (NSSize)cellSize
{
NSSize cellSize = [super cellSize];
cellSize.width += (image ? [image size].width : 0) + 3;
return cellSize;
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)
controlView
{
/* Determine whether we should draw a blue or grey gradient. */
/* We will automatically redraw when our parent view loses/gains
focus,
or when our parent window loses/gains main/key status. */
NSImage *gradient;
if (([[controlView window] firstResponder] == controlView) &&
[[controlView window] isMainWindow] &&
[[controlView window] isKeyWindow]) {
gradient = [NSImage imageNamed:@"highlight_blue.tiff"];
} else {
gradient = [NSImage imageNamed:@"highlight_grey.tiff"];
}
/* Make sure we draw the gradient the correct way up. */
[gradient setFlipped:YES];
int i = 0;
if ([self isHighlighted]) {
[controlView lockFocus];
/* We're selected, so draw the gradient background. */
NSSize gradientSize = [gradient size];
for (i = cellFrame.origin.x; i < (cellFrame.origin.x +
cellFrame.size.width); i += gradientSize.width) {
[gradient drawInRect:NSMakeRect(i, cellFrame.origin.y,
gradientSize.width, cellFrame.size.height)
fromRect:NSMakeRect(0, 0, gradientSize.width, gradientSize.height)
operation:NSCompositeSourceOver
fraction:1.0];
}
/* Now draw our image. */
if (image != nil) {
NSSize imageSize;
NSRect imageFrame;
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;
if ([controlView isFlipped])
imageFrame.origin.y += ceil((cellFrame.size.height +
imageFrame.size.height) / 2);
else
imageFrame.origin.y += ceil((cellFrame.size.height -
imageFrame.size.height) / 2);
[image compositeToPoint:imageFrame.origin
operation:NSCompositeSourceOver];
}
/* Now draw our text in white. */
NSRect inset = cellFrame;
inset.origin.x += 2; // Nasty to hard-code this. Can we get
it to draw its own content, or determine correct inset?
NSMutableDictionary *attrs = [NSMutableDictionary
dictionaryWithDictionary:[[self attributedStringValue]
attributesAtIndex:0 effectiveRange:NULL]];
[attrs setValue:[NSColor whiteColor] forKey:@"NSColor"];
[[self stringValue] drawInRect:inset withAttributes:attrs];
[controlView unlockFocus];
} else {
/* Draw our image if not highlighted. */
if (image != nil) {
NSSize imageSize;
NSRect imageFrame;
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;
if ([controlView isFlipped])
imageFrame.origin.y += ceil((cellFrame.size.height +
imageFrame.size.height) / 2);
else
imageFrame.origin.y += ceil((cellFrame.size.height -
imageFrame.size.height) / 2);
[image compositeToPoint:imageFrame.origin
operation:NSCompositeSourceOver];
}
/* We're not selected, so ask our superclass to draw our
content normally. */
[super drawInteriorWithFrame:cellFrame inView:controlView];
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden