1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-30 19:46:56 +00:00
transmission/macosx/FileBrowserCell.m

200 lines
7 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
2007-08-10 17:55:09 +00:00
#define PADDING_HORIZONAL 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_FILE 2.0
#define PADDING_BELOW_STATUS_FILE 2.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)
2007-08-10 18:00:44 +00:00
{
if (!fFolderImage)
{
fFolderImage = [[[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode('fldr')] copy];
[fFolderImage setFlipped: YES];
[fFolderImage setScalesWhenResized: YES];
}
image = fFolderImage;
}
else
{
[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) imageRectForBounds: (NSRect) bounds
{
NSRect result = bounds;
result.origin.x += PADDING_HORIZONAL;
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;
}
- (NSRect) titleRectForBounds: (NSRect) bounds
{
NSAttributedString * title = [self attributedTitleWithColor: nil];
NSSize titleSize = [title size];
NSRect result = bounds;
if (![[[self objectValue] objectForKey: @"IsFolder"] boolValue])
{
2007-08-10 17:55:09 +00:00
result.origin.x += PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
result.origin.y += PADDING_ABOVE_TITLE_FILE;
}
else
{
2007-08-10 17:55:09 +00:00
result.origin.x += PADDING_HORIZONAL + IMAGE_FOLDER_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
result.origin.y += (result.size.height - titleSize.height) * 0.5;
}
result.size = titleSize;
2007-08-10 17:55:09 +00:00
result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
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;
2007-08-10 17:55:09 +00:00
result.origin.x += PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
result.origin.y += result.size.height - PADDING_BELOW_STATUS_FILE - statusSize.height;
result.size = statusSize;
2007-08-10 17:55:09 +00:00
result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
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