2001-12-17 16:51:51 +00:00
|
|
|
/** <title>NSImageCell</title>
|
1999-06-05 20:30:29 +00:00
|
|
|
|
2001-12-17 16:51:51 +00:00
|
|
|
<abstract>The image cell class</abstract>
|
1999-06-05 20:30:29 +00:00
|
|
|
|
2005-01-28 16:58:10 +00:00
|
|
|
Copyright (C) 1999, 2005 Free Software Foundation, Inc.
|
1999-06-05 20:30:29 +00:00
|
|
|
|
2001-12-17 16:51:51 +00:00
|
|
|
Author: Jonathan Gapen <jagapen@smithlab.chem.wisc.edu>
|
1999-06-05 20:30:29 +00:00
|
|
|
Date: 1999
|
|
|
|
|
|
|
|
This file is part of the GNUstep GUI Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-10-29 21:16:17 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1999-06-05 20:30:29 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-10 04:01:49 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
1999-06-05 20:30:29 +00:00
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2007-10-29 21:16:17 +00:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
1999-06-05 20:30:29 +00:00
|
|
|
|
2007-10-29 21:16:17 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1999-06-05 20:30:29 +00:00
|
|
|
License along with this library; see the file COPYING.LIB.
|
2007-10-29 21:16:17 +00:00
|
|
|
If not, see <http://www.gnu.org/licenses/> or write to the
|
|
|
|
Free Software Foundation, 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
1999-06-05 20:30:29 +00:00
|
|
|
*/
|
|
|
|
|
2003-07-31 23:52:10 +00:00
|
|
|
#include "config.h"
|
2011-03-04 11:33:22 +00:00
|
|
|
#import <Foundation/NSDebug.h>
|
|
|
|
#import "AppKit/NSAffineTransform.h"
|
|
|
|
#import "AppKit/NSCell.h"
|
|
|
|
#import "AppKit/NSGraphics.h"
|
|
|
|
#import "AppKit/NSImageCell.h"
|
|
|
|
#import "AppKit/NSImage.h"
|
|
|
|
#import "GNUstepGUI/GSTheme.h"
|
|
|
|
#import "GSGuiPrivate.h"
|
1999-06-05 20:30:29 +00:00
|
|
|
|
2011-11-11 23:10:15 +00:00
|
|
|
@interface NSCell (Private)
|
|
|
|
- (NSSize) _scaleImageWithSize: (NSSize)imageSize
|
|
|
|
toFitInSize: (NSSize)canvasSize
|
|
|
|
scalingType: (NSImageScaling)scalingType;
|
|
|
|
@end
|
|
|
|
|
1999-06-05 20:30:29 +00:00
|
|
|
@implementation NSImageCell
|
|
|
|
|
|
|
|
//
|
|
|
|
// Class methods
|
|
|
|
//
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [NSImageCell class])
|
1999-12-07 06:40:21 +00:00
|
|
|
{
|
|
|
|
[self setVersion: 1];
|
|
|
|
}
|
1999-06-05 20:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Instance methods
|
|
|
|
//
|
|
|
|
- (id) init
|
|
|
|
{
|
2008-09-08 18:59:48 +00:00
|
|
|
return [self initImageCell: nil];
|
1999-06-05 20:30:29 +00:00
|
|
|
}
|
|
|
|
|
1999-12-07 06:40:21 +00:00
|
|
|
- (void) setImage:(NSImage *)anImage
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
|
|
|
[super setImage:anImage];
|
2002-08-03 03:32:19 +00:00
|
|
|
if (anImage)
|
|
|
|
_original_image_size = [anImage size];
|
|
|
|
else
|
|
|
|
_original_image_size = NSMakeSize(1,1);
|
1999-06-05 20:30:29 +00:00
|
|
|
}
|
|
|
|
|
2003-08-18 03:28:58 +00:00
|
|
|
- (void)setObjectValue:(id)object
|
|
|
|
{
|
2008-03-01 12:34:32 +00:00
|
|
|
if ((object == nil) || ([object isKindOfClass:[NSImage class]]))
|
2003-08-18 03:28:58 +00:00
|
|
|
{
|
|
|
|
[self setImage: object];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[super setObjectValue: object];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-06-05 20:30:29 +00:00
|
|
|
//
|
|
|
|
// Aligning and scaling the image
|
|
|
|
//
|
1999-12-07 06:40:21 +00:00
|
|
|
- (NSImageAlignment) imageAlignment
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
|
|
|
return _imageAlignment;
|
|
|
|
}
|
|
|
|
|
1999-12-07 06:40:21 +00:00
|
|
|
- (void) setImageAlignment: (NSImageAlignment)anAlignment
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
|
|
|
NSDebugLLog(@"NSImageCell", @"NSImageCell -setImageAlignment");
|
|
|
|
_imageAlignment = anAlignment;
|
|
|
|
}
|
|
|
|
|
1999-12-07 06:40:21 +00:00
|
|
|
- (NSImageScaling) imageScaling
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
|
|
|
return _imageScaling;
|
|
|
|
}
|
|
|
|
|
1999-12-07 06:40:21 +00:00
|
|
|
- (void) setImageScaling: (NSImageScaling)scaling
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
|
|
|
_imageScaling = scaling;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Choosing the frame
|
|
|
|
//
|
1999-12-07 06:40:21 +00:00
|
|
|
- (NSImageFrameStyle) imageFrameStyle
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
|
|
|
return _frameStyle;
|
|
|
|
}
|
|
|
|
|
1999-12-07 06:40:21 +00:00
|
|
|
- (void) setImageFrameStyle: (NSImageFrameStyle)aFrameStyle
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
2008-03-01 12:34:32 +00:00
|
|
|
// We could set _cell.is_bordered and _cell.is_bezeled here to
|
|
|
|
// reflect the border type, but this wont be used.
|
1999-06-05 20:30:29 +00:00
|
|
|
_frameStyle = aFrameStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Displaying
|
|
|
|
//
|
2008-09-08 18:59:48 +00:00
|
|
|
- (void) _drawBorderAndBackgroundWithFrame: (NSRect)cellFrame
|
|
|
|
inView: (NSView *)controlView
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
2008-09-08 18:59:48 +00:00
|
|
|
NSDebugLLog(@"NSImageCell", @"NSImageCell -_drawBorderAndBackgroundWithFrame");
|
|
|
|
[[GSTheme theme] drawBorderForImageFrameStyle: _frameStyle
|
|
|
|
frame: cellFrame
|
|
|
|
view: controlView];
|
1999-06-05 20:30:29 +00:00
|
|
|
}
|
|
|
|
|
1999-12-07 06:40:21 +00:00
|
|
|
static inline float
|
|
|
|
xLeftInRect(NSSize innerSize, NSRect outerRect)
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
|
|
|
return NSMinX(outerRect);
|
|
|
|
}
|
|
|
|
|
1999-12-07 06:40:21 +00:00
|
|
|
static inline float
|
|
|
|
xCenterInRect(NSSize innerSize, NSRect outerRect)
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
|
|
|
return MAX(NSMidX(outerRect) - (innerSize.width/2.0), 0.0);
|
|
|
|
}
|
|
|
|
|
1999-12-07 06:40:21 +00:00
|
|
|
static inline float
|
|
|
|
xRightInRect(NSSize innerSize, NSRect outerRect)
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
|
|
|
return MAX(NSMaxX(outerRect) - innerSize.width, 0.0);
|
|
|
|
}
|
|
|
|
|
1999-12-07 06:40:21 +00:00
|
|
|
static inline float
|
|
|
|
yTopInRect(NSSize innerSize, NSRect outerRect, BOOL flipped)
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
1999-12-01 13:49:47 +00:00
|
|
|
if (flipped)
|
1999-06-05 20:30:29 +00:00
|
|
|
return NSMinY(outerRect);
|
|
|
|
else
|
|
|
|
return MAX(NSMaxY(outerRect) - innerSize.height, 0.0);
|
|
|
|
}
|
|
|
|
|
1999-12-07 06:40:21 +00:00
|
|
|
static inline float
|
|
|
|
yCenterInRect(NSSize innerSize, NSRect outerRect, BOOL flipped)
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
|
|
|
return MAX(NSMidY(outerRect) - innerSize.height/2.0, 0.0);
|
|
|
|
}
|
|
|
|
|
1999-12-07 06:41:28 +00:00
|
|
|
static inline float
|
1999-12-07 06:40:21 +00:00
|
|
|
yBottomInRect(NSSize innerSize, NSRect outerRect, BOOL flipped)
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
1999-12-01 13:49:47 +00:00
|
|
|
if (flipped)
|
1999-06-05 20:30:29 +00:00
|
|
|
return MAX(NSMaxY(outerRect) - innerSize.height, 0.0);
|
|
|
|
else
|
|
|
|
return NSMinY(outerRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView *)controlView
|
|
|
|
{
|
1999-12-07 06:40:21 +00:00
|
|
|
NSPoint position;
|
|
|
|
BOOL is_flipped = [controlView isFlipped];
|
2008-09-08 18:59:48 +00:00
|
|
|
NSSize imageSize, realImageSize;
|
2011-12-14 18:52:42 +00:00
|
|
|
NSRect rect;
|
1999-06-05 20:30:29 +00:00
|
|
|
|
|
|
|
NSDebugLLog(@"NSImageCell", @"NSImageCell drawInteriorWithFrame called");
|
|
|
|
|
2005-05-22 21:12:08 +00:00
|
|
|
if (!_cell_image)
|
1999-06-05 20:30:29 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// leave room for the frame
|
1999-09-12 19:56:22 +00:00
|
|
|
cellFrame = [self drawingRectForBounds: cellFrame];
|
1999-06-05 20:30:29 +00:00
|
|
|
|
2005-01-25 00:01:50 +00:00
|
|
|
realImageSize = [_cell_image size];
|
2011-11-11 23:10:15 +00:00
|
|
|
imageSize = [self _scaleImageWithSize: realImageSize
|
|
|
|
toFitInSize: cellFrame.size
|
|
|
|
scalingType: _imageScaling];
|
1999-12-01 13:49:47 +00:00
|
|
|
|
1999-12-13 02:43:52 +00:00
|
|
|
switch (_imageAlignment)
|
1999-06-05 20:30:29 +00:00
|
|
|
{
|
2005-01-23 23:45:20 +00:00
|
|
|
default:
|
1999-12-01 13:49:47 +00:00
|
|
|
case NSImageAlignLeft:
|
2008-09-08 18:59:48 +00:00
|
|
|
position.x = xLeftInRect(imageSize, cellFrame);
|
|
|
|
position.y = yCenterInRect(imageSize, cellFrame, is_flipped);
|
|
|
|
break;
|
1999-12-01 13:49:47 +00:00
|
|
|
case NSImageAlignRight:
|
2008-09-08 18:59:48 +00:00
|
|
|
position.x = xRightInRect(imageSize, cellFrame);
|
|
|
|
position.y = yCenterInRect(imageSize, cellFrame, is_flipped);
|
|
|
|
break;
|
1999-12-01 13:49:47 +00:00
|
|
|
case NSImageAlignCenter:
|
2008-09-08 18:59:48 +00:00
|
|
|
position.x = xCenterInRect(imageSize, cellFrame);
|
|
|
|
position.y = yCenterInRect(imageSize, cellFrame, is_flipped);
|
|
|
|
break;
|
1999-12-01 13:49:47 +00:00
|
|
|
case NSImageAlignTop:
|
2008-09-08 18:59:48 +00:00
|
|
|
position.x = xCenterInRect(imageSize, cellFrame);
|
|
|
|
position.y = yTopInRect(imageSize, cellFrame, is_flipped);
|
|
|
|
break;
|
1999-12-01 13:49:47 +00:00
|
|
|
case NSImageAlignBottom:
|
2008-09-08 18:59:48 +00:00
|
|
|
position.x = xCenterInRect(imageSize, cellFrame);
|
|
|
|
position.y = yBottomInRect(imageSize, cellFrame, is_flipped);
|
|
|
|
break;
|
1999-12-01 13:49:47 +00:00
|
|
|
case NSImageAlignTopLeft:
|
2008-09-08 18:59:48 +00:00
|
|
|
position.x = xLeftInRect(imageSize, cellFrame);
|
|
|
|
position.y = yTopInRect(imageSize, cellFrame, is_flipped);
|
|
|
|
break;
|
1999-12-01 13:49:47 +00:00
|
|
|
case NSImageAlignTopRight:
|
2008-09-08 18:59:48 +00:00
|
|
|
position.x = xRightInRect(imageSize, cellFrame);
|
|
|
|
position.y = yTopInRect(imageSize, cellFrame, is_flipped);
|
|
|
|
break;
|
1999-12-01 13:49:47 +00:00
|
|
|
case NSImageAlignBottomLeft:
|
2008-09-08 18:59:48 +00:00
|
|
|
position.x = xLeftInRect(imageSize, cellFrame);
|
|
|
|
position.y = yBottomInRect(imageSize, cellFrame, is_flipped);
|
|
|
|
break;
|
1999-12-01 13:49:47 +00:00
|
|
|
case NSImageAlignBottomRight:
|
2008-09-08 18:59:48 +00:00
|
|
|
position.x = xRightInRect(imageSize, cellFrame);
|
|
|
|
position.y = yBottomInRect(imageSize, cellFrame, is_flipped);
|
|
|
|
break;
|
1999-06-05 20:30:29 +00:00
|
|
|
}
|
|
|
|
|
2011-12-14 18:52:42 +00:00
|
|
|
rect = NSMakeRect(position.x, position.y,
|
|
|
|
imageSize.width, imageSize.height);
|
|
|
|
|
|
|
|
if (nil != controlView)
|
|
|
|
{
|
|
|
|
rect = [controlView centerScanRect: rect];
|
|
|
|
}
|
|
|
|
|
2012-01-31 02:27:18 +00:00
|
|
|
CGFloat fraction = 1.0;
|
|
|
|
|
|
|
|
if (_frameStyle == NSImageFrameNone
|
|
|
|
&& ![self isEnabled])
|
|
|
|
{
|
|
|
|
fraction = 0.5;
|
|
|
|
}
|
2012-01-15 21:35:47 +00:00
|
|
|
|
1999-06-05 20:30:29 +00:00
|
|
|
// draw!
|
2011-12-14 18:52:42 +00:00
|
|
|
[_cell_image drawInRect: rect
|
2008-09-08 18:59:48 +00:00
|
|
|
fromRect: NSMakeRect(0, 0, realImageSize.width,
|
|
|
|
realImageSize.height)
|
|
|
|
operation: NSCompositeSourceOver
|
2012-01-15 21:35:47 +00:00
|
|
|
fraction: fraction
|
2011-09-07 08:18:40 +00:00
|
|
|
respectFlipped: YES
|
|
|
|
hints: nil];
|
2010-06-01 11:04:36 +00:00
|
|
|
|
1999-06-05 20:30:29 +00:00
|
|
|
}
|
|
|
|
|
1999-09-12 19:56:22 +00:00
|
|
|
- (NSSize) cellSize
|
|
|
|
{
|
|
|
|
NSSize borderSize, s;
|
|
|
|
|
|
|
|
// Get border size
|
2008-09-08 18:59:48 +00:00
|
|
|
borderSize = [[GSTheme theme] sizeForImageFrameStyle: _frameStyle];
|
1999-09-12 19:56:22 +00:00
|
|
|
|
|
|
|
// Get Content Size
|
|
|
|
s = _original_image_size;
|
|
|
|
|
|
|
|
// Add in border size
|
|
|
|
s.width += 2 * borderSize.width;
|
|
|
|
s.height += 2 * borderSize.height;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRect) drawingRectForBounds: (NSRect)theRect
|
|
|
|
{
|
|
|
|
NSSize borderSize;
|
|
|
|
|
|
|
|
// Get border size
|
2008-09-08 18:59:48 +00:00
|
|
|
borderSize = [[GSTheme theme] sizeForImageFrameStyle: _frameStyle];
|
1999-09-12 19:56:22 +00:00
|
|
|
return NSInsetRect (theRect, borderSize.width, borderSize.height);
|
|
|
|
}
|
|
|
|
|
1999-06-05 20:30:29 +00:00
|
|
|
//
|
|
|
|
// NSCoding protocol
|
|
|
|
//
|
|
|
|
- (void) encodeWithCoder: (NSCoder *)aCoder
|
|
|
|
{
|
|
|
|
[super encodeWithCoder: aCoder];
|
2006-10-15 08:34:47 +00:00
|
|
|
if ([aCoder allowsKeyedCoding])
|
2006-08-09 05:48:54 +00:00
|
|
|
{
|
|
|
|
[aCoder encodeInt: _imageAlignment forKey: @"NSAlign"];
|
|
|
|
[aCoder encodeInt: _imageScaling forKey: @"NSScale"];
|
|
|
|
[aCoder encodeInt: _frameStyle forKey: @"NSStyle"];
|
|
|
|
[aCoder encodeBool: NO forKey: @"NSAnimates"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-27 11:57:04 +00:00
|
|
|
[aCoder encodeValueOfObjCType: @encode(int) at: &_imageAlignment];
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(int) at: &_frameStyle];
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(int) at: &_imageScaling];
|
2006-08-09 05:48:54 +00:00
|
|
|
[aCoder encodeSize: _original_image_size];
|
|
|
|
}
|
1999-06-05 20:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithCoder: (NSCoder *)aDecoder
|
|
|
|
{
|
2006-10-15 08:34:47 +00:00
|
|
|
if ((self = [super initWithCoder: aDecoder]) != nil)
|
2004-02-02 21:40:00 +00:00
|
|
|
{
|
2006-08-09 05:48:54 +00:00
|
|
|
if ([aDecoder allowsKeyedCoding])
|
|
|
|
{
|
|
|
|
if ([aDecoder containsValueForKey: @"NSAlign"])
|
|
|
|
{
|
|
|
|
[self setImageAlignment: [aDecoder decodeIntForKey: @"NSAlign"]];
|
|
|
|
}
|
|
|
|
if ([aDecoder containsValueForKey: @"NSScale"])
|
|
|
|
{
|
|
|
|
[self setImageScaling: [aDecoder decodeIntForKey: @"NSScale"]];
|
|
|
|
}
|
|
|
|
if ([aDecoder containsValueForKey: @"NSStyle"])
|
|
|
|
{
|
|
|
|
[self setImageFrameStyle: [aDecoder decodeIntForKey: @"NSStyle"]];
|
|
|
|
}
|
|
|
|
if ([aDecoder containsValueForKey: @"NSAnimates"])
|
|
|
|
{
|
|
|
|
//BOOL animates = [aDecoder decodeBoolForKey: @"NSAnimates"];
|
|
|
|
}
|
2004-02-02 21:40:00 +00:00
|
|
|
}
|
2006-08-09 05:48:54 +00:00
|
|
|
else
|
|
|
|
{
|
2010-12-27 11:57:04 +00:00
|
|
|
[aDecoder decodeValueOfObjCType: @encode(int) at: &_imageAlignment];
|
|
|
|
[aDecoder decodeValueOfObjCType: @encode(int) at: &_frameStyle];
|
|
|
|
[aDecoder decodeValueOfObjCType: @encode(int) at: &_imageScaling];
|
2006-08-09 05:48:54 +00:00
|
|
|
_original_image_size = [aDecoder decodeSize];
|
2004-02-02 21:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
1999-06-05 20:30:29 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|