5 // Created by Mac-arena the Bored Zo on 2005-10-30.
6 // Copyright 2005 Adium Team. All rights reserved.
9 #import "AXCFileCell.h"
11 @implementation AXCFileCell
13 - (id)initTextCell:(NSString *)str {
14 if((self = [super initTextCell:str])) {
15 [self setIconSourceMask:AXCFileCellIconSourceAll];
20 - (void)setObjectValue:(id <NSCopying>)newObj {
21 NSString *path = newObj;
23 if (![path isAbsolutePath])
24 path = [[[NSFileManager defaultManager] currentDirectoryPath] stringByAppendingPathComponent:path];
26 [super setObjectValue:path];
31 - (enum AXCFileCellIconSourceMask)iconSourceMask {
32 return iconSource.iconSourceMask;
34 - (void)setIconSourceMask:(enum AXCFileCellIconSourceMask)mask {
35 iconSource.iconSourceMask = mask;
40 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)view {
41 NSString *path = [self stringValue];
43 float gutter = 0.0, contentHeight = 16.0;
44 static const float visualSeparation = 8.0;
47 //first try to get a preview (AXCAbstractXtraDocument creates these).
49 if (iconSource.iconSourceBitfield.getPreviewsByFullPath)
50 icon = [NSImage imageNamed:[@"Preview of " stringByAppendingString:path]];
51 //if there's no preview for the absolute path, try the filename.
52 if (iconSource.iconSourceBitfield.getPreviewsByFilename && (!icon) && [path isAbsolutePath])
53 icon = [NSImage imageNamed:[@"Preview of " stringByAppendingString:[path lastPathComponent]]];
54 //if there isn't a preview for either path, just get the file's icon.
55 if (iconSource.iconSourceBitfield.getPreviewsFromFileIcons && !icon) {
56 icon = [[NSWorkspace sharedWorkspace] iconForFile:path];
57 [icon setFlipped:YES];
59 //fallback on generic file icon.
61 icon = [[NSWorkspace sharedWorkspace] iconForFileType:@"'docu'"];
62 [icon setFlipped:YES];
65 //get the largest size that will fit entirely within the frame.
66 float cellDimension = MIN(cellFrame.size.width, cellFrame.size.height);
67 if (cellDimension > 256.0)
68 contentHeight = 256.0;
69 else if (cellDimension > 128.0)
70 contentHeight = 128.0;
71 else if (cellDimension > 48.0)
73 else if (cellDimension > 32.0)
77 NSSize imageSize = [icon size];
78 if (imageSize.height > contentHeight) {
79 const float scale = imageSize.height / contentHeight;
80 imageSize.width /= scale;
81 imageSize.height /= scale;
83 [icon setSize:imageSize];
85 gutter = (cellDimension - contentHeight) / 2.0;
86 NSRect imageSrcRect = {
90 NSRect imageDestRect = {
91 { cellFrame.origin.x + gutter + visualSeparation, cellFrame.origin.y + gutter },
95 [icon drawInRect:imageDestRect
97 operation:NSCompositeSourceOver
101 /*draw the filename*/ {
102 NSString *filename = [[NSFileManager defaultManager] displayNameAtPath:path];
104 float leftMargin = gutter + (visualSeparation * 2.0) + contentHeight;
106 { cellFrame.origin.x + leftMargin, cellFrame.origin.y + gutter },
107 { cellFrame.size.width - leftMargin, contentHeight }
111 if([NSGraphicsContext currentContextDrawingToScreen] && [self isHighlighted]) {
112 //credit to Ken Ferry for coming up with this test.
113 if([[self highlightColorWithFrame:cellFrame inView:view] isEqual:[NSColor alternateSelectedControlColor]])
114 textColor = [NSColor alternateSelectedControlTextColor];
116 textColor = [NSColor selectedControlTextColor];
118 textColor = [NSColor controlTextColor];
120 NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
121 [NSFont systemFontOfSize:0.0], NSFontAttributeName,
122 textColor, NSForegroundColorAttributeName,
125 [filename drawInRect:destRect withAttributes:attrs];