transmission/macosx/FileBrowserCell.m

190 lines
6.9 KiB
Mathematica
Raw Normal View History

2007-04-03 02:15:24 +00:00
/******************************************************************************
* $Id$
*
* Copyright (c) 2007 Transmission authors and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
2007-01-18 01:19:14 +00:00
#import "FileBrowserCell.h"
2007-01-18 22:53:27 +00:00
#import "StringAdditions.h"
2007-01-18 01:19:14 +00:00
#define PADDING_BEFORE_IMAGE 2.0
#define IMAGE_FOLDER_SIZE 16.0
#define IMAGE_ICON_SIZE 32.0
#define PADDING_BETWEEN_IMAGE_AND_TITLE 4.0
#define PADDING_ABOVE_TITLE_REG 1.0
#define PADDING_BELOW_STATUS_REG 1.0
#define PADDING_AFTER_TITLE 1.0
2007-01-18 01:19:14 +00:00
@interface FileBrowserCell (Private)
- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color;
- (NSAttributedString *) attributedStatusWithColor: (NSColor *) color;
@end
2007-01-18 01:19:14 +00:00
@implementation FileBrowserCell
- (void) awakeFromNib
{
[self setLeaf: YES];
}
2007-01-18 01:19:14 +00:00
- (void) setImage: (NSImage *) image
{
if (!image)
image = [[[[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode('fldr')] copy] autorelease];
2007-01-18 01:19:14 +00:00
[image setFlipped: YES];
[image setScalesWhenResized: YES];
2007-01-18 01:19:14 +00:00
[super setImage: image];
}
- (void) setProgress: (float) progress
{
fPercent = progress * 100.0;
}
- (NSRect) titleRectForBounds: (NSRect) bounds
{
NSAttributedString * title = [self attributedTitleWithColor: nil];
NSSize titleSize = [title size];
NSRect result = bounds;
if (![[[self objectValue] objectForKey: @"IsFolder"] boolValue])
{
result.origin.x += PADDING_BEFORE_IMAGE + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
result.origin.y += PADDING_ABOVE_TITLE_REG;
}
else
{
result.origin.x += PADDING_BEFORE_IMAGE + IMAGE_FOLDER_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
result.origin.y += (result.size.height - titleSize.height) * 0.5;
}
result.size = titleSize;
result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_AFTER_TITLE);
2007-01-18 01:19:14 +00:00
return result;
}
- (NSRect) statusRectForBounds: (NSRect) bounds
{
if ([[[self objectValue] objectForKey: @"IsFolder"] boolValue])
return NSZeroRect;
NSAttributedString * status = [self attributedStatusWithColor: nil];
NSSize statusSize = [status size];
NSRect result = bounds;
result.origin.x += PADDING_BEFORE_IMAGE + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
result.origin.y += result.size.height - PADDING_BELOW_STATUS_REG - statusSize.height;
result.size = statusSize;
result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_AFTER_TITLE);
return result;
}
- (NSRect) imageRectForBounds: (NSRect) bounds
{
NSRect result = bounds;
result.origin.x += PADDING_BEFORE_IMAGE;
const float IMAGE_SIZE = [[[self objectValue] objectForKey: @"IsFolder"] boolValue] ? IMAGE_FOLDER_SIZE : IMAGE_ICON_SIZE;
result.origin.y += (result.size.height - IMAGE_SIZE) * 0.5;
result.size = NSMakeSize(IMAGE_SIZE, IMAGE_SIZE);
return result;
}
- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
{
//icon
[[self image] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect
operation: NSCompositeSourceOver fraction: 1.0];
//title
BOOL highlighted = [self isHighlighted] && [[self highlightColorWithFrame: cellFrame inView: controlView]
isEqual: [NSColor alternateSelectedControlColor]];
[[self attributedTitleWithColor: highlighted ? [NSColor whiteColor] : [NSColor controlTextColor]]
drawInRect: [self titleRectForBounds: cellFrame]];
//status
NSRect statusRect = [self statusRectForBounds: cellFrame];
if (!NSEqualRects(statusRect, NSZeroRect))
[[self attributedStatusWithColor: highlighted ? [NSColor whiteColor] : [NSColor darkGrayColor]] drawInRect: statusRect];
2007-01-18 01:19:14 +00:00
}
@end
@implementation FileBrowserCell (Private)
- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color
{
if (!fTitleAttributes)
{
NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
fTitleAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
[NSFont messageFontOfSize: 12.0], NSFontAttributeName,
paragraphStyle, NSParagraphStyleAttributeName, nil];
[paragraphStyle release];
}
if (color)
[fTitleAttributes setObject: color forKey: NSForegroundColorAttributeName];
NSString * title = [[self objectValue] objectForKey: @"Name"];
return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease];
}
- (NSAttributedString *) attributedStatusWithColor: (NSColor *) color
{
if (!fStatusAttributes)
{
NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
[NSFont messageFontOfSize: 9.0], NSFontAttributeName,
paragraphStyle, NSParagraphStyleAttributeName, nil];
[paragraphStyle release];
}
if (color)
[fStatusAttributes setObject: color forKey: NSForegroundColorAttributeName];
#warning fPercent?
NSString * status = [NSString stringWithFormat: NSLocalizedString(@"%.2f%% of %@",
"Inspector -> Files tab -> file status string"), fPercent,
[NSString stringForFileSize: [[[self objectValue] objectForKey: @"Size"] unsignedLongLongValue]]];
return [[[NSAttributedString alloc] initWithString: status attributes: fStatusAttributes] autorelease];
}
@end