// // JSRunSideBarCell.m // JSRun // // Created by Jonathan Saggau on 2/26/07. // Copyright 2007 Jonathan Saggau. All rights reserved. // #import "JSRunSideBarCell.h" #import "JSCommonMacros.h" #define IMAGE_BORDER 4 #define BOTTOM_BAR_THICKNESS 2 #define BOTTOM_BAR_BORDER 3 #define BOTTOM_BAR_HEIGHT ((BOTTOM_BAR_BORDER*2)+BOTTOM_BAR_THICKNESS) #define BOTTOM_BAR_INSET 10 @implementation JSRunSideBarCell - (id)initTextCell:(NSString *)text { self = [super initTextCell:text]; if (self != nil) { hasBottomBar = NO; selectImage = [[NSImage imageNamed:@"sidebar-select.tif"] retain]; [selectImage setFlipped:YES]; } return self; } - (NSSize)cellSize { // printf("cellSize called\n"); NSSize cellSize = NSMakeSize(0, 0); if ([self image] == nil) { // printf("No image\n"); } else { // printf("Image address: %d\n", [self image]); } if ([[self image] isValid]) { // printf("Valid image\n"); } else { // printf("Not a valid image\n"); } cellSize.height = [[self image] size].height + (2*IMAGE_BORDER); NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:2]; [dic setObject:[self font] forKey:NSFontAttributeName]; cellSize.width = [[self image] size].width + (2*IMAGE_BORDER); cellSize.width += IMAGE_BORDER + [[self stringValue] sizeWithAttributes:dic].width; if (hasBottomBar) { cellSize.height += BOTTOM_BAR_HEIGHT; } return cellSize; } - (NSColor *)textColor { if ([self isHighlighted]) { return [NSColor colorWithCalibratedRed:1 green:1 blue:1 alpha:1.0]; } else { return [NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:1.0]; } } - (NSFont *)font { if ([self isHighlighted]) { return [NSFont fontWithName:@"Trebuchet MS" size:15]; } else { return [NSFont fontWithName:@"Trebuchet MS" size:14]; } } - (BOOL)drawsBackground { return NO; } - (BOOL)isEditable { return NO; } - (BOOL)hasBottomBar { return hasBottomBar; } - (void)setHasBottomBar:(BOOL)bar { hasBottomBar = bar; } - (void)drawImageAndTextWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { NSImage *myImage = [self image]; [myImage setFlipped:YES]; NSRect imageRect = NSMakeRect(0, 0, [myImage size].width, [myImage size].height); NSPoint imagePoint = NSMakePoint(5 + cellFrame.origin.x, 5 + cellFrame.origin.y); [myImage drawAtPoint:imagePoint fromRect:imageRect operation:NSCompositeSourceOver fraction:1.0]; NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:10]; [dic setObject:[self font] forKey:NSFontAttributeName]; [dic setObject:[self textColor] forKey:NSForegroundColorAttributeName]; NSSize textSize = [[self stringValue] sizeWithAttributes:dic]; NSPoint textPoint = NSMakePoint(cellFrame.origin.x + [myImage size].width + (2*IMAGE_BORDER), cellFrame.origin.y + IMAGE_BORDER + ([myImage size].height / 2) - (textSize.height / 2)); [[self stringValue] drawAtPoint:textPoint withAttributes:dic]; } - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { if (hasBottomBar) { NSRect actualRect = cellFrame; actualRect.size.height -= BOTTOM_BAR_HEIGHT; [self drawImageAndTextWithFrame:actualRect inView:controlView]; NSRect barRect = NSMakeRect(actualRect.origin.x + BOTTOM_BAR_INSET, actualRect.origin.y + actualRect.size.height + BOTTOM_BAR_BORDER, actualRect.size.width - (2*BOTTOM_BAR_INSET), BOTTOM_BAR_THICKNESS); [[NSColor colorWithCalibratedRed:245.0/255 green:127.0/255 blue:141.0/255 alpha:1.0] set]; NSRectFill(barRect); } else { [self drawImageAndTextWithFrame:cellFrame inView:controlView]; } } - (void)highlight:(BOOL)flag withFrame:(NSRect)cellFrame inView:(NSView *)controlView { if (flag) { NSRect modifiedRect = cellFrame; if (hasBottomBar) { modifiedRect.size.height -= BOTTOM_BAR_HEIGHT; } NSRect srcRect = NSMakeRect(0, 0, [selectImage size].width, [selectImage size].height); NSRect destRect = NSMakeRect(modifiedRect.origin.x, modifiedRect.origin.y, [selectImage size].width, modifiedRect.size.height); while(destRect.origin.x < modifiedRect.origin.x + modifiedRect.size.width) { if (destRect.origin.x + destRect.size.width > modifiedRect.origin.x + modifiedRect.size.width) { srcRect.size.width -= (destRect.origin.x + destRect.size.width) - (modifiedRect.origin.x + modifiedRect.size.width) + 1; } [selectImage drawInRect:destRect fromRect:srcRect operation:NSCompositeCopy fraction:1.0]; destRect.origin.x += destRect.size.width; } } [self drawWithFrame:cellFrame inView:controlView]; } @end