get rid of unneeded code by instead using built-in function

This commit is contained in:
Mitchell Livingston 2007-01-18 06:22:15 +00:00
parent 9d203de8ef
commit 263bf6934d
3 changed files with 35 additions and 70 deletions

View File

@ -36,9 +36,6 @@
+ (NSString *) stringForSpeedAbbrev: (float) speed;
+ (NSString *) stringForRatio: (float) ratio;
- (NSAttributedString *) attributedStringFittingInWidth: (float) width
attributes: (NSDictionary *) attributes;
- (NSComparisonResult) compareIP: (NSString *) string;
@end

View File

@ -79,7 +79,8 @@
+ (NSString *) stringForSpeed: (float) speed
{
return [[self stringForSpeedAbbrev: speed] stringByAppendingString: NSLocalizedString(@"B/s", "Transfer speed (Bytes per second)")];
return [[self stringForSpeedAbbrev: speed] stringByAppendingString:
NSLocalizedString(@"B/s", "Transfer speed (Bytes per second)")];
}
+ (NSString *) stringForSpeedAbbrev: (float) speed
@ -108,56 +109,6 @@
return [NSString stringWithFormat: @"%.0f", ratio];
}
- (NSAttributedString *) attributedStringFittingInWidth: (float) width
attributes: (NSDictionary *) attributes
{
int i;
float realWidth = [self sizeWithAttributes: attributes].width;
/* The whole string fits */
if( realWidth <= width )
return [[[NSAttributedString alloc] initWithString: self attributes: attributes] autorelease];
float ellipsisWidth = [[NSString ellipsis] sizeWithAttributes: attributes].width;
/* Width is too small */
if ( ellipsisWidth > width )
return [[[NSAttributedString alloc] initWithString: @"" attributes: attributes] autorelease];
/* Don't worry about ellipsis until the end */
width -= ellipsisWidth;
/* Approximate how many characters we'll need to drop... */
i = [self length] * (width / realWidth);
/* ... then refine it */
NSString * newString = [self substringToIndex: i];
realWidth = [newString sizeWithAttributes: attributes].width;
if( realWidth < width )
{
NSString * smallerString;
do
{
smallerString = newString;
newString = [self substringToIndex: ++i];
} while ([newString sizeWithAttributes: attributes].width <= width);
newString = smallerString;
}
else if( realWidth > width )
{
do
{
newString = [self substringToIndex: --i];
} while ([newString sizeWithAttributes: attributes].width > width);
}
else;
return [[[NSAttributedString alloc] initWithString: [newString stringByAppendingEllipsis]
attributes: attributes] autorelease];
}
- (NSComparisonResult) compareIP: (NSString *) string
{
if ([self isEqualToString: string])

View File

@ -171,12 +171,19 @@
{
BOOL highlighted = [self isHighlighted] && [[self highlightColorWithFrame: cellFrame inView: view]
isEqual: [NSColor alternateSelectedControlColor]];
NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
NSDictionary * nameAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
highlighted ? [NSColor whiteColor] : [NSColor controlTextColor], NSForegroundColorAttributeName,
[NSFont messageFontOfSize: 12.0], NSFontAttributeName, nil];
[NSFont messageFontOfSize: 12.0], NSFontAttributeName,
paragraphStyle, NSParagraphStyleAttributeName, nil];
NSDictionary * statusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
highlighted ? [NSColor whiteColor] : [NSColor darkGrayColor], NSForegroundColorAttributeName,
[NSFont messageFontOfSize: 9.0], NSFontAttributeName, nil];
[NSFont messageFontOfSize: 9.0], NSFontAttributeName,
paragraphStyle, NSParagraphStyleAttributeName, nil];
[paragraphStyle release];
NSPoint pen = cellFrame.origin;
const float PADDING = 3.0, LINE_PADDING = 2.0, EXTRA_NAME_SHIFT = 1.0;
@ -210,16 +217,17 @@
//name string
pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT;
pen.y = cellFrame.origin.y + PADDING;
NSAttributedString * nameString = [[info objectForKey: @"Name"] attributedStringFittingInWidth: mainWidth
attributes: nameAttributes];
[nameString drawAtPoint: pen];
NSAttributedString * nameString = [[NSAttributedString alloc] initWithString: [info objectForKey: @"Name"]
attributes: nameAttributes];
[nameString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, [nameString size].height)];
//progress string
pen.y += [nameString size].height + LINE_PADDING - 1.0;
NSAttributedString * progressString = [[info objectForKey: @"ProgressString"]
attributedStringFittingInWidth: mainWidth attributes: statusAttributes];
[progressString drawAtPoint: pen];
NSAttributedString * progressString = [[NSAttributedString alloc] initWithString:
[info objectForKey: @"ProgressString"] attributes: statusAttributes];
[progressString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, [progressString size].height)];
//progress bar
pen.x -= EXTRA_NAME_SHIFT;
@ -235,9 +243,14 @@
//status string
pen.x += EXTRA_NAME_SHIFT;
pen.y += LINE_PADDING;
NSAttributedString * statusString = [[info objectForKey: @"StatusString"]
attributedStringFittingInWidth: mainWidth attributes: statusAttributes];
[statusString drawAtPoint: pen];
NSAttributedString * statusString = [[NSAttributedString alloc] initWithString:
[info objectForKey: @"StatusString"] attributes: statusAttributes];
[statusString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, [statusString size].height)];
[nameString release];
[progressString release];
[statusString release];
}
else //small size
{
@ -258,16 +271,17 @@
? [info objectForKey: @"RemainingTimeString"]
: [info objectForKey: @"ShortStatusString"];
NSAttributedString * statusString = [[[NSAttributedString alloc] initWithString: realStatusString
attributes: statusAttributes] autorelease];
NSAttributedString * nameString = [[info objectForKey: @"Name"] attributedStringFittingInWidth:
mainWidth - [statusString size].width - LINE_PADDING attributes: nameAttributes];
NSAttributedString * statusString = [[NSAttributedString alloc] initWithString: realStatusString
attributes: statusAttributes];
NSAttributedString * nameString = [[NSAttributedString alloc] initWithString:
[info objectForKey: @"Name"] attributes: nameAttributes];
//place name string
pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT;
pen.y = cellFrame.origin.y + LINE_PADDING;
[nameString drawAtPoint: pen];
[nameString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth - [statusString size].width - LINE_PADDING,
[nameString size].height)];
//place status string
pen.x = NSMaxX(cellFrame) - PADDING - [statusString size].width;
@ -285,6 +299,9 @@
[self buildAdvancedBar: barWidth point: pen];
else
[self buildSimpleBar: barWidth point: pen];
[nameString release];
[statusString release];
}
[nameAttributes release];