// // BPAttachmentCell.m // Cells2 // // Created by Bea on 14/07/05. // Copyright 2005 __MyCompanyName__. All rights reserved. // #import "BPAttachmentCell.h" #import "BPUtil.h" @implementation BPAttachmentCell + (void)initialize { defaultFontSize = 11; NSRect screenBounds = [[NSScreen mainScreen] frame]; /* if (screenBounds.size.width <= 1024) { defaultCellImageSize = NSMakeSize(64, 64); } else { defaultCellImageSize = NSMakeSize(96, 96); } */ // test out the 96,96 size before using as the above. defaultCellImageSize = NSMakeSize(64, 64); defaultCellSize = NSMakeSize(defaultCellImageSize.width * 1.1, defaultCellImageSize.height * 1.5); } + (NSSize)defaultSize { return defaultCellSize; } - (NSSize)minSizeToFit { float maxItemWidth = [BPUtil maxOfA:[[self image] size].width b:[caption sizeWithAttributes:captionAttr].width + 20]; float maxOfItemAndCellWidth = [BPUtil maxOfA:defaultCellSize.width b:maxItemWidth]; float height = defaultCellSize.height; // impose some kind of maximum width if (maxOfItemAndCellWidth > 200) maxOfItemAndCellWidth = 200; return NSMakeSize(maxOfItemAndCellWidth, height); } - (id)init { [super init]; caption = @""; fontSize = defaultFontSize; // image cell setup [self setImageAlignment:NSImageAlignTop]; [self setImageFrameStyle:NSImageFrameGrayBezel]; [self setImageScaling:NSScaleProportionally]; // caption attributers captionAttr = [[[NSDictionary alloc] initWithObjectsAndKeys: [NSFont labelFontOfSize:fontSize], NSFontAttributeName, NULL] retain]; return self; } - (void)setCaption:(NSString *)captionText { [captionText retain]; [caption release]; caption = captionText; } - (void)setImage:(NSImage *)image { // important, otherwise image doesn't scale properly when scaling to larger size [image setScalesWhenResized:YES]; [image setSize:[BPUtil scaleSize:[image size] toHeight:defaultCellImageSize.height]]; [super setImage:image]; } - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { // draw image etc. [super drawWithFrame:cellFrame inView:controlView]; // draw caption NSSize textSize = [caption sizeWithAttributes:captionAttr]; NSPoint textPosn = cellFrame.origin; if (textSize.width > [self cellSize].width) { // text is too long to fit in the cell, so left align text so that you // can read the beginning of it textPosn.x += 8; // a bit of padding } else { // center text textPosn.x += cellFrame.size.width/2 - textSize.width/2; } textPosn.y += cellFrame.size.height - fontSize - 8; // 8 is to push text up closer to image [caption drawAtPoint:textPosn withAttributes:captionAttr]; } - (NSSize)cellSize { return [self minSizeToFit]; } @end