Documentation of `SecCertificateGetData()` says that we should malloc space for its...
[adiumx.git] / Other / XtrasCreator / AXCFileCell.m
bloba0e611a52dab2bf0b0d60234241364662e00cbd5
1 //
2 //  AXCFileCell.m
3 //  XtrasCreator
4 //
5 //  Created by Mac-arena the Bored Zo on 2005-10-30.
6 //  Copyright 2005 Adium Team. All rights reserved.
7 //
9 #import "AXCFileCell.h"
11 @implementation AXCFileCell
13 - (id)initTextCell:(NSString *)str {
14         if((self = [super initTextCell:str])) {
15                 [self setIconSourceMask:AXCFileCellIconSourceAll];
16         }
17         return self;
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];
29 #pragma mark -
31 - (enum AXCFileCellIconSourceMask)iconSourceMask {
32         return iconSource.iconSourceMask;
34 - (void)setIconSourceMask:(enum AXCFileCellIconSourceMask)mask {
35         iconSource.iconSourceMask = mask;
38 #pragma mark -
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;
46         /*draw the icon*/ {
47                 //first try to get a preview (AXCAbstractXtraDocument creates these).
48                 NSImage *icon = nil;
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];
58                 }
59                 //fallback on generic file icon.
60                 if (!icon) {
61                         icon = [[NSWorkspace sharedWorkspace] iconForFileType:@"'docu'"];
62                         [icon setFlipped:YES];
63                 }
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)
72                         contentHeight =  48.0;
73                 else if (cellDimension >  32.0)
74                         contentHeight =  32.0;
75                 else
76                         contentHeight =  16.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;
82                 }
83                 [icon setSize:imageSize];
85                 gutter = (cellDimension - contentHeight) / 2.0;
86                 NSRect imageSrcRect = {
87                         NSZeroPoint,
88                         imageSize
89                 };
90                 NSRect imageDestRect = {
91                         { cellFrame.origin.x + gutter + visualSeparation, cellFrame.origin.y + gutter },
92                         imageSize
93                 };
95                 [icon drawInRect:imageDestRect
96                                 fromRect:imageSrcRect
97                            operation:NSCompositeSourceOver
98                                 fraction:1.0];
99         }
101         /*draw the filename*/ {
102                 NSString *filename = [[NSFileManager defaultManager] displayNameAtPath:path];
104                 float leftMargin = gutter + (visualSeparation * 2.0) + contentHeight;
105                 NSRect destRect = {
106                         { cellFrame.origin.x + leftMargin, cellFrame.origin.y + gutter },
107                         { cellFrame.size.width - leftMargin, contentHeight }
108                 };
110                 NSColor *textColor;
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];
115                         else
116                                 textColor = [NSColor selectedControlTextColor];
117                 } else
118                         textColor = [NSColor controlTextColor];
120                 NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
121                         [NSFont systemFontOfSize:0.0], NSFontAttributeName,
122                         textColor, NSForegroundColorAttributeName,
123                         nil];
125                 [filename drawInRect:destRect withAttributes:attrs];
126         }
129 @end