/*
* $Id: STSessionRestoreTextFieldCell.m 227 2011-08-14 12:17:49Z stuart $
*
* SafariTabs
* http://stuconnolly.com/projects/safaritabs/
*
* Copyright (c) 2010 Stuart Connolly. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import "STSessionRestoreTextFieldCell.h"
#import "STConstants.h"
const CGFloat STTableViewTabTitleFontSize = 12.0f;
@interface STSessionRestoreTextFieldCell ()
@end
@implementation STSessionRestoreTextFieldCell
@synthesize elementName;
@synthesize elementAddress;
- (id)init
{
if ((self = [super init]))
{
_mainStringColor
= [NSColor blackColor
];
_subStringColor
= [NSColor grayColor
];
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
STSessionRestoreTextFieldCell *cell = (STSessionRestoreTextFieldCell *)[super copyWithZone:zone];
cell->elementName = nil;
cell->elementAddress = nil;
cell->elementName = [elementName retain];
cell->elementAddress = [elementAddress retain];
return cell;
}
- (void)drawInteriorWithFrame
:(NSRect)cellFrame inView
:(NSView *)controlView
{
(([self isHighlighted
]) && (![[self highlightColorWithFrame
:cellFrame inView
:controlView
] isEqualTo
:[NSColor secondarySelectedControlColor
]])) ?
[self invertFontColors
] : [self restoreFontColors
];
// Construct and get the sub text attributed string
NSRect subFrame = NSMakeRect(0.0f, 0.0f, [subString size].width, [subString size].height);
// Total height of both strings with a 2 pixel separation space
CGFloat totalHeight = [mainString size].height + [subString size].height + 1.0f;
cellFrame.origin.y += (cellFrame.size.height - totalHeight) / 2.0f;
cellFrame.origin.x += 10.0f; // Indent main string from image
// Position the sub text's frame rect
subFrame.origin.y = [mainString size].height + cellFrame.origin.y + 1.0f;
subFrame.origin.x = cellFrame.origin.x;
cellFrame.size.height = totalHeight;
NSUInteger i;
CGFloat maxWidth = cellFrame.size.width - 10;
CGFloat mainStringWidth = [mainString size].width;
CGFloat subStringWidth = [subString size].width;
if (maxWidth < mainStringWidth) {
for (i = 0; i <= [mainString length]; i++)
{
if ([[mainString attributedSubstringFromRange:NSMakeRange(0, i)] size].width >= maxWidth) {
mainString
= [[[NSMutableAttributedString alloc
] initWithString
:[[[mainString attributedSubstringFromRange
:NSMakeRange
(0, i
- 3)] string] stringByAppendingString
:@"..."] attributes
:[self _mainStringAttributedStringAttributes
]] autorelease
];
}
}
}
if (maxWidth < subStringWidth) {
for (i = 0; i <= [subString length]; i++)
{
if ([[subString attributedSubstringFromRange:NSMakeRange(0, i)] size].width >= maxWidth) {
subString
= [[[NSMutableAttributedString alloc
] initWithString
:[[[subString attributedSubstringFromRange
:NSMakeRange
(0, i
- 3)] string] stringByAppendingString
:@"..."] attributes
:[self _subStringAttributedStringAttributes
]] autorelease
];
}
}
}
[mainString drawInRect:cellFrame];
[subString drawInRect:subFrame];
}
- (void)invertFontColors
{
_mainStringColor
= [NSColor whiteColor
];
_subStringColor
= [NSColor whiteColor
];
}
- (void)restoreFontColors
{
_mainStringColor
= [NSColor blackColor
];
_subStringColor
= [NSColor grayColor
];
}
- (void)dealloc
{
[elementName release], elementName = nil;
[elementAddress release], elementAddress = nil;
[super dealloc];
}
{
return [[[NSAttributedString alloc
] initWithString
:elementName attributes
:[self _mainStringAttributedStringAttributes
]] autorelease
];
}
{
return [[[NSAttributedString alloc
] initWithString
:elementAddress attributes
:[self _subStringAttributedStringAttributes
]] autorelease
];
}
{
return [NSDictionary dictionaryWithObjectsAndKeys
:_mainStringColor, NSForegroundColorAttributeName,
[NSFont boldSystemFontOfSize
:STTableViewTabTitleFontSize
], NSFontAttributeName,
nil];
}
{
return [NSDictionary dictionaryWithObjectsAndKeys
:_subStringColor, NSForegroundColorAttributeName,
[NSFont systemFontOfSize
:[NSFont smallSystemFontSize
]], NSFontAttributeName,
nil];
}
@end