Fixed NSScaleProportionally, and documented NSImageCell

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@26159 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Nicola Pero 2008-03-01 12:34:32 +00:00
parent 6afdbb5209
commit a4725a6065
3 changed files with 137 additions and 60 deletions

View file

@ -1,3 +1,10 @@
2008-03-01 Nicola Pero <nicola.pero@meta-innovation.com>
* Source/NSImageCell.m (scaleProportionally): Fixed
NSScaleProportionally to scale images down, but never up. Bug
reported by Otto Herbo <herbo@gmac.com>.
* Headers/AppKit/NSImageCell.h: Documented the whole class.
2008-02-26 Richard Frith-Macdonald <rfm@gnu.org>
* Source/GSToolTips.m: Fix bug #22421

View file

@ -33,12 +33,35 @@
#include <AppKit/NSCell.h>
/**
* <p>Enumeration of the ways that you can display an image in an
* NSImageCell. The available ones are:</p>
* <p><code>NSScaleNone</code>: The image is always displayed with
* its natural size. If it's bigger than the cell size, it is
* cropped.</p>
* <p><code>NSScaleProportionally</code>: If the image is bigger
* than the cell size, it is displayed in its natural size. If it
* is smaller than the cell size, it is resized down proportionally
* to fit the cell size.</p>
* <p><code>NSScaleToFit</code>: The image is always resized (up
* or down) to fit exactly in the cell size.</p>
*/
typedef enum {
NSScaleProportionally = 0, // Fit proportionally
NSScaleToFit, // Forced fit (distort if necessary)
NSScaleNone // Don't scale (clip)
NSScaleProportionally = 0,
NSScaleToFit,
NSScaleNone
} NSImageScaling;
/**
* <p>Enumeration of the ways that you can align an image inside an
* NSImageCell when the image is not taking up all the space inside
* the cell (for example, because you are using NSScaleNone or
* NSScaleProportionally and the cell size is bigger than the natural
* image size). The available ones are: <code>NSImageAlignCenter,
* NSImageAlignTop, NSImageAlignTopLeft, NSImageAlignTopRight,
* NSImageAlignLeft, NSImageAlignBottom, NSImageAlignBottomLeft,
* NSImageAlignBottomRight, NSImageAlignRight</code>.</p>
*/
typedef enum {
NSImageAlignCenter = 0,
NSImageAlignTop,
@ -51,6 +74,12 @@ typedef enum {
NSImageAlignRight
} NSImageAlignment;
/**
* <p>Enumeration of the types of frame that can be used in an
* NSImageCell. The available ones are: <code>NSImageFrameNone,
* NSImageFramePhoto, NSImageFrameGrayBezel, NSImageFrameGroove,
* NSImageFrameButton</code>.</p>
*/
typedef enum {
NSImageFrameNone = 0,
NSImageFramePhoto,
@ -59,27 +88,61 @@ typedef enum {
NSImageFrameButton
} NSImageFrameStyle;
/**
* <p>An NSImageCell is a cell that can display a single image. It
* is normally associated with an NSImageView control; but you can
* use it as a building block in your own controls.</p>
*
* <p>The image to display is set using the -setImage: method
* which is inherited from the superclass.</p>
*
* <p>The -setImageAlignment: and -setImageScaling: methods can be
* used to control how the image is drawn inside a rectangle which is
* larger or smaller than the image size; the image might need to be
* scaled, cropped or aligned.</p>
*
* <p>The -setImageFrameStyle: method can be used to control if the
* cell should display a frame border, and which one.</p>
*/
@interface NSImageCell : NSCell
{
// Attributes
NSImageAlignment _imageAlignment;
NSImageFrameStyle _frameStyle;
NSImageScaling _imageScaling;
NSSize _original_image_size;
}
//
// Aligning and scaling the image
//
/**
* Returns the alignment used when displaying the image inside
* a cell that is bigger than the image, and NSScaleToFit has
* not been selected.
*/
- (NSImageAlignment) imageAlignment;
/**
* Sets the alignment used when displaying the image inside a cell
* that is bigger than the image, and NSScaleToFit has not been
* selected.
*/
- (void) setImageAlignment: (NSImageAlignment)anAlignment;
/**
* Returns the type of image scaling used on the image when the
* image natural size and the cell size are different.
*/
- (NSImageScaling) imageScaling;
/**
* Sets the type of image scaling used on the image when the image
* natural size and the cell size are different.
*/
- (void) setImageScaling: (NSImageScaling)scaling;
//
// Choosing the frame
//
/**
* Returns the style used to draw the frame around the image.
*/
- (NSImageFrameStyle) imageFrameStyle;
/**
* Sets the style used to draw the frame around the image.
*/
- (void) setImageFrameStyle: (NSImageFrameStyle)aFrameStyle;
@end

View file

@ -68,7 +68,7 @@
- (void)setObjectValue:(id)object
{
if ((object == nil) || ([object isKindOfClass:[NSImage class]]))
if ((object == nil) || ([object isKindOfClass:[NSImage class]]))
{
[self setImage: object];
}
@ -112,8 +112,8 @@
- (void) setImageFrameStyle: (NSImageFrameStyle)aFrameStyle
{
// We could set _cell.is_bordered and _cell.is_bezeled here to reflect
// the border type, but this wont be used.
// We could set _cell.is_bordered and _cell.is_bezeled here to
// reflect the border type, but this wont be used.
_frameStyle = aFrameStyle;
}
@ -125,27 +125,27 @@
NSDebugLLog(@"NSImageCell", @"NSImageCell -drawWithFrame");
// do nothing if cell's frame rect is zero
if (NSIsEmptyRect(cellFrame))
if (NSIsEmptyRect (cellFrame))
return;
// draw the border if needed
switch (_frameStyle)
{
case NSImageFrameNone:
// nada
break;
case NSImageFramePhoto:
[[GSTheme theme] drawFramePhoto: cellFrame withClip: NSZeroRect];
break;
case NSImageFrameGrayBezel:
[[GSTheme theme] drawGrayBezel: cellFrame withClip: NSZeroRect];
break;
case NSImageFrameGroove:
[[GSTheme theme] drawGroove: cellFrame withClip: NSZeroRect];
break;
case NSImageFrameButton:
[[GSTheme theme] drawButton: cellFrame withClip: NSZeroRect];
break;
case NSImageFrameNone:
// do nothing
break;
case NSImageFramePhoto:
[[GSTheme theme] drawFramePhoto: cellFrame withClip: NSZeroRect];
break;
case NSImageFrameGrayBezel:
[[GSTheme theme] drawGrayBezel: cellFrame withClip: NSZeroRect];
break;
case NSImageFrameGroove:
[[GSTheme theme] drawGroove: cellFrame withClip: NSZeroRect];
break;
case NSImageFrameButton:
[[GSTheme theme] drawButton: cellFrame withClip: NSZeroRect];
break;
}
[self drawInteriorWithFrame: cellFrame inView: controlView];
@ -198,12 +198,19 @@ scaleProportionally(NSSize imageSize, NSRect canvasRect)
{
float ratio;
// get the smaller ratio and scale the image size by it
/* Get the smaller ratio and scale the image size by it. */
ratio = MIN(NSWidth(canvasRect) / imageSize.width,
NSHeight(canvasRect) / imageSize.height);
imageSize.width *= ratio;
imageSize.height *= ratio;
/* According to the API (see NSScaleProportionally), we should never
* scale images up; we only scale them down. If you want your image
* to scale both up and down, you should use NSScaleToFit.
*/
if (ratio < 1.0)
{
imageSize.width *= ratio;
imageSize.height *= ratio;
}
return imageSize;
}
@ -311,19 +318,19 @@ scaleProportionally(NSSize imageSize, NSRect canvasRect)
// Get border size
switch (_frameStyle)
{
case NSImageFrameNone:
default:
borderSize = NSZeroSize;
break;
case NSImageFramePhoto:
// FIXME
borderSize = _sizeForBorderType (NSNoBorder);
break;
case NSImageFrameGrayBezel:
case NSImageFrameGroove:
case NSImageFrameButton:
borderSize = _sizeForBorderType (NSBezelBorder);
break;
case NSImageFrameNone:
default:
borderSize = NSZeroSize;
break;
case NSImageFramePhoto:
// FIXME
borderSize = _sizeForBorderType (NSNoBorder);
break;
case NSImageFrameGrayBezel:
case NSImageFrameGroove:
case NSImageFrameButton:
borderSize = _sizeForBorderType (NSBezelBorder);
break;
}
// Get Content Size
@ -343,21 +350,21 @@ scaleProportionally(NSSize imageSize, NSRect canvasRect)
// Get border size
switch (_frameStyle)
{
case NSImageFrameNone:
default:
borderSize = NSZeroSize;
break;
case NSImageFramePhoto:
// what does this one look like? TODO (in sync with the rest of the code)
borderSize = _sizeForBorderType (NSNoBorder);
break;
case NSImageFrameGrayBezel:
case NSImageFrameGroove:
case NSImageFrameButton:
borderSize = _sizeForBorderType (NSBezelBorder);
break;
case NSImageFrameNone:
default:
borderSize = NSZeroSize;
break;
case NSImageFramePhoto:
// what does this one look like? TODO (in sync with the rest of the code)
borderSize = _sizeForBorderType (NSNoBorder);
break;
case NSImageFrameGrayBezel:
case NSImageFrameGroove:
case NSImageFrameButton:
borderSize = _sizeForBorderType (NSBezelBorder);
break;
}
return NSInsetRect (theRect, borderSize.width, borderSize.height);
}