3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License.
9 #import "HBImageAndTextCell.h"
13 xLeftInRect(NSSize innerSize, NSRect outerRect)
15 return NSMinX(outerRect);
19 xCenterInRect(NSSize innerSize, NSRect outerRect)
21 return MAX(NSMidX(outerRect) - (innerSize.width/2.0), 0.0);
25 xRightInRect(NSSize innerSize, NSRect outerRect)
27 return MAX(NSMaxX(outerRect) - innerSize.width, 0.0);
31 yTopInRect(NSSize innerSize, NSRect outerRect, BOOL flipped)
34 return NSMinY(outerRect);
36 return MAX(NSMaxY(outerRect) - innerSize.height, 0.0);
40 yCenterInRect(NSSize innerSize, NSRect outerRect, BOOL flipped)
42 return MAX(NSMidY(outerRect) - innerSize.height/2.0, 0.0);
46 yBottomInRect(NSSize innerSize, NSRect outerRect, BOOL flipped)
49 return MAX(NSMaxY(outerRect) - innerSize.height, 0.0);
51 return NSMinY(outerRect);
55 scaleProportionally(NSSize imageSize, NSRect canvasRect)
59 // get the smaller ratio and scale the image size by it
60 ratio = MIN(NSWidth(canvasRect) / imageSize.width,
61 NSHeight(canvasRect) / imageSize.height);
63 imageSize.width *= ratio;
64 imageSize.height *= ratio;
71 @implementation HBImageAndTextCell
73 -(id)initTextCell:(NSString *)aString
75 if (self = [super initTextCell:aString])
77 imageAlignment = NSImageAlignTop;
78 imageSpacing = NSMakeSize (3.0, 2.0);
83 -(id)initWithCoder:(NSCoder *)decoder
85 if (self = [super initWithCoder:decoder])
87 imageAlignment = NSImageAlignTop;
88 imageSpacing = NSMakeSize (3.0, 2.0);
100 - copyWithZone:(NSZone *)zone
102 HBImageAndTextCell *cell = (HBImageAndTextCell *)[super copyWithZone:zone];
103 cell->image = [image retain];
107 - (void)setImage:(NSImage *)anImage
109 if (anImage != image)
112 image = [anImage retain];
121 - (void) setImageAlignment:(NSImageAlignment)alignment;
123 imageAlignment = alignment;
126 - (NSImageAlignment) imageAlignment;
128 return imageAlignment;
131 - (void)setImageSpacing:(NSSize)aSize;
133 imageSpacing = aSize;
136 - (NSSize)imageSpacing
141 - (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent
143 NSRect textFrame, imageFrame;
144 NSDivideRect (aRect, &imageFrame, &textFrame, (imageSpacing.width * 2) + [image size].width, NSMinXEdge);
145 [super editWithFrame: textFrame inView: controlView editor:textObj delegate:anObject event: theEvent];
148 - (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength
150 NSRect textFrame, imageFrame;
151 NSDivideRect (aRect, &imageFrame, &textFrame, (imageSpacing.width * 2) + [image size].width, NSMinXEdge);
152 [super selectWithFrame: textFrame inView: controlView editor:textObj delegate:anObject start:selStart length:selLength];
155 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
163 imageSize = [image size];
164 NSDivideRect(cellFrame, &imageFrame, &cellFrame, (imageSpacing.width * 2) + imageSize.width, NSMinXEdge);
165 if ([self drawsBackground])
167 [[self backgroundColor] set];
168 NSRectFill(imageFrame);
170 imageFrame.origin.x += imageSpacing.width;
171 imageFrame.size = imageSize;
173 switch (imageAlignment)
176 case NSImageAlignTop:
177 if ([controlView isFlipped])
178 imageFrame.origin.y += imageFrame.size.height;
180 imageFrame.origin.y += (cellFrame.size.height - imageFrame.size.height);
183 case NSImageAlignCenter:
184 if ([controlView isFlipped])
185 imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2);
187 imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
190 case NSImageAlignBottom:
191 if ([controlView isFlipped])
192 imageFrame.origin.y += cellFrame.size.height;
193 // for unflipped, imageFrame is already correct
198 [image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
201 [super drawWithFrame:cellFrame inView:controlView];
205 #if 0 // this snippet supports all alignment values plus potentially scaling.
212 BOOL flipped = [controlView isFlipped];
214 imageSize = [image size];
215 srcImageSize = imageSize; // this will be more useful once/if we support scaling
217 NSDivideRect(cellFrame, &imageFrame, &cellFrame, 12 + imageSize.width, NSMinXEdge);
218 if ([self drawsBackground])
220 [[self backgroundColor] set];
221 NSRectFill(imageFrame);
224 switch (imageAlignment)
227 case NSImageAlignLeft:
228 position.x = xLeftInRect(imageSize, imageFrame);
229 position.y = yCenterInRect(imageSize, imageFrame, flipped);
231 case NSImageAlignRight:
232 position.x = xRightInRect(imageSize, imageFrame);
233 position.y = yCenterInRect(imageSize, imageFrame, flipped);
235 case NSImageAlignCenter:
236 position.x = xCenterInRect(imageSize, imageFrame);
237 position.y = yCenterInRect(imageSize, imageFrame, flipped);
239 case NSImageAlignTop:
240 position.x = xCenterInRect(imageSize, imageFrame);
241 position.y = yTopInRect(imageSize, imageFrame, flipped);
243 case NSImageAlignBottom:
244 position.x = xCenterInRect(imageSize, imageFrame);
245 position.y = yBottomInRect(imageSize, imageFrame, flipped);
247 case NSImageAlignTopLeft:
248 position.x = xLeftInRect(imageSize, imageFrame);
249 position.y = yTopInRect(imageSize, imageFrame, flipped);
251 case NSImageAlignTopRight:
252 position.x = xRightInRect(imageSize, imageFrame);
253 position.y = yTopInRect(imageSize, imageFrame, flipped);
255 case NSImageAlignBottomLeft:
256 position.x = xLeftInRect(imageSize, imageFrame);
257 position.y = yBottomInRect(imageSize, imageFrame, flipped);
259 case NSImageAlignBottomRight:
260 position.x = xRightInRect(imageSize, imageFrame);
261 position.y = yBottomInRect(imageSize, imageFrame, flipped);
265 // account for flipped views
268 position.y += imageSize.height;
269 imageSize.height = -imageSize.height;
272 // Set image flipping to match view. Don't know if this is really the best way
273 // to deal with flipped views and images.
274 if ([image isFlipped] != flipped)
275 [image setFlipped: flipped];
278 [image drawInRect: NSMakeRect(position.x, position.y, imageSize.width, imageSize.height)
279 fromRect: NSMakeRect(0, 0, srcImageSize.width,
281 operation: NSCompositeSourceOver
286 [super drawWithFrame:cellFrame inView:controlView];
292 NSSize cellSize = [super cellSize];
293 cellSize.width += (image ? [image size].width + (imageSpacing.width * 2) : 0);