2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-04-03 02:22:25 +00:00
|
|
|
* Copyright (c) 2006-2007 Transmission authors and contributors
|
2006-07-16 19:39:23 +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 "TorrentCell.h"
|
|
|
|
#import "TorrentTableView.h"
|
2007-08-20 16:34:21 +00:00
|
|
|
#import "NSStringAdditions.h"
|
2007-05-23 21:34:14 +00:00
|
|
|
#import "CTGradientAdditions.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
#define BAR_HEIGHT 12.0
|
|
|
|
|
2007-08-11 01:39:53 +00:00
|
|
|
#define IMAGE_SIZE_REG 32.0
|
|
|
|
#define IMAGE_SIZE_MIN 16.0
|
2007-08-11 23:49:26 +00:00
|
|
|
|
|
|
|
//end up being larger than font height
|
|
|
|
#define HEIGHT_TITLE 16.0
|
|
|
|
#define HEIGHT_STATUS 12.0
|
|
|
|
|
|
|
|
#define PADDING_HORIZONAL 2.0
|
2007-08-20 02:26:59 +00:00
|
|
|
#define PADDING_ABOVE_IMAGE_REG 9.0
|
2007-08-11 01:39:53 +00:00
|
|
|
#define PADDING_BETWEEN_IMAGE_AND_TITLE 4.0
|
|
|
|
#define PADDING_ABOVE_TITLE 2.0
|
|
|
|
#define PADDING_ABOVE_MIN_STATUS 4.0
|
|
|
|
#define PADDING_BETWEEN_TITLE_AND_MIN_STATUS 2.0
|
|
|
|
#define PADDING_BETWEEN_TITLE_AND_PROGRESS 2.0
|
|
|
|
#define PADDING_LESS_BETWEEN_TITLE_AND_BAR 1.0
|
|
|
|
#define PADDING_BETWEEN_PROGRESS_AND_BAR 2.0
|
2007-08-11 23:49:26 +00:00
|
|
|
#define PADDING_BETWEEN_TITLE_AND_BAR_MIN 3.0
|
2007-08-11 01:39:53 +00:00
|
|
|
#define PADDING_BETWEEN_BAR_AND_STATUS 2.0
|
|
|
|
|
2007-08-10 03:48:18 +00:00
|
|
|
#define MAX_PIECES 324
|
|
|
|
#define BLANK_PIECE -99
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
@interface TorrentCell (Private)
|
|
|
|
|
2007-08-16 03:02:27 +00:00
|
|
|
- (void) drawSimpleBar: (NSRect) barRect;
|
|
|
|
- (void) drawAdvancedBar: (NSRect) barRect;
|
2007-08-16 03:28:01 +00:00
|
|
|
- (NSImage *) advancedBar;
|
2007-08-11 01:39:53 +00:00
|
|
|
|
2007-08-11 23:49:26 +00:00
|
|
|
- (NSRect) rectForMinimalStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
|
2007-08-13 05:03:21 +00:00
|
|
|
- (NSRect) rectForTitleWithString: (NSAttributedString *) string basedOnMinimalStatusRect: (NSRect) statusRect
|
2007-08-11 17:04:10 +00:00
|
|
|
inBounds: (NSRect) bounds;
|
2007-08-11 23:49:26 +00:00
|
|
|
- (NSRect) rectForProgressWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
|
2007-08-13 05:03:21 +00:00
|
|
|
- (NSRect) rectForStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds;
|
2007-08-11 01:39:53 +00:00
|
|
|
|
|
|
|
- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color;
|
2007-08-11 04:46:21 +00:00
|
|
|
- (NSAttributedString *) attributedStatusString: (NSString *) string withColor: (NSColor *) color;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation TorrentCell
|
|
|
|
|
2007-08-10 03:48:18 +00:00
|
|
|
// Used to optimize drawing. They contain packed RGBA pixels for every color needed.
|
|
|
|
#define BE OSSwapBigToHostConstInt32
|
|
|
|
|
2007-08-13 05:03:21 +00:00
|
|
|
static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80
|
|
|
|
kBlue = BE(0x50A0FFFF), //80, 160, 255
|
|
|
|
kBlue2 = BE(0x1E46B4FF), //30, 70, 180
|
|
|
|
kGray = BE(0x969696FF), //150, 150, 150
|
2007-08-10 03:48:18 +00:00
|
|
|
kGreen1 = BE(0x99FFCCFF), //153, 255, 204
|
|
|
|
kGreen2 = BE(0x66FF99FF), //102, 255, 153
|
|
|
|
kGreen3 = BE(0x00FF66FF), //0, 255, 102
|
2007-08-13 05:03:21 +00:00
|
|
|
kWhite = BE(0xFFFFFFFF); //255, 255, 255
|
2007-08-10 03:48:18 +00:00
|
|
|
|
2007-08-08 16:14:52 +00:00
|
|
|
//only called one, so don't worry about release
|
2006-07-16 19:39:23 +00:00
|
|
|
- (id) init
|
|
|
|
{
|
2007-05-24 06:46:59 +00:00
|
|
|
if ((self = [super init]))
|
2007-05-23 21:34:14 +00:00
|
|
|
{
|
2006-07-16 19:39:23 +00:00
|
|
|
fDefaults = [NSUserDefaults standardUserDefaults];
|
2006-08-29 00:09:49 +00:00
|
|
|
|
2007-08-08 15:07:37 +00:00
|
|
|
NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
|
|
|
[paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
|
|
|
|
|
2007-08-11 01:39:53 +00:00
|
|
|
fTitleAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
|
2007-08-14 03:59:47 +00:00
|
|
|
[NSFont messageFontOfSize: 12.0], NSFontAttributeName,
|
|
|
|
paragraphStyle, NSParagraphStyleAttributeName, nil];
|
2007-08-11 01:39:53 +00:00
|
|
|
fStatusAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
|
2007-08-14 03:59:47 +00:00
|
|
|
[NSFont messageFontOfSize: 9.0], NSFontAttributeName,
|
|
|
|
paragraphStyle, NSParagraphStyleAttributeName, nil];
|
2007-08-08 15:07:37 +00:00
|
|
|
[paragraphStyle release];
|
2007-08-14 03:59:47 +00:00
|
|
|
|
|
|
|
fBarOverlayColor = [[NSColor colorWithDeviceWhite: 0.0 alpha: 0.2] retain];
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-05-23 21:34:14 +00:00
|
|
|
return self;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-08-10 03:48:18 +00:00
|
|
|
- (id) copyWithZone: (NSZone *) zone
|
2007-08-10 03:49:21 +00:00
|
|
|
{
|
2007-08-10 03:48:18 +00:00
|
|
|
TorrentCell * copy = [super copyWithZone: zone];
|
|
|
|
|
|
|
|
copy->fBitmap = nil;
|
|
|
|
copy->fPieces = NULL;
|
2007-08-14 03:59:47 +00:00
|
|
|
|
|
|
|
return copy;
|
2007-08-10 03:48:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
2007-08-10 03:49:21 +00:00
|
|
|
{
|
2007-08-10 03:48:18 +00:00
|
|
|
[fBitmap release];
|
|
|
|
if (fPieces)
|
|
|
|
free(fPieces);
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2007-08-11 03:09:31 +00:00
|
|
|
- (NSRect) iconRectForBounds: (NSRect) bounds
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-11 01:39:53 +00:00
|
|
|
NSRect result = bounds;
|
|
|
|
|
|
|
|
result.origin.x += PADDING_HORIZONAL;
|
2007-03-24 17:31:10 +00:00
|
|
|
|
2007-08-11 01:39:53 +00:00
|
|
|
float imageSize;
|
|
|
|
if ([fDefaults boolForKey: @"SmallView"])
|
|
|
|
{
|
|
|
|
imageSize = IMAGE_SIZE_MIN;
|
|
|
|
result.origin.y += (result.size.height - imageSize) * 0.5;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
imageSize = IMAGE_SIZE_REG;
|
2007-08-20 02:26:59 +00:00
|
|
|
result.origin.y += PADDING_ABOVE_IMAGE_REG;
|
2007-08-11 01:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result.size = NSMakeSize(imageSize, imageSize);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-11 01:39:53 +00:00
|
|
|
- (NSRect) titleRectForBounds: (NSRect) bounds
|
|
|
|
{
|
2007-08-13 05:03:21 +00:00
|
|
|
return [self rectForTitleWithString: [self attributedTitleWithColor: nil]
|
|
|
|
basedOnMinimalStatusRect: [self minimalStatusRectForBounds: bounds] inBounds: bounds];
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-08-11 01:39:53 +00:00
|
|
|
- (NSRect) minimalStatusRectForBounds: (NSRect) bounds
|
|
|
|
{
|
2007-08-11 04:46:21 +00:00
|
|
|
Torrent * torrent = [self representedObject];
|
2007-08-11 17:04:10 +00:00
|
|
|
NSString * string = [torrent isActive] && ![fDefaults boolForKey: @"SmallStatusRegular"]
|
2007-08-11 04:46:21 +00:00
|
|
|
? [torrent remainingTimeString] : [torrent shortStatusString];
|
2007-08-11 23:49:26 +00:00
|
|
|
return [self rectForMinimalStatusWithString: [self attributedStatusString: string withColor: nil] inBounds: bounds];
|
2007-08-11 01:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRect) progressRectForBounds: (NSRect) bounds
|
|
|
|
{
|
2007-08-11 23:49:26 +00:00
|
|
|
return [self rectForProgressWithString: [self attributedStatusString: [[self representedObject] progressString] withColor: nil]
|
|
|
|
inBounds: bounds];
|
2007-08-11 01:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRect) barRectForBounds: (NSRect) bounds
|
|
|
|
{
|
2007-08-13 04:30:52 +00:00
|
|
|
BOOL minimal = [fDefaults boolForKey: @"SmallView"];
|
|
|
|
|
|
|
|
NSRect result = bounds;
|
|
|
|
result.size.height = BAR_HEIGHT;
|
|
|
|
result.origin.x = PADDING_HORIZONAL + (minimal ? IMAGE_SIZE_MIN : IMAGE_SIZE_REG)
|
|
|
|
+ PADDING_BETWEEN_IMAGE_AND_TITLE - PADDING_LESS_BETWEEN_TITLE_AND_BAR;
|
|
|
|
|
|
|
|
result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE;
|
|
|
|
if (minimal)
|
|
|
|
result.origin.y += PADDING_BETWEEN_TITLE_AND_BAR_MIN;
|
|
|
|
else
|
|
|
|
result.origin.y += PADDING_BETWEEN_TITLE_AND_PROGRESS + HEIGHT_STATUS + PADDING_BETWEEN_PROGRESS_AND_BAR;
|
|
|
|
|
|
|
|
result.size.width = NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL - BUTTONS_TOTAL_WIDTH;
|
|
|
|
|
|
|
|
return result;
|
2007-08-11 01:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRect) statusRectForBounds: (NSRect) bounds
|
|
|
|
{
|
2007-08-11 23:49:26 +00:00
|
|
|
return [self rectForStatusWithString: [self attributedStatusString: [[self representedObject] statusString] withColor: nil]
|
|
|
|
inBounds: bounds];
|
2007-08-11 01:39:53 +00:00
|
|
|
}
|
|
|
|
|
2007-08-11 17:04:10 +00:00
|
|
|
- (void) drawInteriorWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-11 17:04:10 +00:00
|
|
|
[super drawInteriorWithFrame: cellFrame inView: controlView];
|
|
|
|
|
2007-08-10 03:20:01 +00:00
|
|
|
Torrent * torrent = [self representedObject];
|
2007-03-22 04:30:08 +00:00
|
|
|
|
2007-08-11 01:39:53 +00:00
|
|
|
BOOL minimal = [fDefaults boolForKey: @"SmallView"];
|
|
|
|
|
|
|
|
//error image
|
|
|
|
BOOL error = [torrent isError];
|
|
|
|
if (error && !fErrorImage)
|
|
|
|
{
|
|
|
|
fErrorImage = [[NSImage imageNamed: @"Error.png"] copy];
|
|
|
|
[fErrorImage setFlipped: YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
//icon
|
2007-08-16 04:12:43 +00:00
|
|
|
NSImage * icon = minimal && error ? fErrorImage : [torrent icon];
|
2007-08-11 03:09:31 +00:00
|
|
|
NSRect iconRect = [self iconRectForBounds: cellFrame];
|
2007-08-11 01:39:53 +00:00
|
|
|
[icon drawInRect: iconRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
|
|
|
|
|
|
|
|
if (error && !minimal)
|
|
|
|
{
|
|
|
|
NSRect errorRect = NSMakeRect(NSMaxX(iconRect) - IMAGE_SIZE_MIN, NSMaxY(iconRect) - IMAGE_SIZE_MIN,
|
|
|
|
IMAGE_SIZE_MIN, IMAGE_SIZE_MIN);
|
|
|
|
[fErrorImage drawInRect: errorRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
|
|
|
|
}
|
|
|
|
|
2007-08-11 02:12:47 +00:00
|
|
|
//text color
|
2007-08-11 01:39:53 +00:00
|
|
|
BOOL highlighted = [self isHighlighted] && [[self highlightColorWithFrame: cellFrame inView: controlView]
|
|
|
|
isEqual: [NSColor alternateSelectedControlColor]];
|
|
|
|
NSColor * titleColor, * statusColor;
|
|
|
|
if ([self isHighlighted]
|
|
|
|
&& [[self highlightColorWithFrame: cellFrame inView: controlView] isEqual: [NSColor alternateSelectedControlColor]])
|
|
|
|
{
|
|
|
|
titleColor = [NSColor whiteColor];
|
|
|
|
statusColor = [NSColor whiteColor];
|
|
|
|
}
|
2007-05-23 21:34:14 +00:00
|
|
|
else
|
2007-08-11 01:39:53 +00:00
|
|
|
{
|
|
|
|
titleColor = [NSColor controlTextColor];
|
|
|
|
statusColor = [NSColor darkGrayColor];
|
|
|
|
}
|
|
|
|
|
|
|
|
//minimal status
|
2007-08-11 17:04:10 +00:00
|
|
|
NSRect minimalStatusRect;
|
2007-08-11 01:39:53 +00:00
|
|
|
if (minimal)
|
2007-08-11 04:46:21 +00:00
|
|
|
{
|
|
|
|
NSString * string = ![fDefaults boolForKey: @"SmallStatusRegular"] && [torrent isActive]
|
|
|
|
? [torrent remainingTimeString] : [torrent shortStatusString];
|
2007-08-11 17:04:10 +00:00
|
|
|
NSAttributedString * minimalString = [self attributedStatusString: string withColor: statusColor];
|
2007-08-11 23:49:26 +00:00
|
|
|
minimalStatusRect = [self rectForMinimalStatusWithString: minimalString inBounds: cellFrame];
|
2007-08-11 17:04:10 +00:00
|
|
|
|
|
|
|
[minimalString drawInRect: minimalStatusRect];
|
2007-08-11 04:46:21 +00:00
|
|
|
}
|
2007-08-11 02:12:47 +00:00
|
|
|
|
|
|
|
//title
|
2007-08-11 17:04:10 +00:00
|
|
|
NSAttributedString * titleString = [self attributedTitleWithColor: titleColor];
|
2007-08-13 05:03:21 +00:00
|
|
|
NSRect titleRect = [self rectForTitleWithString: titleString basedOnMinimalStatusRect: minimalStatusRect inBounds: cellFrame];
|
2007-08-11 17:04:10 +00:00
|
|
|
[titleString drawInRect: titleRect];
|
2007-08-11 01:39:53 +00:00
|
|
|
|
|
|
|
//progress
|
|
|
|
NSRect progressRect;
|
|
|
|
if (!minimal)
|
|
|
|
{
|
2007-08-11 17:04:10 +00:00
|
|
|
NSAttributedString * progressString = [self attributedStatusString: [torrent progressString] withColor: statusColor];
|
2007-08-11 23:49:26 +00:00
|
|
|
progressRect = [self rectForProgressWithString: progressString inBounds: cellFrame];
|
2007-08-11 17:04:10 +00:00
|
|
|
[progressString drawInRect: progressRect];
|
2007-08-11 01:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//bar
|
2007-08-13 04:30:52 +00:00
|
|
|
NSRect barRect = [self barRectForBounds: cellFrame];
|
2007-08-16 03:02:27 +00:00
|
|
|
[fDefaults boolForKey: @"UseAdvancedBar"] ? [self drawAdvancedBar: barRect] : [self drawSimpleBar: barRect];
|
2007-08-11 01:39:53 +00:00
|
|
|
|
|
|
|
//status
|
|
|
|
if (!minimal)
|
|
|
|
{
|
2007-08-11 17:04:10 +00:00
|
|
|
NSAttributedString * statusString = [self attributedStatusString: [torrent statusString] withColor: statusColor];
|
2007-08-11 23:49:26 +00:00
|
|
|
[statusString drawInRect: [self rectForStatusWithString: statusString inBounds: cellFrame]];
|
2007-08-11 01:39:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation TorrentCell (Private)
|
|
|
|
|
2007-08-14 22:55:06 +00:00
|
|
|
#warning NSDivideRect ?
|
2007-08-16 03:02:27 +00:00
|
|
|
- (void) drawSimpleBar: (NSRect) barRect
|
2007-08-11 01:39:53 +00:00
|
|
|
{
|
|
|
|
Torrent * torrent = [self representedObject];
|
|
|
|
|
2007-08-11 23:49:26 +00:00
|
|
|
float progress = [torrent progress], left = [torrent progressLeft];
|
2007-05-23 21:34:14 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
if (progress < 1.0)
|
|
|
|
{
|
2007-08-08 16:31:10 +00:00
|
|
|
if (!fWhiteGradient)
|
|
|
|
fWhiteGradient = [[CTGradient progressWhiteGradient] retain];
|
2007-08-16 03:02:27 +00:00
|
|
|
[fWhiteGradient fillRect: barRect angle: -90];
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-08-11 01:39:53 +00:00
|
|
|
float include = progress + left;
|
|
|
|
if (include < 1.0)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-08-08 16:31:10 +00:00
|
|
|
if (!fLightGrayGradient)
|
|
|
|
fLightGrayGradient = [[CTGradient progressLightGrayGradient] retain];
|
2007-08-16 03:02:27 +00:00
|
|
|
|
|
|
|
NSRect noIncludeRect = barRect;
|
|
|
|
noIncludeRect.origin.x += barRect.size.width * include;
|
|
|
|
noIncludeRect.size.width *= 1.0 - include;
|
|
|
|
[fLightGrayGradient fillRect: noIncludeRect angle: -90];
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-16 03:02:27 +00:00
|
|
|
NSRect completeRect = barRect;
|
|
|
|
completeRect.size.width *= progress;
|
|
|
|
|
2007-08-10 03:20:01 +00:00
|
|
|
if ([torrent isActive])
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-10 03:20:01 +00:00
|
|
|
if ([torrent isChecking])
|
2007-08-08 16:31:10 +00:00
|
|
|
{
|
|
|
|
if (!fYellowGradient)
|
|
|
|
fYellowGradient = [[CTGradient progressYellowGradient] retain];
|
2007-08-16 03:02:27 +00:00
|
|
|
[fYellowGradient fillRect: completeRect angle: -90];
|
2007-08-08 16:31:10 +00:00
|
|
|
}
|
2007-08-10 03:20:01 +00:00
|
|
|
else if ([torrent isSeeding])
|
2007-07-04 18:14:58 +00:00
|
|
|
{
|
2007-08-16 03:02:27 +00:00
|
|
|
NSRect ratioRect = completeRect;
|
|
|
|
ratioRect.size.width *= [torrent progressStopRatio];
|
2007-07-04 18:14:58 +00:00
|
|
|
|
2007-08-16 03:02:27 +00:00
|
|
|
if (ratioRect.size.width < completeRect.size.width)
|
2007-08-08 16:31:10 +00:00
|
|
|
{
|
|
|
|
if (!fLightGreenGradient)
|
|
|
|
fLightGreenGradient = [[CTGradient progressLightGreenGradient] retain];
|
2007-08-16 03:02:27 +00:00
|
|
|
[fLightGreenGradient fillRect: completeRect angle: -90];
|
2007-08-08 16:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!fGreenGradient)
|
|
|
|
fGreenGradient = [[CTGradient progressGreenGradient] retain];
|
2007-08-16 03:02:27 +00:00
|
|
|
[fGreenGradient fillRect: ratioRect angle: -90];
|
2007-07-04 18:14:58 +00:00
|
|
|
}
|
|
|
|
else
|
2007-08-08 16:31:10 +00:00
|
|
|
{
|
|
|
|
if (!fBlueGradient)
|
|
|
|
fBlueGradient = [[CTGradient progressBlueGradient] retain];
|
2007-08-16 03:02:27 +00:00
|
|
|
[fBlueGradient fillRect: completeRect angle: -90];
|
2007-08-08 16:31:10 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
else
|
2007-07-04 21:50:50 +00:00
|
|
|
{
|
2007-08-10 03:20:01 +00:00
|
|
|
if ([torrent waitingToStart])
|
2007-07-04 21:50:50 +00:00
|
|
|
{
|
|
|
|
if (left <= 0.0)
|
2007-08-08 16:31:10 +00:00
|
|
|
{
|
|
|
|
if (!fDarkGreenGradient)
|
|
|
|
fDarkGreenGradient = [[CTGradient progressDarkGreenGradient] retain];
|
2007-08-16 03:02:27 +00:00
|
|
|
[fDarkGreenGradient fillRect: completeRect angle: -90];
|
2007-08-08 16:31:10 +00:00
|
|
|
}
|
2007-07-04 21:50:50 +00:00
|
|
|
else
|
2007-08-08 16:31:10 +00:00
|
|
|
{
|
|
|
|
if (!fDarkBlueGradient)
|
|
|
|
fDarkBlueGradient = [[CTGradient progressDarkBlueGradient] retain];
|
2007-08-16 03:02:27 +00:00
|
|
|
[fDarkBlueGradient fillRect: completeRect angle: -90];
|
2007-08-08 16:31:10 +00:00
|
|
|
}
|
2007-07-04 21:50:50 +00:00
|
|
|
}
|
|
|
|
else
|
2007-08-08 16:31:10 +00:00
|
|
|
{
|
|
|
|
if (!fGrayGradient)
|
|
|
|
fGrayGradient = [[CTGradient progressGrayGradient] retain];
|
2007-08-16 03:02:27 +00:00
|
|
|
[fGrayGradient fillRect: completeRect angle: -90];
|
2007-08-08 16:31:10 +00:00
|
|
|
}
|
2007-07-04 21:50:50 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-08-14 03:59:47 +00:00
|
|
|
[fBarOverlayColor set];
|
2007-08-16 03:02:27 +00:00
|
|
|
[NSBezierPath strokeRect: NSInsetRect(barRect, 0.5, 0.5)];
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 03:02:27 +00:00
|
|
|
- (void) drawAdvancedBar: (NSRect) barRect
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-16 03:28:01 +00:00
|
|
|
[[self advancedBar] drawInRect: barRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
|
2007-08-11 01:39:53 +00:00
|
|
|
|
2007-08-08 16:31:10 +00:00
|
|
|
if (!fTransparentGradient)
|
|
|
|
fTransparentGradient = [[CTGradient progressTransparentGradient] retain];
|
2007-08-16 03:12:58 +00:00
|
|
|
[fTransparentGradient fillRect: barRect angle: -90];
|
2007-08-08 16:31:10 +00:00
|
|
|
|
2007-08-14 03:59:47 +00:00
|
|
|
[fBarOverlayColor set];
|
2007-08-16 03:02:27 +00:00
|
|
|
[NSBezierPath strokeRect: NSInsetRect(barRect, 0.5, 0.5)];
|
2006-09-12 00:08:30 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 03:28:01 +00:00
|
|
|
- (NSImage *) advancedBar
|
2007-08-10 03:48:18 +00:00
|
|
|
{
|
|
|
|
if (!fBitmap)
|
|
|
|
fBitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: nil
|
|
|
|
pixelsWide: MAX_PIECES pixelsHigh: BAR_HEIGHT bitsPerSample: 8 samplesPerPixel: 4 hasAlpha: YES
|
|
|
|
isPlanar: NO colorSpaceName: NSCalibratedRGBColorSpace bytesPerRow: 0 bitsPerPixel: 0];
|
|
|
|
|
|
|
|
uint32_t * p;
|
|
|
|
uint8_t * bitmapData = [fBitmap bitmapData];
|
|
|
|
int bytesPerRow = [fBitmap bytesPerRow];
|
|
|
|
|
|
|
|
if (!fPieces)
|
|
|
|
{
|
|
|
|
fPieces = malloc(MAX_PIECES);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < MAX_PIECES; i++)
|
|
|
|
fPieces[i] = BLANK_PIECE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Torrent * torrent = [self representedObject];
|
|
|
|
int pieceCount = [torrent pieceCount];
|
|
|
|
int8_t * piecesAvailablity = malloc(pieceCount);
|
|
|
|
[torrent getAvailability: piecesAvailablity size: pieceCount];
|
|
|
|
|
|
|
|
//lines 2 to 14: blue, green, or gray depending on piece availability
|
|
|
|
int i, h, index = 0;
|
|
|
|
float increment = (float)pieceCount / MAX_PIECES, indexValue = 0;
|
|
|
|
uint32_t color;
|
|
|
|
BOOL change;
|
|
|
|
for (i = 0; i < MAX_PIECES; i++)
|
|
|
|
{
|
|
|
|
change = NO;
|
|
|
|
if (piecesAvailablity[index] < 0)
|
|
|
|
{
|
|
|
|
if (fPieces[i] != -1)
|
|
|
|
{
|
|
|
|
color = kBlue;
|
|
|
|
fPieces[i] = -1;
|
|
|
|
change = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (piecesAvailablity[index] == 0)
|
|
|
|
{
|
|
|
|
if (fPieces[i] != 0)
|
|
|
|
{
|
|
|
|
color = kGray;
|
|
|
|
fPieces[i] = 0;
|
|
|
|
change = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (piecesAvailablity[index] <= 4)
|
|
|
|
{
|
|
|
|
if (fPieces[i] != 1)
|
|
|
|
{
|
|
|
|
color = kGreen1;
|
|
|
|
fPieces[i] = 1;
|
|
|
|
change = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (piecesAvailablity[index] <= 8)
|
|
|
|
{
|
|
|
|
if (fPieces[i] != 2)
|
|
|
|
{
|
|
|
|
color = kGreen2;
|
|
|
|
fPieces[i] = 2;
|
|
|
|
change = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (fPieces[i] != 3)
|
|
|
|
{
|
|
|
|
color = kGreen3;
|
|
|
|
fPieces[i] = 3;
|
|
|
|
change = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change)
|
|
|
|
{
|
|
|
|
//point to pixel (i, 2) and draw "vertically"
|
|
|
|
p = (uint32_t *)(bitmapData + 2 * bytesPerRow) + i;
|
|
|
|
for (h = 2; h < BAR_HEIGHT; h++)
|
|
|
|
{
|
|
|
|
p[0] = color;
|
|
|
|
p = (uint32_t *)((uint8_t *)p + bytesPerRow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
indexValue += increment;
|
|
|
|
index = (int)indexValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//determine percentage finished and available
|
|
|
|
int have = rintf((float)MAX_PIECES * [torrent progress]), avail;
|
|
|
|
if (![torrent isActive] || [torrent progress] >= 1.0 || [torrent totalPeersConnected] <= 0)
|
|
|
|
avail = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float * piecesFinished = malloc(pieceCount * sizeof(float));
|
|
|
|
[torrent getAmountFinished: piecesFinished size: pieceCount];
|
|
|
|
|
|
|
|
float available = 0;
|
|
|
|
for (i = 0; i < pieceCount; i++)
|
|
|
|
if (piecesAvailablity[i] > 0)
|
|
|
|
available += 1.0 - piecesFinished[i];
|
|
|
|
|
|
|
|
avail = rintf(MAX_PIECES * available / (float)pieceCount);
|
|
|
|
if (have + avail > MAX_PIECES) //case if both end in .5 and all pieces are available
|
|
|
|
avail--;
|
|
|
|
|
|
|
|
free(piecesFinished);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(piecesAvailablity);
|
|
|
|
|
|
|
|
//first two lines: dark blue to show progression, green to show available
|
|
|
|
p = (uint32_t *)bitmapData;
|
|
|
|
for (i = 0; i < have; i++)
|
|
|
|
{
|
|
|
|
p[i] = kBlue2;
|
|
|
|
p[i + bytesPerRow / 4] = kBlue2;
|
|
|
|
}
|
|
|
|
for (; i < avail + have; i++)
|
|
|
|
{
|
|
|
|
p[i] = kGreen3;
|
|
|
|
p[i + bytesPerRow / 4] = kGreen3;
|
|
|
|
}
|
|
|
|
for (; i < MAX_PIECES; i++)
|
|
|
|
{
|
|
|
|
p[i] = kWhite;
|
|
|
|
p[i + bytesPerRow / 4] = kWhite;
|
|
|
|
}
|
|
|
|
|
|
|
|
//actually draw image
|
|
|
|
NSImage * bar = [[NSImage alloc] initWithSize: [fBitmap size]];
|
2007-08-11 01:39:53 +00:00
|
|
|
[bar setFlipped: YES];
|
2007-08-10 03:48:18 +00:00
|
|
|
[bar addRepresentation: fBitmap];
|
|
|
|
|
|
|
|
return [bar autorelease];
|
|
|
|
}
|
|
|
|
|
2007-08-11 23:49:26 +00:00
|
|
|
- (NSRect) rectForMinimalStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
|
2007-08-11 17:04:10 +00:00
|
|
|
{
|
|
|
|
if (![fDefaults boolForKey: @"SmallView"])
|
|
|
|
return NSZeroRect;
|
|
|
|
|
|
|
|
NSRect result = bounds;
|
|
|
|
result.size = [string size];
|
|
|
|
|
|
|
|
result.origin.x += bounds.size.width - result.size.width - PADDING_HORIZONAL;
|
|
|
|
result.origin.y += PADDING_ABOVE_MIN_STATUS;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-08-13 05:03:21 +00:00
|
|
|
- (NSRect) rectForTitleWithString: (NSAttributedString *) string basedOnMinimalStatusRect: (NSRect) statusRect
|
2007-08-11 17:04:10 +00:00
|
|
|
inBounds: (NSRect) bounds
|
2006-07-25 00:59:14 +00:00
|
|
|
{
|
2007-08-11 01:39:53 +00:00
|
|
|
BOOL minimal = [fDefaults boolForKey: @"SmallView"];
|
|
|
|
|
2007-08-11 17:04:10 +00:00
|
|
|
NSRect result = bounds;
|
2007-08-11 01:39:53 +00:00
|
|
|
result.origin.y += PADDING_ABOVE_TITLE;
|
|
|
|
result.origin.x += PADDING_HORIZONAL + (minimal ? IMAGE_SIZE_MIN : IMAGE_SIZE_REG) + PADDING_BETWEEN_IMAGE_AND_TITLE;
|
|
|
|
|
2007-08-11 17:04:10 +00:00
|
|
|
result.size = [string size];
|
2007-08-11 01:39:53 +00:00
|
|
|
result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL
|
|
|
|
- (minimal ? PADDING_BETWEEN_TITLE_AND_MIN_STATUS + statusRect.size.width : 0));
|
|
|
|
|
|
|
|
return result;
|
2006-07-25 00:59:14 +00:00
|
|
|
}
|
|
|
|
|
2007-08-11 23:49:26 +00:00
|
|
|
- (NSRect) rectForProgressWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-11 01:39:53 +00:00
|
|
|
if ([fDefaults boolForKey: @"SmallView"])
|
|
|
|
return NSZeroRect;
|
2007-01-18 06:22:15 +00:00
|
|
|
|
2007-08-11 23:49:26 +00:00
|
|
|
NSRect result = bounds;
|
|
|
|
result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE + PADDING_BETWEEN_TITLE_AND_PROGRESS;
|
|
|
|
result.origin.x += PADDING_HORIZONAL + IMAGE_SIZE_REG + PADDING_BETWEEN_IMAGE_AND_TITLE;
|
2006-09-11 23:52:03 +00:00
|
|
|
|
2007-08-11 23:49:26 +00:00
|
|
|
result.size = [string size];
|
|
|
|
result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
|
2007-08-11 01:39:53 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-11 23:49:26 +00:00
|
|
|
- (NSRect) rectForStatusWithString: (NSAttributedString *) string inBounds: (NSRect) bounds
|
2007-08-11 01:39:53 +00:00
|
|
|
{
|
|
|
|
if ([fDefaults boolForKey: @"SmallView"])
|
|
|
|
return NSZeroRect;
|
|
|
|
|
2007-08-11 23:49:26 +00:00
|
|
|
NSRect result = bounds;
|
|
|
|
result.origin.y += PADDING_ABOVE_TITLE + HEIGHT_TITLE + PADDING_BETWEEN_TITLE_AND_PROGRESS + HEIGHT_STATUS
|
|
|
|
+ PADDING_BETWEEN_PROGRESS_AND_BAR + BAR_HEIGHT + PADDING_BETWEEN_BAR_AND_STATUS;
|
|
|
|
result.origin.x += PADDING_HORIZONAL + IMAGE_SIZE_REG + PADDING_BETWEEN_IMAGE_AND_TITLE;
|
2007-08-11 01:39:53 +00:00
|
|
|
|
2007-08-11 23:49:26 +00:00
|
|
|
result.size = [string size];
|
|
|
|
result.size.width = MIN(result.size.width, NSMaxX(bounds) - result.origin.x - PADDING_HORIZONAL);
|
2007-08-11 01:39:53 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-11 01:39:53 +00:00
|
|
|
- (NSAttributedString *) attributedTitleWithColor: (NSColor *) color
|
|
|
|
{
|
|
|
|
if (color)
|
|
|
|
[fTitleAttributes setObject: color forKey: NSForegroundColorAttributeName];
|
2007-08-11 23:49:26 +00:00
|
|
|
|
2007-08-11 01:39:53 +00:00
|
|
|
NSString * title = [[self representedObject] name];
|
|
|
|
return [[[NSAttributedString alloc] initWithString: title attributes: fTitleAttributes] autorelease];
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-11 04:46:21 +00:00
|
|
|
- (NSAttributedString *) attributedStatusString: (NSString *) string withColor: (NSColor *) color
|
2007-08-11 01:39:53 +00:00
|
|
|
{
|
|
|
|
if (color)
|
|
|
|
[fStatusAttributes setObject: color forKey: NSForegroundColorAttributeName];
|
|
|
|
|
2007-08-11 04:46:21 +00:00
|
|
|
return [[[NSAttributedString alloc] initWithString: string attributes: fStatusAttributes] autorelease];
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|