dev.stuconnolly.com / svn / safaritabs

  1. /*
  2.  *  $Id: STImageTextFieldCell.m 224 2011-08-13 20:39:45Z 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 "STImageTextFieldCell.h"
  24.  
  25. @implementation STImageTextFieldCell
  26.  
  27. - (id)initWithImage:(NSImage *)image
  28. {
  29.     if ((self = [super init])) {
  30.         _image = image;
  31.     }
  32.    
  33.     return self;
  34. }
  35.  
  36. - (id)copyWithZone:(NSZone *)zone
  37. {
  38.     STImageTextFieldCell *cell = (STImageTextFieldCell *)[super copyWithZone:zone];
  39.    
  40.     cell->_image = nil;
  41.     cell->_image = [_image retain];
  42.    
  43.     return cell;
  44. }
  45.  
  46. - (NSImage *)image
  47. {
  48.     return _image;
  49. }
  50.  
  51. - (void)setImage:(NSImage *)image
  52. {
  53.     if (_image != image) {
  54.         [_image release];
  55.         _image = [image retain];
  56.     }
  57. }
  58.  
  59. - (NSSize)cellSize
  60. {
  61.     NSSize cellSize = [super cellSize];
  62.     cellSize.width += (_image ? [_image size].width : 0) + 3;
  63.    
  64.     return cellSize;
  65. }
  66.  
  67. - (NSRect)imageFrameForCellFrame:(NSRect)cellFrame
  68. {
  69.     if (_image != nil) {
  70.         NSRect imageFrame;
  71.         imageFrame.size = [_image size];
  72.         imageFrame.origin = cellFrame.origin;
  73.         imageFrame.origin.x += 3;
  74.         imageFrame.origin.y += (CGFloat)ceil((cellFrame.size.height - imageFrame.size.height) / 2);
  75.        
  76.         return imageFrame;
  77.     }
  78.    
  79.     return NSZeroRect;
  80. }
  81.  
  82. - (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent
  83. {
  84.     NSRect textFrame, imageFrame;
  85.     NSDivideRect(aRect, &imageFrame, &textFrame, 3 + [_image size].width, NSMinXEdge);
  86.     [super editWithFrame:textFrame inView:controlView editor:textObj delegate:anObject event:theEvent];
  87. }
  88.  
  89. - (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength
  90. {
  91.     NSRect textFrame, imageFrame;
  92.     NSDivideRect(aRect, &imageFrame, &textFrame, 3 + [_image size].width, NSMinXEdge);
  93.     [super selectWithFrame:textFrame inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
  94. }
  95.  
  96. - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
  97. {
  98.     if (_image != nil) {
  99.         NSRect imageFrame;
  100.         NSSize imageSize = [_image size];
  101.        
  102.         NSDivideRect(cellFrame, &imageFrame, &cellFrame, 3 + imageSize.width, NSMinXEdge);
  103.        
  104.         if ([self drawsBackground]) {
  105.             [[self backgroundColor] set];
  106.             NSRectFill(imageFrame);
  107.         }
  108.        
  109.         imageFrame.origin.x += 3;
  110.         imageFrame.size = imageSize;
  111.        
  112.         imageFrame.origin.y += ([controlView isFlipped]) ? (CGFloat)ceil((cellFrame.size.height + imageFrame.size.height) / 2) : (CGFloat)ceil((cellFrame.size.height - imageFrame.size.height) / 2);
  113.        
  114.         [_image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
  115.     }
  116.    
  117.     [super drawWithFrame:cellFrame inView:controlView];
  118. }
  119.  
  120. - (void)dealloc
  121. {
  122.     [_image release], _image = nil;
  123.    
  124.     [super dealloc];
  125. }
  126.  
  127. @end
  128.