dev.stuconnolly.com / svn / safaritabs

  1. /*
  2.  *  $Id: STSessionRestoreTextFieldCell.m 227 2011-08-14 12:17:49Z stuart $
  3.  *
  4.  *  SafariTabs
  5.  *  http://stuconnolly.com/projects/safaritabs/
  6.  *
  7.  *  Copyright (c) 2010 Stuart Connolly. All rights reserved.
  8.  *
  9.  *  This program is free software: you can redistribute it and/or modify
  10.  *  it under the terms of the GNU General Public License as published by
  11.  *  the Free Software Foundation, either version 3 of the License, or
  12.  *  (at your option) any later version.
  13.  *
  14.  *  This program is distributed in the hope that it will be useful,
  15.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17.  *  GNU General Public License for more details.
  18.  *
  19.  *  You should have received a copy of the GNU General Public License
  20.  *  along with this program. If not, see <http://www.gnu.org/licenses/>.
  21.  */
  22.  
  23. #import "STSessionRestoreTextFieldCell.h"
  24. #import "STConstants.h"
  25.  
  26. const CGFloat STTableViewTabTitleFontSize = 12.0f;
  27.  
  28. @interface STSessionRestoreTextFieldCell ()
  29.  
  30. - (NSAttributedString *)_attributedStringForElementName;
  31. - (NSAttributedString *)_attributedStringForElementAddress;
  32.  
  33. - (NSDictionary *)_mainStringAttributedStringAttributes;
  34. - (NSDictionary *)_subStringAttributedStringAttributes;
  35.  
  36. @end
  37.  
  38. @implementation STSessionRestoreTextFieldCell
  39.  
  40. @synthesize elementName;
  41. @synthesize elementAddress;
  42.  
  43. - (id)init
  44. {
  45.     if ((self = [super init]))
  46.     {
  47.         _mainStringColor = [NSColor blackColor];
  48.         _subStringColor  = [NSColor grayColor];
  49.     }
  50.    
  51.     return self;
  52. }
  53.  
  54. - (id)copyWithZone:(NSZone *)zone
  55. {
  56.     STSessionRestoreTextFieldCell *cell = (STSessionRestoreTextFieldCell *)[super copyWithZone:zone];
  57.    
  58.     cell->elementName    = nil;
  59.     cell->elementAddress = nil;
  60.    
  61.     cell->elementName    = [elementName retain];
  62.     cell->elementAddress = [elementAddress retain];
  63.    
  64.     return cell;
  65. }
  66.  
  67. - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
  68. {  
  69.     (([self isHighlighted]) && (![[self highlightColorWithFrame:cellFrame inView:controlView] isEqualTo:[NSColor secondarySelectedControlColor]])) ? [self invertFontColors] : [self restoreFontColors];
  70.    
  71.     // Construct and get the sub text attributed string
  72.     NSAttributedString *mainString = [self _attributedStringForElementName];
  73.     NSAttributedString *subString  = [self _attributedStringForElementAddress];
  74.    
  75.     NSRect subFrame = NSMakeRect(0.0f, 0.0f, [subString size].width, [subString size].height);
  76.    
  77.     // Total height of both strings with a 2 pixel separation space
  78.     CGFloat totalHeight = [mainString size].height + [subString size].height + 1.0f;
  79.    
  80.     cellFrame.origin.y += (cellFrame.size.height - totalHeight) / 2.0f;
  81.     cellFrame.origin.x += 10.0f; // Indent main string from image
  82.    
  83.     // Position the sub text's frame rect
  84.     subFrame.origin.y = [mainString size].height + cellFrame.origin.y + 1.0f;
  85.     subFrame.origin.x = cellFrame.origin.x;
  86.    
  87.     cellFrame.size.height = totalHeight;
  88.    
  89.     NSUInteger i;
  90.    
  91.     CGFloat maxWidth        = cellFrame.size.width - 10;
  92.     CGFloat mainStringWidth = [mainString size].width;
  93.     CGFloat subStringWidth  = [subString size].width;
  94.    
  95.     if (maxWidth < mainStringWidth) {
  96.         for (i = 0; i <= [mainString length]; i++)
  97.         {
  98.             if ([[mainString attributedSubstringFromRange:NSMakeRange(0, i)] size].width >= maxWidth) {
  99.                 mainString = [[[NSMutableAttributedString alloc] initWithString:[[[mainString attributedSubstringFromRange:NSMakeRange(0, i - 3)] string] stringByAppendingString:@"..."] attributes:[self _mainStringAttributedStringAttributes]] autorelease];
  100.             }
  101.         }
  102.     }
  103.    
  104.     if (maxWidth < subStringWidth) {
  105.         for (i = 0; i <= [subString length]; i++)
  106.         {
  107.             if ([[subString attributedSubstringFromRange:NSMakeRange(0, i)] size].width >= maxWidth) { 
  108.                 subString = [[[NSMutableAttributedString alloc] initWithString:[[[subString attributedSubstringFromRange:NSMakeRange(0, i - 3)] string] stringByAppendingString:@"..."] attributes:[self _subStringAttributedStringAttributes]] autorelease];
  109.             }
  110.         }
  111.     }
  112.    
  113.     [mainString drawInRect:cellFrame];
  114.     [subString drawInRect:subFrame];
  115. }
  116.  
  117. - (void)invertFontColors
  118. {
  119.     _mainStringColor = [NSColor whiteColor];
  120.     _subStringColor  = [NSColor whiteColor];
  121. }
  122.  
  123. - (void)restoreFontColors
  124. {
  125.     _mainStringColor = [NSColor blackColor];
  126.     _subStringColor  = [NSColor grayColor];
  127. }
  128.  
  129. - (void)dealloc
  130. {
  131.     [elementName    release], elementName = nil;
  132.     [elementAddress release], elementAddress = nil;
  133.    
  134.     [super dealloc];
  135. }
  136.  
  137. - (NSAttributedString *)_attributedStringForElementName
  138. {
  139.     return [[[NSAttributedString alloc] initWithString:elementName attributes:[self _mainStringAttributedStringAttributes]] autorelease];
  140. }
  141.  
  142. - (NSAttributedString *)_attributedStringForElementAddress
  143. {
  144.     return [[[NSAttributedString alloc] initWithString:elementAddress attributes:[self _subStringAttributedStringAttributes]] autorelease];
  145. }
  146.  
  147. - (NSDictionary *)_mainStringAttributedStringAttributes
  148. {
  149.     return [NSDictionary dictionaryWithObjectsAndKeys:_mainStringColor, NSForegroundColorAttributeName, [NSFont boldSystemFontOfSize:STTableViewTabTitleFontSize], NSFontAttributeName, nil];
  150. }
  151.  
  152. - (NSDictionary *)_subStringAttributedStringAttributes
  153. {
  154.     return [NSDictionary dictionaryWithObjectsAndKeys:_subStringColor, NSForegroundColorAttributeName, [NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName, nil];
  155. }
  156.  
  157. @end
  158.