2007-09-16 01:02:06 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-01-02 16:55:05 +00:00
|
|
|
* Copyright (c) 2007-2008 Transmission authors and contributors
|
2007-09-16 01:02:06 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#import "FileNameCell.h"
|
2007-10-20 19:25:14 +00:00
|
|
|
#import "FileOutlineView.h"
|
2007-10-20 19:13:52 +00:00
|
|
|
#import "Torrent.h"
|
2008-05-22 18:39:49 +00:00
|
|
|
#import "FileListNode.h"
|
2007-09-16 01:02:06 +00:00
|
|
|
#import "NSStringAdditions.h"
|
|
|
|
|
2008-11-01 14:26:51 +00:00
|
|
|
#define PADDING_HORIZONAL 2.0f
|
|
|
|
#define IMAGE_FOLDER_SIZE 16.0f
|
|
|
|
#define IMAGE_ICON_SIZE 32.0f
|
|
|
|
#define PADDING_BETWEEN_IMAGE_AND_TITLE 4.0f
|
|
|
|
#define PADDING_ABOVE_TITLE_FILE 2.0f
|
|
|
|
#define PADDING_BELOW_STATUS_FILE 2.0f
|
|
|
|
#define PADDING_BETWEEN_NAME_AND_FOLDER_STATUS 4.0f
|
2007-09-16 01:02:06 +00:00
|
|
|
|
|
|
|
@interface FileNameCell (Private)
|
|
|
|
|
|
|
|
- (NSRect) rectForTitleWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
|
2008-10-12 13:22:13 +00:00
|
|
|
- (NSRect) rectForStatusWithString: (NSAttributedString *) string withTitleRect: (NSRect) titleRect inBounds: (NSRect) bounds;
|
2007-09-16 01:02:06 +00:00
|
|
|
|
|
|
|
- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color;
|
|
|
|
- (NSAttributedString *) attributedStatusWithColor: (NSColor *) color;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation FileNameCell
|
|
|
|
|
2008-01-07 00:07:03 +00:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
|
|
|
[paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
|
|
|
|
|
|
|
|
fTitleAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
|
2008-11-01 14:26:51 +00:00
|
|
|
[NSFont messageFontOfSize: 12.0f], NSFontAttributeName,
|
2008-01-07 00:07:03 +00:00
|
|
|
paragraphStyle, NSParagraphStyleAttributeName, nil];
|
|
|
|
|
|
|
|
fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
|
2008-11-01 14:26:51 +00:00
|
|
|
[NSFont messageFontOfSize: 9.0f], NSFontAttributeName,
|
2008-01-07 00:07:03 +00:00
|
|
|
paragraphStyle, NSParagraphStyleAttributeName, nil];
|
|
|
|
|
|
|
|
[paragraphStyle release];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[fTitleAttributes release];
|
|
|
|
[fStatusAttributes release];
|
|
|
|
|
|
|
|
[fFolderImage release];
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone *) zone
|
|
|
|
{
|
|
|
|
FileNameCell * copy = [super copyWithZone: zone];
|
|
|
|
|
|
|
|
copy->fTitleAttributes = [fTitleAttributes retain];
|
|
|
|
copy->fStatusAttributes = [fStatusAttributes retain];
|
2008-01-07 00:23:15 +00:00
|
|
|
|
2008-01-07 00:07:03 +00:00
|
|
|
copy->fFolderImage = [fFolderImage retain];
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
- (NSImage *) image
|
|
|
|
{
|
2008-05-22 18:39:49 +00:00
|
|
|
FileListNode * node = (FileListNode *)[self objectValue];
|
|
|
|
NSImage * image;
|
|
|
|
if ([node isFolder])
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
if (!fFolderImage)
|
|
|
|
{
|
|
|
|
fFolderImage = [[[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode('fldr')] copy];
|
|
|
|
[fFolderImage setFlipped: YES];
|
|
|
|
}
|
|
|
|
image = fFolderImage;
|
|
|
|
}
|
2008-05-22 18:39:49 +00:00
|
|
|
else
|
|
|
|
image = [node icon];
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRect) imageRectForBounds: (NSRect) bounds
|
|
|
|
{
|
|
|
|
NSRect result = bounds;
|
|
|
|
|
|
|
|
result.origin.x += PADDING_HORIZONAL;
|
|
|
|
|
2008-11-01 14:26:51 +00:00
|
|
|
const CGFloat IMAGE_SIZE = [(FileListNode *)[self objectValue] isFolder] ? IMAGE_FOLDER_SIZE : IMAGE_ICON_SIZE;
|
|
|
|
result.origin.y += (result.size.height - IMAGE_SIZE) * 0.5f;
|
2007-09-16 01:02:06 +00:00
|
|
|
result.size = NSMakeSize(IMAGE_SIZE, IMAGE_SIZE);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRect) titleRectForBounds: (NSRect) bounds
|
|
|
|
{
|
|
|
|
return [self rectForTitleWithString: [self attributedTitleWithColor: nil] inBounds: bounds];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRect) statusRectForBounds: (NSRect) bounds
|
|
|
|
{
|
2008-10-12 13:22:13 +00:00
|
|
|
return [self rectForStatusWithString: [self attributedStatusWithColor: nil]
|
|
|
|
withTitleRect: [(FileListNode *)[self objectValue] isFolder] ? [self titleRectForBounds: bounds] : NSZeroRect
|
|
|
|
inBounds: bounds];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
|
|
|
|
{
|
|
|
|
//icon
|
2007-11-04 16:12:49 +00:00
|
|
|
[[self image] drawInRect: [self imageRectForBounds: cellFrame] fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
|
2007-09-16 01:02:06 +00:00
|
|
|
|
|
|
|
//title
|
|
|
|
NSColor * specialColor = nil;
|
2008-04-10 19:52:27 +00:00
|
|
|
if ([self isHighlighted]
|
2007-11-30 14:22:49 +00:00
|
|
|
&& [[self highlightColorWithFrame: cellFrame inView: controlView] isEqual: [NSColor alternateSelectedControlColor]])
|
2007-09-16 01:02:06 +00:00
|
|
|
specialColor = [NSColor whiteColor];
|
2008-10-12 13:22:13 +00:00
|
|
|
else if ([[(FileOutlineView *)[self controlView] torrent] checkForFiles: [(FileListNode *)[self objectValue] indexes]] == NSOffState)
|
2007-09-16 01:02:06 +00:00
|
|
|
specialColor = [NSColor disabledControlTextColor];
|
|
|
|
else;
|
|
|
|
|
|
|
|
NSAttributedString * titleString = [self attributedTitleWithColor: specialColor ? specialColor : [NSColor controlTextColor]];
|
|
|
|
NSRect titleRect = [self rectForTitleWithString: titleString inBounds: cellFrame];
|
|
|
|
[titleString drawInRect: titleRect];
|
|
|
|
|
|
|
|
//status
|
2008-10-12 13:22:13 +00:00
|
|
|
NSAttributedString * statusString = [self attributedStatusWithColor: specialColor ? specialColor : [NSColor darkGrayColor]];
|
|
|
|
NSRect statusRect = [self rectForStatusWithString: statusString withTitleRect: titleRect inBounds: cellFrame];
|
|
|
|
[statusString drawInRect: statusRect];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation FileNameCell (Private)
|
|
|
|
|
|
|
|
- (NSRect) rectForTitleWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
|
|
|
|
{
|
|
|
|
NSSize titleSize = [string size];
|
|
|
|
|
|
|
|
NSRect result = bounds;
|
|
|
|
|
2008-05-22 18:39:49 +00:00
|
|
|
if (![(FileListNode *)[self objectValue] isFolder])
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
result.origin.x += PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
|
|
|
|
result.origin.y += PADDING_ABOVE_TITLE_FILE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.origin.x += PADDING_HORIZONAL + IMAGE_FOLDER_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
|
2008-11-01 14:26:51 +00:00
|
|
|
result.origin.y += (result.size.height - titleSize.height) * 0.5f;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
result.size = titleSize;
|
|
|
|
result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-10-12 13:22:13 +00:00
|
|
|
- (NSRect) rectForStatusWithString: (NSAttributedString *) string withTitleRect: (NSRect) titleRect inBounds: (NSRect) bounds;
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
NSSize statusSize = [string size];
|
|
|
|
|
2008-10-12 13:22:13 +00:00
|
|
|
NSRect result;
|
|
|
|
if (![(FileListNode *)[self objectValue] isFolder])
|
|
|
|
{
|
2008-11-01 15:22:32 +00:00
|
|
|
result.origin.x = bounds.origin.x + PADDING_HORIZONAL + IMAGE_ICON_SIZE + PADDING_BETWEEN_IMAGE_AND_TITLE;
|
2008-11-01 17:52:25 +00:00
|
|
|
result.origin.y = NSMaxY(bounds) - PADDING_BELOW_STATUS_FILE - statusSize.height;
|
2008-10-12 13:22:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.origin.x = NSMaxX(titleRect) + PADDING_BETWEEN_NAME_AND_FOLDER_STATUS;
|
2008-11-01 14:26:51 +00:00
|
|
|
result.origin.y = NSMaxY(titleRect) - statusSize.height - 1.0f;
|
2008-10-12 13:22:13 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
result.size = statusSize;
|
|
|
|
result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color
|
|
|
|
{
|
|
|
|
if (color)
|
|
|
|
[fTitleAttributes setObject: color forKey: NSForegroundColorAttributeName];
|
|
|
|
|
2008-05-22 18:39:49 +00:00
|
|
|
NSString * title = [(FileListNode *)[self objectValue] name];
|
2007-09-16 01:02:06 +00:00
|
|
|
return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSAttributedString *) attributedStatusWithColor: (NSColor *) color
|
|
|
|
{
|
|
|
|
if (color)
|
|
|
|
[fStatusAttributes setObject: color forKey: NSForegroundColorAttributeName];
|
|
|
|
|
2007-10-20 19:13:52 +00:00
|
|
|
Torrent * torrent = [(FileOutlineView *)[self controlView] torrent];
|
2008-05-22 18:39:49 +00:00
|
|
|
FileListNode * node = (FileListNode *)[self objectValue];
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2008-10-12 13:22:13 +00:00
|
|
|
NSString * percentString;
|
2008-11-01 14:26:51 +00:00
|
|
|
CGFloat progress = [torrent fileProgress: node];
|
|
|
|
percentString = progress == 1.0f ? @"100%" : [NSString localizedStringWithFormat: @"%.2f%%", progress * 100.0f];
|
2008-10-12 13:22:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
NSString * status = [NSString localizedStringWithFormat: NSLocalizedString(@"%@ of %@",
|
|
|
|
"Inspector -> Files tab -> file status string"), percentString, [NSString stringForFileSize: [node size]]];
|
2007-09-16 01:02:06 +00:00
|
|
|
|
|
|
|
return [[[NSAttributedString alloc] initWithString: status attributes: fStatusAttributes] autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|